Check out the latest documentation.

Using Logging Functionality

The 51Degrees Java Webapp relies on the SLF4J framework for logging the WebApp events and messages. The framework defines a basic abstraction that can be implemented differently by placing a new JAR in the classpath , making it easy to log events to files, databases or anywhere else.

You will need to supply an implementation for the SLF4J logger in order to log the events and messages of the WebApp JAR. The Core JAR does not make use of logging. Following is an example of adding a logger to the Java servlet that extends the 51Degrees BaseServlet class:

Servlet POM File

Add the following dependencies to your servlet POM file. If your project is not a Maven project download the JAR files manually.

										
										<dependency>
										
										
										<groupId>
										
									org.slf4j
										
										</groupId>
										
										
										<artifactId>
										
									slf4j-api
										
										</artifactId>
										
										
										<version>
										
									1.7.7
										
										</version>
										
										
										</dependency>
										
										
										<dependency>
										
										
										<groupId>
										
									ch.qos.logback
										
										</groupId>
										
										
										<artifactId>
										
									logback-classic
										
										</artifactId>
										
										
										<version>
										
									1.1.2
										
										</version>
										
										
										</dependency>
										
										
										<dependency>
										
										
										<groupId>
										
									ch.qos.logback
										
										</groupId>
										
										
										<artifactId>
										
									logback-core
										
										</artifactId>
										
										
										<version>
										
									1.1.2
										
										</version>
										
										
										</dependency>
										
									

Configure Logger

You should now configure the logger by adding the logback.xml file with settings to the src/main directory of your servlet. Sample logback.xml file:

										
										<?xml version="1.0" encoding="UTF-8"?>
										
										
										<configuration>
										
										
										<appender
										
										
										name=
										
										
										"STDOUT"
										
										
										class=
										
										
										"ch.qos.logback.core.ConsoleAppender"
										
										
										>
										
										
										<layout
										
										
										class=
										
										
										"ch.qos.logback.classic.PatternLayout"
										
										
										>
										
										
										<Pattern>
										
									
	  %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
	  
										
										</Pattern>
										
										
										</layout>
										
										
										</appender>
										
										
										<appender
										
										
										name=
										
										
										"FILE"
										
										
										class=
										
										
										"ch.qos.logback.core.rolling.RollingFileAppender"
										
										
										>
										
										
										<file>
										
									d:/WebApp.log
										
										</file>
										
										
										<encoder
										
										
										class=
										
										
										"ch.qos.logback.classic.encoder.PatternLayoutEncoder"
										
										
										>
										
										
										<Pattern>
										
									
	  %d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
	  
										
										</Pattern>
										
										
										</encoder>
										
										
										<rollingPolicy
										
										
										class=
										
										
										"ch.qos.logback.core.rolling.FixedWindowRollingPolicy"
										
										
										>
										
										
										<FileNamePattern>
										
									d:/jcg.%i.log.zip
										
										</FileNamePattern>
										
										
										<MinIndex>
										
									1
										
										</MinIndex>
										
										
										<MaxIndex>
										
									10
										
										</MaxIndex>
										
										
										</rollingPolicy>
										
										
										<triggeringPolicy
										
										
										class=
										
										
										"ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"
										
										
										>
										
										
										<MaxFileSize>
										
									2MB
										
										</MaxFileSize>
										
										
										</triggeringPolicy>
										
										
										</appender>
										
										
										<logger
										
										
										name=
										
										
										"fiftyone.mobile.example.servlet"
										
										
										level=
										
										
										"TRACE"
										
										
										>
										
										
										<appender-ref
										
										
										ref=
										
										
										"STDOUT"
										
										
										/>
										
										
										<appender-ref
										
										
										ref=
										
										
										"FILE"
										
										
										/>
										
										
										</logger>
										
										
										<logger
										
										
										name=
										
										
										"fiftyone.mobile.detection.webapp"
										
										
										level=
										
										
										"TRACE"
										
										
										>
										
										
										<appender-ref
										
										
										ref=
										
										
										"STDOUT"
										
										
										/>
										
										
										<appender-ref
										
										
										ref=
										
										
										"FILE"
										
										
										/>
										
										
										</logger>
										
										
										<root
										
										
										level=
										
										
										"debug"
										
										
										>
										
										
										<appender-ref
										
										
										ref=
										
										
										"STDOUT"
										
										
										/>
										
										
										</root>
										
										
										</configuration>
										
									

The config file above sets up two loggers: one for the example.servlet, another for the detection.webapp. Both loggers print to a console (STDOUT) as well as append to a log file (FILE). The console and file outputs are configured using the XML element. For the above example the output file location is: D:\WebApp.log.

Links and useful information: