[logback-user] Using logback simply

Sebastien Pennec sebastien at qos.ch
Wed Jan 24 11:25:28 CET 2007


Hello shef,

It seems to me that the first two chapters should help give a simple but precise idea 
of how to use logback.

The Example 1.1 of the introduction[1] shows how the classes that use logback should 
retrieve a logger and use it in a basic way.
Basically, you only do:

Logger logger = LoggerFactory.getLogger("chapter1.HelloWorld1");
logger.debug("Hello world.");

The chapter 2[2] explains how to use the logger methods, understand the level 
inheritance and the way printing methods works, so that you can use logback in a more 
efficient way.

However, you are right about the lack of code examples on how to create logback 
components. You'll find the code you need to create a RollingFileAppender at the end 
of this email.
This said, we tend to prefer the use of Joran and xml configuration files to 
configure logback. Note that the file can be loaded by your class, if you wish to be 
free to place it wherever you want.

About the jars, our homepage[3] explains what they contain:

The logback-core module lays the groundwork for the other two modules.

The logback-classic module can be assimilated to a significantly improved version of 
log4j. Moreover, logback-classic natively implements the SLF4J API so that you can 
readily switch back and forth between logback and other logging systems such as log4j 
or JDK14 Logging.

The Access module integrates with Servlet containers to provide HTTP-access log 
functionality. Note that you can easily build your own modules on top of the Core module.

The Access and Classic modules are used in rather different situations thus the two 
jars, so that you don't have to carry HTTP-related logging components with you if you 
don't need them. The Core jar contains many components that are generic and used 
commonly by the two other modules. As of SLF4J, it is a different project, that 
provides an easy facade so that your application's classes are not tied to a specific 
logging implementation. Since logback is a direct implementation of the SLF4J, the 
SLF4J api is required to run logback classic. However, logback access is not tied to 
SLF4J so if you want to do http-access logging, you won't need the SLF4J api jar.

Here is some code that you might want to add to your configuration classes. It 
creates a RollingFileAppender, with a TimeBasedRollingPolicy that will rollover the 
file every day. There is also a PatternLayout to configure the formatting of the 
logging events.

Hope this helps :)

Sébastien

//////////////

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.shutdownAndReset();
RollingFileAppender appender = new RollingFileAppender();
appender.setContext(lc);
appender.setFile("/tmp/filename.txt");
appender.setName("rfa");
TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
policy.setContext(lc);
policy.setParent(appender);
policy.setFileNamePattern("filename_%d{yyyy-MM-dd}.txt");
PatternLayout layout = new PatternLayout();
layout.setContext(lc);
layout.setPattern("%date [%level] %logger{30} %message %n");

appender.setRollingPolicy(policy);
appender.setLayout(layout);
policy.start();
layout.start();
appender.start();

Logger root = lc.getLogger(LoggerContext.ROOT_NAME);
root.addAppender(appender);

//start logging:
Logger logger = (Logger)LoggerFactory.getLogger(MyApp.class);
logger.debug("Logging configuration done");
//////////////

Links:
[1]http://logback.qos.ch/manual/introduction.html
[2]http://logback.qos.ch/manual/architecture.html
[3]http://logback.qos.ch/index.html


shef wrote:
> Hello,
> 
> I've read the docs, and I'm having a hard time understanding how to perform basic tasks. The manual, is, ah, less than straightforward...
> 
> All I want to do is log to a disk file, and configure this in code. Using XML config files is not an option. I see no sample code in the docs that shows how to do this. The manual gets lost in complexity very, very quickly.
> 
> All I want is something like this:
> 
> someClass.setType(...) // console, disk file, whatever
> someClass.setDestination("/mydir");
> someClass.setRolloverPolicy("some_policy_here");
> 
> Also -- where does it explain what is contained in logback-access.jar, logback-classic.jar, and logback-core.jar? Do I need all three, plus the slf4j-apis? Why not just one jar file?
> 
> 
> 
> 
> 
>  
> ____________________________________________________________________________________
> 8:00? 8:25? 8:40? Find a flick in no time 
> with the Yahoo! Search movie showtime shortcut.
> http://tools.search.yahoo.com/shortcuts/#news
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user
> 

-- 
Sébastien Pennec
sebastien at qos.ch

Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch/



More information about the Logback-user mailing list