Log annotation for logging frameworks
"Why, sometimes I've believed as many as six impossible things before breakfast." - Lewis Carroll, Alice in Wonderland.
Herald provides a very simple way to initialize logger objects and does all magic for you.
You can annotate any field of some class with a @Log
annotation to let Herald inject suitable logger in this field.
It does not matter whether it is a static field or not.
Just forget about this code:
private static final Logger LOGGER =
LoggerFactory.getLogger(Foo.class);
Write less code, use short form:
@Log
private Logger logger;
Online documentation:
It is also possible to add other logging frameworks:
com.github.vbauer.herald.logger.LogFactory
.
Add all necessary logic about logger creation in this class.ServiceLoader
's file in your project: "META-INF/services/com.github.vbauer.herald.logger.LogFactory".That's all!
Maven:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<dependency>
<groupId>com.github.vbauer</groupId>
<artifactId>herald</artifactId>
<version>1.2.3</version>
</dependency>
Gradle:
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
compile 'com.github.vbauer:herald:1.2.3'
}
The project is integrated with Spring & Guice frameworks, but can be used without it:
LoggerInjector.inject(bean);
// or even using varargs:
LoggerInjector.inject(bean1, bean2, bean3);
As you can see, it is unnecessary to do some specific configuration when you use it in Java without IOC container.
You need to create base class for your component (ex: Activity) and call LoggerInjector.inject
:
public class BaseActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoggerInjector.inject(this);
}
}
Herald contains specific Guice module to support @Log
annotation (com.github.vbauer.herald.ext.guice.LogModule
):
final Injector injector = Guice.createInjector(new LogModule());
Now, all your beans will be processed with LoggerInjector and logger fields will be initialized if necessary.
You need to configure only one BeanPostProcessor
:
@Configuration
public class AppContext {
@Bean
public LogBeanPostProcessor logBeanPostProcessor() {
return new LogBeanPostProcessor();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.github.vbauer.herald.ext.spring.LogBeanPostProcessor" />
</beans>
Herald has out of the box integration with Spring Boot. You do not need to define LogBeanPostProcessor
in your application context.
Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.
See LogAutoConfiguration for more details.
You can use @Log
annotation in 2 ways:
MissedLogFactoryException
otherwise.It is also possible to configure logger name using this annotation:
@Log("MyCustomLoggerName")
private Logger logger;
If you do not specify it, then class name will be used as logger name.
If you want to specify mandatory for logger instantiation, use required
parameter (default is true
).
Use @Log(required = false)
to make your logger object optional (it could be useful in some rare cases).
final SyslogIF syslog = Syslog.getInstance("udp");
syslog.getConfig().setHost("192.168.100.1");
syslog.getConfig().setPort(1514);
To build project in strict mode with tests, you can run:
mvn -P strict clean package
Copyright 2015 Vladislav Bauer
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
See LICENSE file for more details.