java - Configuring LogBack/ConsoleAppender to write to STDERR -
i'd configure logback consoleappender use stderr, opposed stdout. in particular, pure code, opposed properties/xml files. here's attempt. key notes regarding code: when .reset() called (as per code below), nothing logs out @ all. when .reset() not called, logs sent stdout (thus indicating code below not work @ all).
what's key bit enable this?
import ch.qos.logback.classic.level; import ch.qos.logback.classic.logger; import ch.qos.logback.classic.loggercontext; import ch.qos.logback.classic.encoder.patternlayoutencoder; import ch.qos.logback.classic.spi.iloggingevent; import ch.qos.logback.core.consoleappender; import org.slf4j.loggerfactory; // stuff private static logger buildlogger() { loggercontext lc = (loggercontext) loggerfactory.getiloggerfactory(); logger logger = (ch.qos.logback.classic.logger) loggerfactory.getlogger(henson.class); lc.reset(); patternlayoutencoder ple = new patternlayoutencoder(); ple.setpattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n"); ple.setcontext(lc); ple.start(); consoleappender<iloggingevent> ca = new consoleappender<>(); ca.settarget("system.err"); ca.setcontext(lc); ca.start(); logger.detachandstopallappenders(); logger.addappender(ca); logger.setlevel(level.debug); return logger; }
the trick add encoder console , ensure loggers come context.
private static logger buildlogger() { loggercontext lc = (loggercontext) loggerfactory.getiloggerfactory(); lc.reset(); logger logger = lc.getlogger(henson.class); logger.detachandstopallappenders(); patternlayoutencoder ple = new patternlayoutencoder(); ple.setpattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n"); ple.setcontext(lc); ple.start(); consoleappender<iloggingevent> ca = new consoleappender<>(); ca.settarget("system.err"); ca.setcontext(lc); ca.setencoder(ple); ca.start(); logger.addappender(ca); logger.setlevel(level.debug); return logger; }
Comments
Post a Comment