Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating logback logger programmatically

Tags:

java

logback

I have a problem regarding to logback project. My requirement is I have to create log properties dynamically. Let me explain this by an example.

My project creates socket communication with external system and it may have multiple sockets. For each socket, I want to have different log files which will contain the messages that is read and sent. To accomplish this, I create the logger for sockets programmatically. Problem is when I want to reconfigure loggers based on logback.xml (by adding scan="true" or by reinitializing the logback), the loggers I created becomes unusable. How can I fixed that or can you advise me another solution?

This is my configuration file (logback.xml)

<?xml version="1.0" ?>
<configuration>
    <property name="HOME_PATH" value="/data/logs/myapp/" scope="CONTEXT" />
    <property name="MYAPP_LOG_FILE" value="myapp.log" />
    <property name="MYAPP_ROLLING_TEMPLATE" value="%d{yy-MM-dd}" scope="CONTEXT" />
    <property name="MYAPP_OLD_LOG_FILE" value="${MYAPP_LOG_FILE}.%d{yy-MM-dd}" />
    <property name="DEFAULT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] [%level] %msg%n" scope="CONTEXT" />

    <appender name="myAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${HOME_PATH}${MYAPP_LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${HOME_PATH}${MYAPP_LOG_FILE}.${MYAPP_ROLLING_TEMPLATE}</fileNamePattern>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${DEFAULT_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="com.myapp" level="DEBUG" additivity="false">
        <appender-ref ref="myAppender" />
    </logger>

    <root level="OFF">
    </root>
</configuration>

and here you can see how I create loggers programmatically (again, I do this only for socket logs).

public static Logger createLogger(String name) {
        ch.qos.logback.classic.Logger templateLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger("com.myapp");
        LoggerContext context = templateLogger.getLoggerContext();

        String logDir = context.getProperty("HOME_PATH");

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setPattern(context.getProperty("DEFAULT_PATTERN"));
        encoder.setContext(context);

        DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> timeBasedTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
        timeBasedTriggeringPolicy.setContext(context);

        TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
        timeBasedRollingPolicy.setContext(context);
        timeBasedRollingPolicy.setFileNamePattern(logDir + name + ".log." + context.getProperty("MYAPP_ROLLING_TEMPLATE"));
        timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy);
        timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy);

        RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
        rollingFileAppender.setAppend(true);
        rollingFileAppender.setContext(context);
        rollingFileAppender.setEncoder(encoder);
        rollingFileAppender.setFile(logDir + name + ".log");
        rollingFileAppender.setName(name + "Appender");
        rollingFileAppender.setPrudent(false);
        rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);
        rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy);

        timeBasedRollingPolicy.setParent(rollingFileAppender);

        encoder.start();
        timeBasedRollingPolicy.start();

        rollingFileAppender.stop();
        rollingFileAppender.start();

        ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger(name);
        logbackLogger.setLevel(templateLogger.getLevel());
        logbackLogger.setAdditive(false);
        logbackLogger.addAppender(rollingFileAppender);

        return logbackLogger;
}

And this is how I reinitialize logback

private static void initializeLogback() {
    File logbackFile = new File(logFilePath);
    System.setProperty("logback.configurationFile", logbackFile.getAbsolutePath());
    StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton();
    LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory();

    loggerContext.reset();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(loggerContext);
    try {
        configurator.doConfigure(logbackFile);
    } catch( JoranException e ) {
        throw new ColumbusRuntimeException(e.getMessage(), e);
    }
}
like image 287
Yusuf Soysal Avatar asked Jan 30 '12 07:01

Yusuf Soysal


Video Answer


1 Answers

Looks like you need the SiftingAppender where your discriminator would be the socket id itself, or any combined variation. I dunno what threading issues you'll run into with this (when MDC is read, etc), but this should be a good starting point and seems similar to your case.

like image 177
Spencer Kormos Avatar answered Sep 17 '22 17:09

Spencer Kormos