[logback-user] Using logback simply

Sebastien Pennec sebastien at qos.ch
Wed Jan 24 17:56:55 CET 2007


Hello,

At the moment, logback does variable substitutions in its configuration file. It is 
possible to declare a variable, either in the config file or in a separate file and 
use it later on during the configuration process.

Would it be of any use to you if logback offered to substitute variables whose values 
are stored in JNDI ?

Cheers,

Sébastien

f_du_preez at limehouse.co.za wrote:
> Hi Sebastien
> 
> Nice explanation. 
> 
> I would like to comment on "we tend to prefer the use of Joran and xml
> configuration files to configure logback". I can understand this to a degree,
> but programmatic configuration can be very important, and I think it should
> form part of the official documentation.
> 
> A good example: when we write our applications, this is done on our development
> machines, which then goes to a test, pre-prod, and finally production
> environments. In order for us to prevent code / property file changes between
> these environments, specifically from our dev machines to one of the three
> offical environments, we store system specific values, such as logging
> locations, logging levels etc in JNDI. This enables us to use exactly the same
> code base for all environments - which is actually a self-enforced requirement
> of our company, and understandably so.
> 
> Kind regards
> Fouche du Preez
> 
> 
> Quoting Sebastien Pennec <sebastien at qos.ch>:
> 
>> 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/
>> _______________________________________________
>> Logback-user mailing list
>> Logback-user at qos.ch
>> http://qos.ch/mailman/listinfo/logback-user
>>
> 
> 
> 
> _______________________________________________
> 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