Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Nanosecond Precise Logging With Tinylog

DZone's Guide to

Nanosecond Precise Logging With Tinylog

tinyloggers rejoice! See how the latest version of this open-source tool allows for up to nanosecond precision logging with Java projects.

· Java Zone ·
Free Resource

Take 60 minutes to understand the Power of the Actor Model with "Designing Reactive Systems: The Role Of Actors In Distributed Architecture". Brought to you in partnership with Lightbend.

In version 1.3, the logging framework tinylog can output log entries with nanosecond precision timestamps under Java 9. With version 9, Java offers, for the first time, the possibility to retrieve the current system time with nanosecond precision. In older Java versions, the precision is limited to milliseconds. However, there were regular requests, especially from the finance sector, to log events with microsecond precision. This is finally possible with tinylog 1.3.

Example With Tinylog

With tinylog, typically, properties files are used for configuring the open-source logging framework. Alternatively, there is also a fluent Java API for configuring tinylog. The example configuration below shows how to configure tinylog via a properties file to output log messages with microsecond precision on the console:

tinylog.writer = console
tinylog.format = {date:HH:mm:ss.SSSSSS} [{class}] {level}: {message}


The date pattern uses the DateTimeFormatter for formatting the time. For nanosecond precision, another three "S" letters simply have to be added. A complete user manual for configuring the logging framework is available on tinylog.org.

Here's an example Java application that uses tinylog for logging:

import org.pmw.tinylog.Logger;

public class Application {

    public static void main(String[] args) {
        Logger.info("Hello World!");
    }

}


As opposed to other logging frameworks, tinylog has a static logger. It is not necessary to create an instance of the logger before logging. If you use logging under Java, typically each class has an own logger instance and the logger is instantiated with the name of the current class. Such boilerplate code can be avoided with tinylog.

When running the example application class above, a log entry will be output to the console:

10:47:45.044819 [Application] INFO: Hello World!


Previous Java Versions

Even though tinylog supports Java 9 features — like precise timestamps — tinylog requires only Java 6, including support for Android. If tinylog is used with an older Java version, features that require a newer Java version can just not be used. For example, if you try to output a timestamp with nanosecond or microsecond precision under Java 6, tinylog will detect it and output a warning. Logging with tinylog will work nevertheless.

Learn how the Actor model provides a simple but powerful way to design and implement reactive applications that can distribute work across clusters of cores and servers. Brought to you in partnership with Lightbend.

Topics:
java ,logging ,tinylog ,java 9

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}