<div><pre class="lang-java prettyprint" style="margin-top:0px;margin-bottom:10px;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;vertical-align:baseline;background-image:initial;overflow-x:auto;overflow-y:auto;width:auto;max-height:600px">
<code style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;vertical-align:baseline;background-image:initial;background-color:rgb(238,238,238)"><span class="pun" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;vertical-align:baseline;background-image:initial;background-color:transparent"><span style="background-color:rgb(255,255,255);white-space:normal"><font face="arial"><div>
Hello,</div><div><br></div><div>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.</div><div><br></div><div>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?</div>
<div><br></div><div>This is my configuration file (logback.xml)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><?xml version="1.0" ?></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><configuration></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span><property name="HOME_PATH" value="/data/logs/myapp/" scope="CONTEXT" /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><property name="MYAPP_LOG_FILE" value="myapp.log" /></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span><property name="MYAPP_ROLLING_TEMPLATE" value="%d{yy-MM-dd}" scope="CONTEXT" /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><property name="MYAPP_OLD_LOG_FILE" value="${MYAPP_LOG_FILE}.%d{yy-MM-dd}" /></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span><property name="DEFAULT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] [%level] %msg%n" scope="CONTEXT" /></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><appender name="myAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span><file>${HOME_PATH}${MYAPP_LOG_FILE}</file></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"></div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span><fileNamePattern>${HOME_PATH}${MYAPP_LOG_FILE}.${MYAPP_ROLLING_TEMPLATE}</fileNamePattern></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span></rollingPolicy></div>
<div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> <pattern>${DEFAULT_PATTERN}</pattern></div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span></encoder></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></appender></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span><logger name="com.myapp" level="DEBUG" additivity="false"></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><appender-ref ref="myAppender" /></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span></logger></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span><root level="OFF"></div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span></root></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></configuration></div><div><br></div><div>and here you can see how I create loggers programmatically (again, I do this only for socket logs).</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static Logger createLogger(String name) {</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>ch.qos.logback.classic.Logger templateLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger("com.myapp");</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>LoggerContext context = templateLogger.getLoggerContext();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> String logDir = context.getProperty("HOME_PATH");</div>
<div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span> PatternLayoutEncoder encoder = new PatternLayoutEncoder();</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>encoder.setPattern(context.getProperty("DEFAULT_PATTERN"));</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>encoder.setContext(context);</div><div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> timeBasedTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>timeBasedTriggeringPolicy.setContext(context);</div><div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>timeBasedRollingPolicy.setContext(context);</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>timeBasedRollingPolicy.setFileNamePattern(logDir + name + ".log." + context.getProperty("MYAPP_ROLLING_TEMPLATE"));</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy);</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy);</div>
<div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setAppend(true);</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setContext(context);</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setEncoder(encoder);</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setFile(logDir + name + ".log");</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setName(name + "Appender");</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setPrudent(false);</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy);</div><div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>timeBasedRollingPolicy.setParent(rollingFileAppender);</div>
<div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>encoder.start();</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>timeBasedRollingPolicy.start();</div>
<div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.stop();</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>rollingFileAppender.start();</div>
<div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger(name);</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>logbackLogger.setLevel(templateLogger.getLevel());</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>logbackLogger.setAdditive(false);</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>logbackLogger.addAppender(rollingFileAppender);</div>
<div><br></div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>return logbackLogger;</div><div> }</div><div><br></div><div>And this is how I reinitialize logback</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private static void initializeLogback() {</div>
<div> File logbackFile = new File(logFilePath);</div><div> System.setProperty("logback.configurationFile", logbackFile.getAbsolutePath());</div><div> StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton();</div>
<div> LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory();</div><div><br></div><div> loggerContext.reset();</div><div> JoranConfigurator configurator = new JoranConfigurator();</div>
<div> configurator.setContext(loggerContext);</div><div> try {</div><div> configurator.doConfigure(logbackFile);</div><div> } catch( JoranException e ) {</div><div> throw new ColumbusRuntimeException(e.getMessage(), e);</div>
<div> }</div><div> }</div><div><br></div><div>Note: I also posted the same question to stackoverflow: </div></font></span></span></code><a href="http://stackoverflow.com/questions/9060545/creating-logback-logger-programmatically">http://stackoverflow.com/questions/9060545/creating-logback-logger-programmatically</a>
</pre><p></p></div>