SimpleLogging - Simple Logging Library

Maven Central Changelog Documentation Code Coverage CircleCi

The goal of this library is to be a small logging facade that backends to a number of standard logging packages and that can be copied into another project. This allows you to write your code and include log messages without having a fixed dependency on any one logging package. I include this code into my libraries and so they can stay agnostic. This logging code allows you to write messages with the slf4j-style {} argument support, handles arrays appropriately, and supports up to 4 arguments before forcing the caller to pass in an object array or calling a different logArgs(...) method for variable arguments.

I understand that this library is similar to other logging systems which separate their API from the implementation. I think SimpleLogging is better than the others because it doesn't use the classpath order to satisfy the connection between the API and the backend. SimpleLogging includes calls directly to specific backend APIs which can be chosen through code or configuration. This direct calling allows for more control over the backend selection and usually fewer dependencies.

SimpleLogging is also designed to be copied into your open source project so you don't have to add a maven dependency. Just copy the java files from src/main/java into your source tree and rename the packages as necessary. Please also copy the SIMPLELOGGING_LICENSE.txt file which is the very permissive ISC license.

Simple Code Sample

The following is a quick code sample to help you get started using the logging library.

// usually a logger will be per-class, getLogger() also can take a String label
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
...
// log trace message with arguments. toString() on the args only called if trace messages enabled
logger.trace("some trace information: {} and {}", arg1, arg2);
...
// NOTE: exception argument comes _before_ the message format to not confuse the arguments
logger.error(exception, "http client threw getting URL: {}", url);

"Fluent" Logging with Method Chaining Supported

SimpleLogging also supports "fluent" logging where you can chain log methods together to build your log message which will generate no objects if the log level is not enabled. For example, even with port being an int primitive below, there are no objects generated by thus call unless TRACE log level is enabled.

private static final FluentLogger fluentLogger =
    LoggerFactory.getFluentLogger(MyClass.class);
...
// this generates no objects even due to auto-boxing unless trace is enabled
fluentLogger.atLevel(Level.TRACE)
    .msg("connected to host '{}' port '{}'")
    .arg(host)
    .arg(port)
    .log();

Examples of Argument Processing

Here are some more examples showing the details about the argument logic:

// no arguments but a {} pattern
logger.info("connected to host '{}'");
// outputs: connected to host '{}'

// missing argument to second {} displays as empty string
String host = "host1";
logger.info("connected to host '{}' port '{}'", host);
// outputs: connected to host 'host1' port ''

// extra argument (port) is ignored
String host = "host1";
int port = 443;
logger.info("connected to host '{}'", host, port);
// outputs: connected to host 'host1'

// null argument
String host = null;
logger.info("connected to host '{}' failed", host);
// outputs: connected to host 'null'

// arguments that are arrays
String[] hosts = new String[] { "srv1", "srv2" };
logger.info("connected to hosts {} failed", hosts);
// outputs: connected to hosts [srv1, srv2]

// logging of Host which implements LogArgumentCreator where
// host.createArg() returns the string: host4
Host host = new Host();
logger.info("connected to host '{}'", host);
// outputs: connected to host 'host4'

// logging of Server where server.toString() returns: srv3
Server server = new Server();
logger.info("connected to host '{}'", server);
// outputs: connected to host 'srv3'

More 256 Sources

Free Spam Protection   Android ORM   Simple Java Zip   JMX using HTTP   Great Eggnog Recipe