Hello everyone,<div><br></div><div>I'm sorry to post this message again, but I have no clue to continue. Is it possible to solve this issue in anyway?</div><div><br></div><div>By the way, I was able to accomplish this with log4j :(</div>

<div><br></div><div>Regards</div><div><br></div><div>----------------------</div><div><br></div><div><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 style="white-space:pre-wrap">      </span><?xml version="1.0" ?></div><div><span style="white-space:pre-wrap">   </span><configuration></div>

<div><span style="white-space:pre-wrap">          </span><property name="HOME_PATH" value="/data/logs/myapp/" scope="CONTEXT" /></div><div><span style="white-space:pre-wrap">              </span><property name="MYAPP_LOG_FILE" value="myapp.log" /></div>

<div><span style="white-space:pre-wrap">          </span><property name="MYAPP_ROLLING_TEMPLATE" value="%d{yy-MM-dd}"</div><div>scope="CONTEXT" /></div><div><span style="white-space:pre-wrap">                </span><property name="MYAPP_OLD_LOG_FILE" value="${MYAPP_LOG_FILE}.%d{yy-MM-dd}" /></div>

<div><span style="white-space:pre-wrap">          </span><property name="DEFAULT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS}</div><div>[%file:%line] [%level] %msg%n" scope="CONTEXT" /></div>
<div><span style="white-space:pre-wrap">                  </span><appender name="myAppender"</div><div>class="ch.qos.logback.core.rolling.RollingFileAppender"></div><div><span style="white-space:pre-wrap">                     </span><file>${HOME_PATH}${MYAPP_LOG_FILE}</file></div>

<div><span style="white-space:pre-wrap">                  </span><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"></div><div>            <span style="white-space:pre-wrap">   </span><fileNamePattern>${HOME_PATH}${MYAPP_LOG_FILE}.${MYAPP_ROLLING_TEMPLATE}</fileNamePattern></div>

<div>        <span style="white-space:pre-wrap">  </span></rollingPolicy></div><div><br></div><div>        <span style="white-space:pre-wrap">  </span><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"></div>

<div><span style="white-space:pre-wrap">  </span>            <pattern>${DEFAULT_PATTERN}</pattern></div><div>        <span style="white-space:pre-wrap">    </span></encoder></div>
<div><span style="white-space:pre-wrap">          </span></appender></div><div><span style="white-space:pre-wrap">                        </span><logger name="com.myapp" level="DEBUG" additivity="false"></div>

<div><span style="white-space:pre-wrap">                  </span><appender-ref ref="myAppender" /></div><div><span style="white-space:pre-wrap">                </span></logger></div><div><span style="white-space:pre-wrap">  </span>    <span style="white-space:pre-wrap">    </span><root level="OFF"></div>

<div>    <span style="white-space:pre-wrap">      </span></root></div><div><span style="white-space:pre-wrap">    </span></configuration></div><div><br></div><div>and here you can see how I create loggers programmatically (again, I</div>

<div>do this only for socket logs).</div><div><br></div><div><span style="white-space:pre-wrap">    </span>public static Logger createLogger(String name) {</div><div>        <span style="white-space:pre-wrap"> </span>ch.qos.logback.classic.Logger templateLogger =</div>

<div>(ch.qos.logback.classic.Logger) LogUtil.getLogger("com.myapp");</div><div>        <span style="white-space:pre-wrap">  </span>LoggerContext context = templateLogger.getLoggerContext();</div>
<div><br></div><div><span style="white-space:pre-wrap"> </span>        String logDir = context.getProperty("HOME_PATH");</div><div><br></div><div>    <span style="white-space:pre-wrap"> </span>    PatternLayoutEncoder encoder = new PatternLayoutEncoder();</div>

<div>        <span style="white-space:pre-wrap">  </span>encoder.setPattern(context.getProperty("DEFAULT_PATTERN"));</div><div>        <span style="white-space:pre-wrap">    </span>encoder.setContext(context);</div>

<div><br></div><div>        <span style="white-space:pre-wrap"> </span>DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent></div><div>timeBasedTriggeringPolicy = new</div><div>DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();</div>

<div>        <span style="white-space:pre-wrap">  </span>timeBasedTriggeringPolicy.setContext(context);</div><div><br></div><div>        <span style="white-space:pre-wrap">  </span>TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy</div>

<div>= new TimeBasedRollingPolicy<ILoggingEvent>();</div><div>        <span style="white-space:pre-wrap">       </span>timeBasedRollingPolicy.setContext(context);</div><div>        <span style="white-space:pre-wrap">      </span>timeBasedRollingPolicy.setFileNamePattern(logDir + name +</div>

<div>".log." + context.getProperty("MYAPP_ROLLING_TEMPLATE"));</div><div>        <span style="white-space:pre-wrap">      </span>timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy);</div>

<div>        <span style="white-space:pre-wrap">  </span>timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy);</div><div><br></div><div>        <span style="white-space:pre-wrap">    </span>RollingFileAppender<ILoggingEvent> rollingFileAppender = new</div>

<div>RollingFileAppender<ILoggingEvent>();</div><div>        <span style="white-space:pre-wrap">        </span>rollingFileAppender.setAppend(true);</div><div>        <span style="white-space:pre-wrap">     </span>rollingFileAppender.setContext(context);</div>

<div>        <span style="white-space:pre-wrap">  </span>rollingFileAppender.setEncoder(encoder);</div><div>        <span style="white-space:pre-wrap"> </span>rollingFileAppender.setFile(logDir + name + ".log");</div>

<div>        <span style="white-space:pre-wrap">  </span>rollingFileAppender.setName(name + "Appender");</div><div>        <span style="white-space:pre-wrap">        </span>rollingFileAppender.setPrudent(false);</div>
<div>        <span style="white-space:pre-wrap">  </span>rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);</div><div>        <span style="white-space:pre-wrap">    </span>rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy);</div>

<div><br></div><div>        <span style="white-space:pre-wrap"> </span>timeBasedRollingPolicy.setParent(rollingFileAppender);</div><div><br></div><div>        <span style="white-space:pre-wrap">  </span>encoder.start();</div>

<div>        <span style="white-space:pre-wrap">  </span>timeBasedRollingPolicy.start();</div><div><br></div><div>        <span style="white-space:pre-wrap"> </span>rollingFileAppender.stop();</div>
<div>        <span style="white-space:pre-wrap">  </span>rollingFileAppender.start();</div><div><br></div><div>        <span style="white-space:pre-wrap">    </span>ch.qos.logback.classic.Logger logbackLogger =</div>
<div>(ch.qos.logback.classic.Logger) LogUtil.getLogger(name);</div><div>        <span style="white-space:pre-wrap">   </span>logbackLogger.setLevel(templateLogger.getLevel());</div><div>        <span style="white-space:pre-wrap">       </span>logbackLogger.setAdditive(false);</div>

<div>        <span style="white-space:pre-wrap">  </span>logbackLogger.addAppender(rollingFileAppender);</div><div><br></div><div>        <span style="white-space:pre-wrap"> </span>return logbackLogger;</div>
<div>    }</div><div><br></div><div>And this is how I reinitialize logback</div><div><br></div><div><span style="white-space:pre-wrap">       </span>private static void initializeLogback() {</div><div>        File logbackFile = new File(logFilePath);</div>

<div>        System.setProperty("logback.configurationFile",</div><div>logbackFile.getAbsolutePath());</div><div>        StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton();</div><div>        LoggerContext loggerContext = (LoggerContext)</div>

<div>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><div><a href="http://stackoverflow.com/questions/9060545/creating-logback-logger-programmatically" target="_blank">http://stackoverflow.com/questions/9060545/creating-logback-logger-programmatically</a></div>

</div>