How Tinylog 1.2 Simplifies Your Logging
tinylog 1.2 offers a lightweight alternative to some of the more popular professional Java application logging frameworks.
Join the DZone community and get the full member experience.
Join For FreeLogging is used in almost every professional Java application nowadays. The most common logging frameworks are Log4j and Logback. tinylog is a lightweight alternative. It has a size of only 94KB and tries to simplify logging and configuration. After a short introduction to tinylog, the new features of version 1.2 will be demonstrated by means of examples.
Logging API
tinylog has a static logger class. Thereby, it isn't necessary to create an instance of a logger for each class, in which logging should be used. Just write your logging statement:
import org.pmw.tinylog.Logger;
public class Application {
public static void main(String[] args) {
Logger.info("Hello World!");
}
}
The logger accepts plain objects, exceptions, strings, and formatted messages with placeholders. With tinylog 1.2, it is possible to format numeric placeholders. tinylog uses for numeric values NumberFormat in the same way as MessageFormat does, but the syntax is shorter:
MessageFormat.format("Price: {0,number,0.00} EUR", amount);
Logger.info("Price: {0.00} EUR", amount);
Of course, such format patterns are optional. If no format pattern has been defined, tinylog uses a standard way to format them. ChoiceFormat can be used to format values based on conditions.
MessageFormat.format("Received {0,choice,0#nothing|1#a single message|1<{0} messages}", count);
Logger.info("Received {0#nothing|1#a single message|1<{} messages}", count);
Configuration
If you develop a Java EE application, you might want to use the logging system of your application or web server. In this case, you can use tinylog-jboss for Wildfly and JBoss EAP or tinylog-jul for Tomcat and Glassfish. These libraries are replacements for the real tinylog JAR. They provide the same logging API, but redirects all log entries to the underlying logging system of the server and no configuration is required.
tinylog itself can be configured via properties files, system properties and/or a fluent Java API. In this article, properties files are used for example configurations. A complete manual can be found on the tinylog website.
The most interesting part of the configuration is how to configure a writer. tinylog can output log entries to console, files, databases, and logcat. A simple file writer could look like this:
tinylog.writer = file
tinylog.writer.filename = ${home}/logs/dummy.log
tinylog.writer.format = [{level)] {class}.{method}() {message}
Unix-like placeholders with environment variables (${home}
for example) will be resolved on any OS by tinylog automatically. With version 1.2, tinylog's JDBC writer supports Java EE data sources and reestablishing database connections automatically in case of connection losses. Example for a custom JDBC writer configuration:
tinylog.writer = jdbc
tinylog.writer.url = java:jboss/datasources/ExampleDS
tinylog.writer.reconnect = 60
tinylog.writer.table = LOG_ENTRIES_TABLE
tinylog.writer.columns = COLUMN_A, COLUMN_B
tinylog.writer.values = LEVEL, MESSAGE
Completely new in tinylog 1.2 is full support for Android out of the box. The new logcat writer can redirect all log entries to logcat. In opposite to Android's Log class, tinylog doesn't need a tag. Instead, it can automatically infer the tag from the class name. Of course, all other writers of tinylog can be used additionally. A configuration for logcat could look like this:
tinylog.writer = logcat
tinylog.level = trace
tinylog.writer.format = {message}
If you would like to configure multiple Java programs in the same way, you can provide a global properties file in your network. tinylog can load any configuration via URL. The URL can be defined as system property for example:
-Dtinylog.configuration=http://192.168.0.1/tinylog.properties
Performance
Log4j and other logging frameworks warn to output location information, such as method name or line number. Thanks to its architecture, tinylog can extract single stack trace element without loading the entire stack trace. That is pretty fast. Therefore, there is no performance reason to avoid outputting location information with tinylog.
If logging performance is important for you, you are might be interested in a benchmark that compares tinylog with Log4j and Logback. The open source benchmark program is adjustable for custom use cases. So, you can find the fastest logging framework for your personal use case.
Conclusion
This article could give only a brief introduction to tinylog. A complete manual with all configuration parameters can be found on the tinylog website. tinylog is published under the Apache License 2, thus it can be used in open source as well as in commercial projects. There are also a facade for Log4j 1.x as well as bindings for SLF4J and Apache Commons Logging.
Opinions expressed by DZone contributors are their own.
Comments