Logback

Logback Appender Example

In this example we are going to see some capabilities from logback logging framework.

Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.

We are going to see an example of a project that uses logback as logging framework.

For this example we use the following technologies:

  • MAC OSX
  • Eclipse Mars.1
  • Maven3
  • Logback 1.1.7
  • JDK 1.8.0_65 64bits

1. Introduction

Logback is a logging framework based on slf4j (Simple Logging Facade for Java). The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.

Logback’s architecture is sufficiently generic so as to apply under different circumstances. At present time, logback is divided into three modules, logback-core, logback-classic and logback-access.

The logback-core module is the main module and is the base for the other two modules. The logback-classic module is like a significantly improved version of log4j and is this module the one that natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks such as log4j or java.util.logging (JUL). The logback-access module integrates with Servlet containers, to provide HTTP-access log functionality.

Logback defines several appenders. This kind of components are responsible for the task of writing logs, the appenders delegates the log message format to a Layout or Encoder object inside the logback architecture.

2. Appenders architecture

Appender has the following design inside logback architecture

Appenders Class Diagram
Appenders Class Diagram

The basic logback appender components are derived from OutputStreamAppender and they delegate in a Encoder component the way the message will be printed, the most important appenders are the following ones:

  • ConsoleAppender: Appends messages to the console, or more formally to the System.err or System.out.
  • FileAppender: Appends messages to a file.
  • RollingFileAppender: Appends messages to a file that will be rotated when some conditions are reached.

The Encoder components are responsible for transform an incoming event into a byte array and writing out the byte array onto the appropriate OutputStream. Thus, encoders have total control of what and when gets written to the OutputStream maintained by the owning appender.

3. Example project

The example project is a java project which is building as a jar artifact. The project is modeled with maven, you can see the project structure below

Proyect structure
Proyect structure

The project has a class called MyClass. This class prints some messages with several severity levels through a slf4j logger. The logger configuration is described in a configuration file called logback.xml in the src/main/resources folder.

You can see the logback.xml file below

logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>
				%d{dd-MM-yyyy HH:mm:ss} - %msg%n
			</Pattern>
		</layout>
	</appender>
	
	<appender name="FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>/tmp/javacodegeeks_example.log</file>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<Pattern>
				%d{dd-MM-yyyy HH:mm:ss} - %msg%n
			</Pattern>
		</encoder>

		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- rollover daily -->
			<fileNamePattern>/tmp/archived/javacodegeeks_example.%d{dd-MM-yyyy}.%i.log</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>50MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>

	</appender>

	<logger name="com.javacodegeeks.example" level="debug"
		additivity="false">
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="FILE" />
	</logger>

	<root level="error">
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="FILE" />
	</root>

</configuration>

The logback configuration defines two appenders:

  • CONSOLE: Logback Console Appender that prints messages to the system standard output
  • FILE: Logback Rolling File Appender that prints messages to a file that will be rotated each 50 Mb.

The configuration specify that the classes below com.javacodegeeks.example package will be logged with debug severity to the CONSOLE and FILE appenders.

The rest of messages will be logged with error severity and will be delivered to the CONSOLE and FILE appenders.

4. Output

Run MyClass selecting it with right-click button and select Run As -> Java Application. The program output will be the following:

output:

19-05-2016 20:20:15 - Hello from javaCodeGeeks. Info
19-05-2016 20:20:15 - Hello from javaCodeGeeks. Warn
19-05-2016 20:20:15 - Hello from javaCodeGeeks. Error
19-05-2016 20:20:15 - Hello from javaCodeGeeks. Debug

You can see the messages logged in the console output, you can see the file appended in the tmp folder with the same content.

5. More appenders

Logback has more appenders that can be useful in some circunstances, you can see more details about it in the following site.

6. Conclusions

As you have seen, logback is quite simple to use and very powerfull too. You can configure your project easily and quickly in order to manage your log messages in a variety of ways, depending on your demand. Logback can be integrated with almost all the application servers as well as you can use it in standalone mode in the same way we did in this example.

7. Download

Download
You can download the full source code of this example here: logback appender example

Francisco Hernandez

JEE technologies geek and development engineer. I have over 13 years of experience as software engineer in JEE architectures: Design, development, improvement etc. Currently I work as software architect and consultant. I am mainly involved in projects related to the bank and energy sectors based on Java technologies and Oracle products. I am also very interested in open-source projects
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button