[logback-user] Programmmatically creatinglogger/appender/pattern
Ceki Gulcu
ceki at qos.ch
Thu Mar 26 13:28:55 CET 2009
You need to invoke the start() method on most if not all logback components for
them to function property.
Lance White wrote:
> OK, I still can't get this working, and having looked at the junit src, they're all individual tests and I couldn't find any integrated tests in there.
>
> This is the simple test I'm trying to use. If I use the LoggerContext from the ILoggerFactory, then I get a log message to the console (using the standard stuff I presume) and no file is created. If I create the LoggerContext then no output is produced anywhere.
>
> Java is 1.5.0_16.
>
> Cheers
>
> Lance
>
> (p.s. I've got this working with log4j, but I'm trying to upgrade)
>
> ---------------------------------------------------------------------------------
>
> import org.slf4j.LoggerFactory;
>
> import ch.qos.logback.classic.Logger;
> import ch.qos.logback.classic.LoggerContext;
> import ch.qos.logback.classic.PatternLayout;
> import ch.qos.logback.classic.spi.LoggingEvent;
> import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
> import ch.qos.logback.core.rolling.RollingFileAppender;
> import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
>
> public class Test2
> {
> public Test2() { }
>
> public void run(String name) {
> LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
> // LoggerContext lc = new LoggerContext();
> Logger logger = lc.getLogger(name);
>
> PatternLayout layout = new PatternLayout();
> layout.setPattern("%d:%msg%n");
>
> RollingFileAppender<LoggingEvent> rfa = new RollingFileAppender<LoggingEvent>();
> rfa.setContext(lc);
> rfa.setName(name + "_Appender");
> rfa.setLayout(layout);
> rfa.setFile("c:\\" + name + ".log");
>
> SizeBasedTriggeringPolicy<LoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<LoggingEvent>();
> triggeringPolicy.setContext(lc);
> triggeringPolicy.setMaxFileSize(String.valueOf(50000));
> triggeringPolicy.start();
> rfa.setTriggeringPolicy(triggeringPolicy);
>
> FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
> rollingPolicy.setContext(lc);
> rollingPolicy.setFileNamePattern("c:\\" + name + "-%i.log");
> rollingPolicy.setMinIndex(1);
> rollingPolicy.setMaxIndex(10);
> rollingPolicy.setParent(rfa);
> rollingPolicy.start();
> rfa.setRollingPolicy(rollingPolicy);
>
>
> logger.info("hello");
> }
>
>
> public static void main(String[] args)
> {
> Test2 t = new Test2();
> t.run("fred");
> }
> }
> ---------------------------------------------------------------------------------
>
> -----Original Message-----
> From: logback-user-bounces at qos.ch [mailto:logback-user-bounces at qos.ch] On Behalf Of Lance White
> Sent: 25 March 2009 16:09
> To: logback users list
> Subject: Re: [logback-user] Programmmatically creatinglogger/appender/pattern
>
> Cheers Ceki, I'll go through those.
>
> Lance
>
> -----Original Message-----
> From: logback-user-bounces at qos.ch [mailto:logback-user-bounces at qos.ch] On Behalf Of Ceki Gulcu
> Sent: 25 March 2009 16:02
> To: logback users list
> Subject: Re: [logback-user] Programmmatically creatinglogger/appender/pattern
>
> Hello Lance,
>
> Look into the junit test cases which do that type of operation all the time. For
> example, see:
>
> http://logback.qos.ch/xref-test/ch/qos/logback/classic/html/HTMLLayoutTest.html
> http://logback.qos.ch/xref-test/ch/qos/logback/core/rolling/SizeBasedRollingTest.html
>
> If that does not help, post your code here.
>
> Lance White wrote:
>> Has anyone got any examples on how to programmatically create a logger /
>> appender / pattern et. al in-line in code?
>>
>> I can't for the life of me get this running. Currently I've got the
>> appender (file appender in this case) creating the output files, but
>> all I get in them is stuff like:
>>
>> %PARSER_ERROR_d:MODULE:%PARSER_ERROR_msg%
>>
>> Which looks like the pattern stuff isn't started up properly.
>>
>> Cheers
>>
>> Lance
>
--
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
More information about the Logback-user
mailing list