[logback-user] Java programmatic configurations

Tony Trinh tony19 at gmail.com
Fri Jul 6 13:20:40 CEST 2012


On Fri, Jul 6, 2012 at 4:02 AM, Martinus Martinus <martinus787 at gmail.com>wrote:

> Hi,
>
> What is the equivalent programmatical java code for below logback.xml
> configuration :
>
> <configuration>
>
>     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
>         <!-- encoders are assigned the type
> ch.qos.logback.classic.encoder.PatternLayoutEncoder
>             by default -->
>         <encoder>
>             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger -
> %msg%n</pattern>
>         </encoder>
>     </appender>
>
>     <appender name="SYSLOG"
> class="ch.qos.logback.classic.net.SyslogAppender">
> *        <syslogHost>myhost</syslogHost>*
> *        <facility>USER</facility>*
> *        <suffixPattern>[%thread] %logger *%msg</suffixPattern>
>     </appender>
>
>     <root level="debug">
>         <appender-ref ref="SYSLOG"/>
>         <appender-ref ref="STDOUT"/>
>     </root>
> </configuration>
>
> Thanks.


Hi Martinus,

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.

Btw, please refrain from posting the same question repeatedly. :)

Hope that helps,
Tony

static private final String LOGBACK_XML =
 "<configuration>" +
"
                    " +
 "    <appender name='STDOUT' class='ch.qos.logback.core.ConsoleAppender'>
                      " +
 "        <!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder" +
 "            by default -->
                      " +
 "        <encoder>
                     " +
 "            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger -
%msg%n</pattern>            " +
 "        </encoder>
                      " +
 "    </appender>
                     " +
 "
                      " +
 "    <appender name='SYSLOG'
class='ch.qos.logback.classic.net.SyslogAppender'>                 " +
 "        <syslogHost>myhost</syslogHost>
                     " +
 "        <facility>USER</facility>
                     " +
 "        <suffixPattern>[%thread] %logger %msg</suffixPattern>
                     " +
 "    </appender>
                     " +
 "
                      " +
 "    <root level='debug'>
                      " +
 "        <appender-ref ref='SYSLOG'/>
                      " +
 "        <appender-ref ref='STDOUT'/>
                      " +
 "    </root>
                     " +
 "</configuration>
                      "
 ;
     static private void configureLogback() {
        BufferedInputStream xmlStream = new BufferedInputStream(new
ByteArrayInputStream(LOGBACK_XML.getBytes()));

        LoggerContext context =
(LoggerContext)LoggerFactory.getILoggerFactory();
        context.reset(); // override default config
        JoranConfigurator joran = new JoranConfigurator();
        joran.setContext(context);
        try {
            joran.doConfigure(xmlStream);
            StatusPrinter.printInCaseOfErrorsOrWarnings(context);
        } catch (JoranException e) {
            e.printStackTrace();
        }
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20120706/28a5c7f1/attachment.html>


More information about the Logback-user mailing list