[logback-user] Basic example of programmatically setting the configuration

David Roussel nabble at diroussel.xsmail.com
Fri Jul 12 17:50:43 CEST 2013


This is great. We need more examples like this of common, and less common, logback configuration approaches. 

David

On 12 Jul 2013, at 15:36, Roger Thomas <roger.thomas at p-mail.eu> wrote:

> I've just started to set up an environment with logback, but found examples for programmatically configuring logback limited, or a mix of solutions for a number of different logger interfaces. As such I hope that the code below helps someone in the future as an example. It may not be the best code on the planet but what it demos is
> 
>          - Select the root logger so that the configuration becomes the default.
>          - Sets up a RollingFileAppender
>          - Sets up a FixedWindowRollingPolicy
>          - Sets up a SizeBasedTriggeringPolicy
>          - Sets up a PatternLayoutEncoder
> 
> To be honest I'm not at the point of fully understanding all the steps so can't provide full docs for the example (yet).
> 
> 
> package org.utils;
> 
> import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
> import ch.qos.logback.classic.spi.ILoggingEvent;
> import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
> import ch.qos.logback.core.rolling.RollingFileAppender;
> import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
> import ch.qos.logback.core.util.StatusPrinter;
> 
> import ch.qos.logback.classic.Logger;
> import ch.qos.logback.classic.LoggerContext;
> 
> import org.slf4j.LoggerFactory;
> 
> public class setupLogging
> {
>    public static void logbackInit()
>    {
>        Logger rootLogger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
> 
>        LoggerContext loggerContext = rootLogger.getLoggerContext();
>        loggerContext.reset();
> 
>        RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<ILoggingEvent>();
>        rfAppender.setContext(loggerContext);
>        rfAppender.setFile("log output.log");
> 
>        FixedWindowRollingPolicy fwRollingPolicy = new FixedWindowRollingPolicy();
>        fwRollingPolicy.setContext(loggerContext);
>        fwRollingPolicy.setFileNamePattern("log output-%i.log.zip");
>        fwRollingPolicy.setParent(rfAppender);
>        fwRollingPolicy.start();
> 
>        SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
>        triggeringPolicy.setMaxFileSize("5MB");
>        triggeringPolicy.start();
> 
>        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
>        encoder.setContext(loggerContext);
>        encoder.setPattern("%-4relative [%thread] %-5level %logger{35} - %msg%n");
>        encoder.start();
> 
>        rfAppender.setEncoder(encoder);
>        rfAppender.setRollingPolicy(fwRollingPolicy);
>        rfAppender.setTriggeringPolicy(triggeringPolicy);
>        rfAppender.start();
> 
>        rootLogger.addAppender(rfAppender);
> 
>        // generate some output
> 
>        StatusPrinter.print(loggerContext);
> 
>        rootLogger.debug("hello tt");
>    }
> }
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user


More information about the Logback-user mailing list