[logback-user] Java programmatic configurations
Martinus Martinus
martinus787 at gmail.com
Mon Jul 9 04:11:10 CEST 2012
Hi Tony,
Thanks for your answer. Is there a way to make my application log to be
save in separate syslog file? Like with name MyApps. Sorry from posting the
same question repeatedly, because I just join this group and I thought my
first email didn't reach this group mail.
Thanks.
On Fri, Jul 6, 2012 at 7:20 PM, Tony Trinh <tony19 at gmail.com> wrote:
> 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();
> }
> }
>
>
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20120709/67ea578f/attachment.html>
More information about the Logback-user
mailing list