Yes, you can - the whole thing is very much programmatically accessible.<br><br>But, if the one thing you want is to specify the filename to parse, I found that the better way was to do as follows:<br><br> System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, <filename>);<br>
<br>Do that <b>before</b> you access any loggers or logging context, as that triggers the configuration. After that, I personally do something along the lines of..:<br><br> // Getting the LoggerContext, which will trigger the default log config, using the CONFIG_FILE_PROPERTY<br>
LoggerContext lc;<br> try {<br> lc = (LoggerContext) LoggerFactory.getILoggerFactory();<br> }<br> catch (ClassCastException cce) {<br> throw new CannotStartupException("Apparently SLF4J is not backed by Logback."<br>
+ " This is a requirement, thus an internal fault.", cce);<br> }<br><br><br> // Set the name of the LoggerContext<br> lc.setName("SomeName");<br><br> // Fetch this class's Logger<br>
log = LoggerFactory.getLogger(LogSetupClass.class);<br><br> // ?: Check if we have warnings or errors in configuration.<br> StatusChecker sc = new StatusChecker(lc);<br> if (sc.getHighestLevel(0) >= ErrorStatus.WARN) {<br>
// -> Yes, there are warnings or errors<br> // Whine hard about this.<br> System.out.println();<br> String msg = "****WARNING****: THERE ARE WARNINGS OR ERRORS IN THE LOGGING CONFIGURATION, READ STATUS BELOW CAREFULLY!!";<br>
System.out.println(msg);<br> // Sleeping to let standard out flush.<br> sleep(150);<br> System.err.println(msg);<br> // Sleeping to let standard error flush.<br>
sleep(150);<br> System.out.println();<br> }<br><br> // Print the entire configuration setup ("historical" in that it shows how we got to this..)<br> StatusPrinter.print(lc);<br>
<br>
<br><br><br>Kind regards,<br>Endre.<br><br><div class="gmail_quote">On Sun, Mar 4, 2012 at 20:54, Alex Glass <span dir="ltr"><<a href="mailto:alexander.glass@gmail.com">alexander.glass@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Simple question, <br><br>How do you configure logback programmatically like we can do in Log4j with DOMConfigurator.configure? I'd like to pass in a filename and have it load my configuration.<br clear="all">
<br>Thanks,<br>
Alex<br>
<br>_______________________________________________<br>
Logback-user mailing list<br>
<a href="mailto:Logback-user@qos.ch">Logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" target="_blank">http://mailman.qos.ch/mailman/listinfo/logback-user</a><br></blockquote></div><br>