DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Popular
  3. Open Source
  4. How Tinylog 1.2 Simplifies Your Logging

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.

Martin Winandy user avatar by
Martin Winandy
·
Mar. 02, 17 · Tutorial
Like (5)
Save
Tweet
Share
7.81K Views

Join the DZone community and get the full member experience.

Join For Free

Logging 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.

file IO Open source

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using Swagger for Creating a PingFederate Admin API Java Wrapper
  • 19 Most Common OpenSSL Commands for 2023
  • A Beginner’s Guide To Styling CSS Forms
  • Distributed Tracing: A Full Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: