[logback-user] HELP - RollingFileAppender not logging when created entirely in code

Ceki Gülcü ceki at qos.ch
Wed Sep 15 07:26:43 CEST 2010


Hi Bob,

It looks like you forgot to invoke start() on the rolling policy instance.

HTH,


On 15/09/2010 3:32 AM, Bob DeRemer wrote:
> Hi Logback community,
>
> I hope the problem is operator error, but I can’t seem to figure this
> out. My java webapp requirements are such that we need to
> programmatically create our logback logging components (i.e. no use of
> an XML configuration file at all). This means I am resetting the
> LoggerContext and programmatically creating a single PatternLayout along
> with a ConsoleAppender and 2 specific RollingFileAppenders – 1 for
> general application logging and 1 dedicated to security-related stuff.
> If I create these as FileAppenders, they work fine, but as soon as I try
> and create RollingFileAppenders, the files get created, but nothing is
> logged out.
>
> I’ve provided the code I’m using below. If you look at the
> createRollingFileAppender method, you will see that I’m creating the
> TimeBasedRollingPolicy as well. I believe it has something to do with
> how I’m building up the RollingFileAppender and the Policy.
>
> Any suggestions would be greatly appreciated!
>
> Thanks,
>
> *Bob DeRemer*
>
> *Senior Director, Architecture and Development*
>
> http://www.burningskysoftware.com <http://www.burningskysoftware.com/>
>
> O: 717.505.7923
>
> M: 717.881.3986
>
> =====================================================================================================================
>
> public void initializeLoggerContext() throws IOException
>
> {
>
> // clear logging context
>
> LoggerContext loggerContext = (LoggerContext)
> LoggerFactory.getILoggerFactory();
>
> loggerContext.reset();
>
> // create standard layout pattern to be used by all appenders
>
> PatternLayout layout = new PatternLayout();
>
> layout.setContext(loggerContext);
>
> layout.setPattern(LAYOUT_PATTERN);
>
> layout.start();
>
> // create console appender
>
> ConsoleAppender<ILoggingEvent> consoleAppender = new
> ConsoleAppender<ILoggingEvent>();
>
> consoleAppender.setContext(loggerContext);
>
> consoleAppender.setLayout(layout);
>
> consoleAppender.start();
>
> // create application appender
>
> FileAppender<ILoggingEvent> applicationAppender =
> this.createFileAppender(loggerContext, layout, APPLICATION_LOG);
>
> applicationAppender.start();
>
> // create security appender
>
> RollingFileAppender<ILoggingEvent> securityAppender =
> this.createRollingFileAppender(loggerContext, layout, SECURITY_LOG);
>
> securityAppender.start();
>
> // create configuration appender
>
> RollingFileAppender<ILoggingEvent> configurationAppender =
> this.createRollingFileAppender(loggerContext, layout, CONFIGURATION_LOG);
>
> configurationAppender.start();
>
> // configure top-level ROOT logger
>
> Logger rootLogger =
> (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
>
> rootLogger.addAppender(consoleAppender);
>
> rootLogger.addAppender(applicationAppender);
>
> // configure special security logger
>
> Logger securityLogger = (Logger)LoggerFactory.getLogger(SECURITY_LOG);
>
> securityLogger.setAdditive(false);
>
> securityLogger.setLevel(Level.DEBUG);
>
> securityLogger.addAppender(securityAppender);
>
> // configure special configuration logger
>
> Logger configurationLogger =
> (Logger)LoggerFactory.getLogger(CONFIGURATION_LOG);
>
> configurationLogger.setAdditive(false);
>
> configurationLogger.setLevel(Level.DEBUG);
>
> configurationLogger.addAppender(configurationAppender);
>
> }
>
> private FileAppender<ILoggingEvent> createFileAppender(LoggerContext
> loggerContext, PatternLayout layout, String logName) throws IOException
>
> {
>
> FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
>
> // get logs directory from Application Context
>
> ApplicationContext appContext = ApplicationContext.getInstance();
>
> String logsRoot = appContext.getLogsDirectory();
>
> String logFile = this.getLogFilePath(logsRoot, logName);
>
> // create appender
>
> appender.setContext(loggerContext);
>
> appender.setName(logName);
>
> appender.setFile(logFile);
>
> appender.setAppend(true);
>
> appender.setLayout(layout);
>
> return appender;
>
> }
>
> private RollingFileAppender<ILoggingEvent>
> createRollingFileAppender(LoggerContext loggerContext, PatternLayout
> layout, String logName) throws IOException
>
> {
>
> // get logs directory from Application Context
>
> ApplicationContext appContext = ApplicationContext.getInstance();
>
> String logsRoot = appContext.getLogsDirectory();
>
> String logFile = this.getLogFilePath(logsRoot, logName);
>
> // create appender
>
> RollingFileAppender<ILoggingEvent> appender = new
> RollingFileAppender<ILoggingEvent>();
>
> appender.setContext(loggerContext);
>
> appender.setName(logName);
>
> appender.setFile(logFile);
>
> appender.setAppend(true);
>
> appender.setLayout(layout);
>
> // create rollover policy
>
> TimeBasedRollingPolicy<ILoggingEvent> policy = new
> TimeBasedRollingPolicy<ILoggingEvent>();
>
> policy.setContext(loggerContext);
>
> policy.setFileNamePattern(this.getArchiveFileNamePattern(logsRoot,
> logName));
>
> // MUST set parent - linking appender to policy, or you get a NULL ptr
> exception when starting appender
>
> policy.setParent(appender);
>
> // associate policy with appender
>
> appender.setRollingPolicy(policy);
>
> return appender;
>
> }
>



More information about the Logback-user mailing list