[logback-user] Creating logback logger programmatically

Yusuf Soysal yusuf.soysal at gmail.com
Wed Feb 22 12:40:13 CET 2012


Hello everyone,

I'm sorry to post this message again, but I have no clue to continue. Is it
possible to solve this issue in anyway?

By the way, I was able to accomplish this with log4j :(

Regards

----------------------

Hello,

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);
        }
    }

Note: I also posted the same question to stackoverflow:
http://stackoverflow.com/questions/9060545/creating-logback-logger-programmatically
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20120222/c21a18cd/attachment.html>


More information about the Logback-user mailing list