[prev in list] [next in list] [prev in thread] [next in thread] 

List:       logback-user
Subject:    [logback-user] Creating logback logger programmatically
From:       Yusuf Soysal <yusuf.soysal () gmail ! com>
Date:       2012-02-22 11:40:13
Message-ID: CAB0-dMHfirUwaccOPJeY27KZM=P15h6WKz9K7tZ3fcyiuzpX0Q () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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

[Attachment #5 (text/html)]

Hello everyone,<div><br></div><div>I&#39;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=&quot;true&quot; 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>&lt;?xml version=&quot;1.0&quot; \
?&gt;</div><div><span \
style="white-space:pre-wrap">	</span>&lt;configuration&gt;</div>

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

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

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


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


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

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

<div><span style="white-space:pre-wrap">			</span>&lt;appender-ref \
ref=&quot;myAppender&quot; /&gt;</div><div><span \
style="white-space:pre-wrap">		</span>&lt;/logger&gt;</div><div><span \
style="white-space:pre-wrap">	</span>    <span \
style="white-space:pre-wrap">	</span>&lt;root level=&quot;OFF&quot;&gt;</div>

<div>    <span style="white-space:pre-wrap">	</span>&lt;/root&gt;</div><div><span \
style="white-space:pre-wrap">	</span>&lt;/configuration&gt;</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(&quot;com.myapp&quot;);</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(&quot;HOME_PATH&quot;);</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(&quot;DEFAULT_PATTERN&quot;));</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&lt;ILoggingEvent&gt;</div><div>timeBasedTriggeringPolicy \
= new</div><div>DefaultTimeBasedFileNamingAndTriggeringPolicy&lt;ILoggingEvent&gt;();</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&lt;ILoggingEvent&gt; \
timeBasedRollingPolicy</div>

<div>= new TimeBasedRollingPolicy&lt;ILoggingEvent&gt;();</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>&quot;.log.&quot; + \
context.getProperty(&quot;MYAPP_ROLLING_TEMPLATE&quot;));</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&lt;ILoggingEvent&gt; \
rollingFileAppender = new</div>

<div>RollingFileAppender&lt;ILoggingEvent&gt;();</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 \
+ &quot;.log&quot;);</div>

<div>        <span style="white-space:pre-wrap">	</span>rollingFileAppender.setName(name \
+ &quot;Appender&quot;);</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(&quot;logback.configurationFile&quot;,</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>



_______________________________________________
Logback-user mailing list
Logback-user@qos.ch
http://mailman.qos.ch/mailman/listinfo/logback-user

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic