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.
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);
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();
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'
Free Spam Protection Android ORM Simple Java Zip JMX using HTTP Great Eggnog Recipe