/herald

Log annotation for logging frameworks

Primary LanguageJavaApache License 2.0Apache-2.0

Herald

Android Arsenal Build Status Coverage Status Maven Codacy Badge

"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:

Main features

Supported logging frameworks

It is also possible to add other logging frameworks:

  • Create new class in your project, it should implement interface com.github.vbauer.herald.logger.LogFactory. Add all necessary logic about logger creation in this class.
  • Create ServiceLoader's file in your project: "META-INF/services/com.github.vbauer.herald.logger.LogFactory".
  • Add full class name of your new extension in this file.

That's all!

Setup

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'
}

Configuration

Java configuration

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.

Android configuration

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);
    }
}

Guice / RoboGuice configuration

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.

Spring configuration

Java based configuration

You need to configure only one BeanPostProcessor:

@Configuration
public class AppContext {

    @Bean
    public LogBeanPostProcessor logBeanPostProcessor() {
        return new LogBeanPostProcessor();
    }

}

XML Schema-based configuration

<?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>

Spring Boot support

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.

@Log annotation

You can use @Log annotation in 2 ways:

  • Put it on class - All suitable logger fields will be injected. Validation check will be switched off, so all undefined logger fields will be skipped.
  • Put it on field - It allows you to inject only suitable logger and throws 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).

FAQ

  • Q: How to select protocol for Syslog4j?
    • A: Use @Log.name(), default protocol is "udp".
  • Q: How to configure Syslog4j with Herald?
    • A: Use standard Syslog4j API, ex:
    final SyslogIF syslog = Syslog.getInstance("udp");
    syslog.getConfig().setHost("192.168.100.1");
    syslog.getConfig().setPort(1514);

Development

To build project in strict mode with tests, you can run:

mvn -P strict clean package

Might also like

  • jconditions - Extra conditional annotations for JUnit.
  • jackdaw - Java Annotation Processor which allows to simplify development.
  • houdini - Type conversion system for Java projects.
  • caesar - Library that allows to create async beans from sync beans.
  • commons-vfs2-cifs - SMB/CIFS provider for Commons VFS.
  • avconv4java - Java interface to avconv tool.

License

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.