Author |
Topic: Simple Example SLF4j + Log4j |
|
EricJ member offline |
|
posts: |
50 |
joined: |
02/22/2007 |
from: |
CA |
|
|
|
|
|
Simple Example SLF4j + Log4j |
SLF4J stands for Simple Logging Facade for Java which is an abstract layer of different logging frameworks. This make the code more reusable given that the real logging implementation can be plugged-in at end user's desire.
In this example, Log4j is chosen as the real logging framework.
What are required?
1) Abstraction -- slf4j-api-x.jar
2) Adapter -- slf4j-log4j-x.jar
3) The real framework -- log4j-x.jar
Code Example:
package com.mycompany.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SLF4j {
/** debug logger reference. */
private static Logger logger = LoggerFactory.getLogger(SLF4j.class);
public void hello(String name){
logger.error("Hello {}!", name);
logger.warn ("Hello {}!", name);
logger.info ("Hello {}!", name);
logger.debug("Hello {}!", name);
logger.trace("Hello {}!", name);
}
public static void main(String[] args){
SLF4j slf4j = new SLF4j();
slf4j.hello("World");
}
}
|
|
|
|
|
|
|
EricJ member offline |
|
posts: |
50 |
joined: |
02/22/2007 |
from: |
CA |
|
|
|
|
|
Log4j.properties file |
#
# Define parameters
#
mycompany.log.dir=C:/tmp
mycompany.log.file=slf4j.log
# Root logger option
log4j.rootLogger=INFO, CONSOLE, FILE
#
# console
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.Target=System.err
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}.%M@%L] - %m%n
#
# file
#
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.Threshold=DEBUG
log4j.appender.FILE.File=${mycompany.log.dir}/${mycompany.log.file}
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE.MaxBackupIndex=1
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n
# fine tune each package
log4j.logger.com.mycompany.example=TRACE
|
|
|
|
|
|
|
EricJ member offline |
|
posts: |
50 |
joined: |
02/22/2007 |
from: |
CA |
|
|
|
|
|
How to load log4j.properties file? |
Two basic ways:
1) via system property -Dlog4j.properties=<path_to_file>
2) Default to resource folder <resources>/log4j.properties
|
|
|
|
|
|
|
EricJ member offline |
|
posts: |
50 |
joined: |
02/22/2007 |
from: |
CA |
|
|
|
|
|
Output |
Here come the CONSOLE output:
2013-09-06 19:26:19,235 - ERROR [main:SLF4j.hello@12] - Hello World!
2013-09-06 19:26:19,238 - WARN [main:SLF4j.hello@13] - Hello World!
2013-09-06 19:26:19,238 - INFO [main:SLF4j.hello@14] - Hello World!
and the FILE output:
2013-09-06 19:26:19,235 - ERROR [main:SLF4j@12] - Hello World!
2013-09-06 19:26:19,238 - WARN [main:SLF4j@13] - Hello World!
2013-09-06 19:26:19,238 - INFO [main:SLF4j@14] - Hello World!
2013-09-06 19:26:19,238 - DEBUG [main:SLF4j@14] - Hello World!
|
|
|
|
|
|
|
|