<div class="gmail_quote">On Fri, Jul 6, 2012 at 4:02 AM, Martinus Martinus <span dir="ltr"><<a href="mailto:martinus787@gmail.com" target="_blank">martinus787@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



Hi,<br><br>What is the equivalent programmatical java code for below logback.xml configuration :<br><br><blockquote>
<span style="font-family:"Courier New",Courier,monospace"><configuration></span><br>
<br>
<span style="font-family:"Courier New",Courier,monospace">    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"></span><br>
<span style="font-family:"Courier New",Courier,monospace">        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder </span><br>
<span style="font-family:"Courier New",Courier,monospace">            by default --></span><br>
<span style="font-family:"Courier New",Courier,monospace">        <encoder></span><br>
<span style="font-family:"Courier New",Courier,monospace">            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern></span><br>
<span style="font-family:"Courier New",Courier,monospace">        </encoder></span><br>
<span style="font-family:"Courier New",Courier,monospace">    </appender></span><br>
<br>
<span style="font-family:"Courier New",Courier,monospace">    <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender"></span><br>
<b><span style="font-family:"Courier New",Courier,monospace">        <syslogHost>myhost</syslogHost></span></b><br>
<b><span style="font-family:"Courier New",Courier,monospace">        <facility>USER</facility></span></b><br>
<span style="font-family:"Courier New",Courier,monospace"><b>        <suffixPattern>[%thread] %logger </b>%msg</suffixPattern></span><br>
<span style="font-family:"Courier New",Courier,monospace">    </appender></span><br>
<br>
<span style="font-family:"Courier New",Courier,monospace">    <root level="debug"></span><br>
<span style="font-family:"Courier New",Courier,monospace">        <appender-ref ref="SYSLOG"/></span><br>
<span style="font-family:"Courier New",Courier,monospace">        <appender-ref ref="STDOUT"/></span><br>
<span style="font-family:"Courier New",Courier,monospace">    </root></span><br>
<span style="font-family:"Courier New",Courier,monospace"></configuration></span> </blockquote>Thanks. </blockquote><div><br></div><div>Hi Martinus,</div><div><br></div><div>This doesn't directly answer your question, but it provides a solution if your goal is to configure logback from code instead of logback.xml... You can load the configuration XML into a string and feed that to a JoranConfigurator, as in the example below. </div>

<div><br></div><div>Btw, please refrain from posting the same question repeatedly. :)</div><div><br></div><div>Hope that helps,</div><div>Tony</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre">      </span><font face="courier new, monospace">static private final String LOGBACK_XML =</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"<configuration>" +</font></div><div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">         </span>"                                                                                               " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"    <appender name='STDOUT' class='ch.qos.logback.core.ConsoleAppender'>                       " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder" +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"            by default -->                                                                     " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        <encoder>                                                                              " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>            " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        </encoder>                                                                             " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"    </appender>                                                                                " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"                                                                                               " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"    <appender name='SYSLOG' class='ch.qos.logback.classic.net.SyslogAppender'>                 " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        <syslogHost>myhost</syslogHost>                                                        " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        <facility>USER</facility>                                                              " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        <suffixPattern>[%thread] %logger %msg</suffixPattern>                                  " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"    </appender>                                                                                " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"                                                                                               " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"    <root level='debug'>                                                                       " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        <appender-ref ref='SYSLOG'/>                                                           " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"        <appender-ref ref='STDOUT'/>                                                           " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"    </root>                                                                                    " +</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">          </span>"</configuration>                                                                               " </font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>;</font></div><div><span class="Apple-tab-span" style="white-space:pre"><font face="courier new, monospace">   </font></span></div>

<div><font face="courier new, monospace">    static private void configureLogback() {</font></div><div><font face="courier new, monospace">        BufferedInputStream xmlStream = new BufferedInputStream(new ByteArrayInputStream(LOGBACK_XML.getBytes()));</font></div>

<div><font face="courier new, monospace">        </font></div><div><font face="courier new, monospace">        LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();</font></div><div><font face="courier new, monospace">        context.reset(); // override default config</font></div>

<div><font face="courier new, monospace">        JoranConfigurator joran = new JoranConfigurator();</font></div><div><font face="courier new, monospace">        joran.setContext(context);</font></div><div><font face="courier new, monospace">        try {</font></div>

<div><font face="courier new, monospace">            joran.doConfigure(xmlStream);</font></div><div><font face="courier new, monospace">            StatusPrinter.printInCaseOfErrorsOrWarnings(context);</font></div><div><font face="courier new, monospace">        } catch (JoranException e) {</font></div>

<div><span style="font-family:'courier new',monospace">            e.printStackTrace();</span></div><div><font face="courier new, monospace">        }</font></div><div><font face="courier new, monospace">    }</font></div>

</div><div><br></div></div>