Marc Rufer bio photo

Marc Rufer

Software engineer
@d-fens GmbH

Twitter Google+ LinkedIn Github Stackoverflow
RSS Feed

Some hints concerning logging with Log4j

Marc Rufer 03 Mar 2015 Java logging Log4j

This blog post treats some useful information concerning logging with the logging framework Log4j version 2. The post covers the different log levels, the configuration for a rolling file appender and some information regarding performance.

Log4j-Logo

The logging framework Log4j is a Java Open Source project, which is part of the logging project of the [Apache Software Foundation}(http://www.apache.org/). It’s one of the most used logging frameworks.

Log levels

Log4j provides different log levels, which are listed and shortly described below

Level Description
TRACE Detailed logging i.e. for comments
DEBUG Debug logs for analyzing and troubleshooting bugs
INFO Common information about application execution like “Execution started”,..
WARN Logging unexpected occurrences
ERROR Exception logging (i.e. catched exceptions)
FATAL Critical errors and program termination

Configure rolling file appender

A common use case is configuring the logging framework to log some specific log messages or even all log messages to a file. With Log4j this can be done by adding the following configuration to the log4j.properties file

The first line sets the log level for the package org.web.rufer.sample to DEBUG and sets the log appender. Lines 4 to 9 configure the log appender named ROLLINGFILEAPPENDER. Besides the path and name of the file there are as well properties provided to set the maximal file size, maximal file backup count and of course the layout of the log messages.

Performance optimization

To optimize logging performance it’s advisable to use the methods, which take a string with placeholders and a list of objects as parameters (see Sample 2 and Sample 3). The advantage to use the placeholder variant instead of method with string parameter like in Sample 1 is, that the toString()-method of object arguments will only be evaluated, if the corresponding log level is activated. In Sample 1 the String will be built in every case. If you have a lot of debug logs with rich toString-logic this could be an issue.

For more information about Log4j consult the Log4j manual

comments powered by Disqus

Posts

HOWTO Access Azure File Share in PowerShell Script executed by a Scheduled Task BUG "Task Scheduler service is not available" Error, if specifying Network Connection Condition HOWTO Sync OneDrive on Server even if Windows User not logged in HOWTO Access Microsoft Access Database with PowerShell Get NetworkCredential from PSCredential splits Username into Username and Domain HOWTO Create LocalDB File (.mdf) manually in Visual Studio 2015 HOWTO Set Cookie Header on DefaultRequestHeaders of HttpClient HOWTO Set up PowerShell Remote Session Configuration HOWTO Analyze PowerShell Scripts with PSScriptAnalyzer HOWTO Sign PDF with SuisseID NoBrainer PUT/PATCH/MERGE with OData Service Client Apply Commit from one Repository to another GitHub Repository creation with PowerShell HOWTO Sign PowerShell Script with SuisseID Swissunihockey game schedule PDF generator HOWTO Maven Release on JetBrains TeamCity Limitations when running Activiti in H2 Embedded Mode HOWTO Pylint Integration in IntelliJ 14 HOWTO Install Windows 10 IoT Core on Raspberry PI 2 from a VM First steps with Dropwizard Allman code style for IntelliJ First steps with Flyway IntelliJ - Setup custom inspection profiles Configuration properties meta-data support in IntelliJ AMQP Integration with spring integration OCA Java SE 7 Programmer I Certification Guide Swissunihockey key matcher Swisscom SMS-API-Client Some hints concerning logging with Log4j IFTTT A brilliant service Run deltacloud on a VirtualBox-VM Project specific maven settings in IntelliJ AngularJS Javascript for beginners Liferay in Action