logging - Programmatically add new Appender in log4j2 -


i have log4j2.xml defines base appenders project needs, in cases need add new appenders during runtime log4j states should possible (https://logging.apache.org/log4j/2.0/manual/architecture.html under section appender addloggerappender method should: "...the appender attached , loggers notified update loggerconfig references."). however, not seem work (or i'm missing step).

i'm running log4j2 version 2.6 , log4j2.xml defines root-logger of level debug , have 3 appenders (one stdout, 1 stderr , rollingfile). without trying change logging configuration works should.

here code i've tried adding appenders:

public static void addfileappender(string logpath, string name, level level){     loggercontext ctx = (loggercontext) logmanager.getcontext(true);     configuration config = ctx.getconfiguration();     layout<?> layout = patternlayout.createlayout(             patternlayout.simple_conversion_pattern,              null,              config,              null,             null,false, false, null, null);     appender fileappender = fileappender.createappender(logpath, "false", "false", name, "true",         "false", "true", null, layout, null, "false", null, config);     fileappender.start();     config.addloggerappender((org.apache.logging.log4j.core.logger) logmanager.getrootlogger(), fileappender);     ctx.updateloggers(); }   public static void addoutputstreamappender(outputstream appendto, string name, level level){     loggercontext ctx = (loggercontext) logmanager.getcontext();     configuration config = ctx.getconfiguration();     appender streamappender = outputstreamappender.createappender(null, null, appendto, name, true, false);     streamappender.start();     config.addloggerappender((org.apache.logging.log4j.core.logger) logmanager.getrootlogger(), streamappender);     ctx.updateloggers(config); } 

trying find problem wrote simple code check appenders of root-logger:

public void printrootappenders() {     loggercontext loggercontext = (loggercontext) logmanager.getcontext();     configuration configuration = loggercontext.getconfiguration();      // root logger , check appenders     loggerconfig rootloggerconfig = configuration.getloggerconfig(logmanager.root_logger_name);     map<string, appender> appenders = rootloggerconfig.getappenders();      for(entry<string, appender> app: appenders.entryset()){         system.out.println("appender {" + app.getkey() + "} : {" + app.getvalue().getlayout().tostring()+ "}");     } } 

simply calling printrootappenders before adding new appenders give output:

appender {stderr} : {%d{hh:mm:ss} [%-5p] - %m%n} appender {stdout} : {%m%n} appender {logfile} : {%d{yyyy-mm-dd hh:mm:ss} %-5p %c [%t] - %m%n} 

after calling both add-methods, addfileappender name "extralogfile" , addoutputstreamappender name "bytearray" (expected) output:

appender {stderr} : {%d{hh:mm:ss} [%-5p] - %m%n}  appender {bytearray} : {%m%n}  appender {extralogfile} : {%d [%t] %p %c - %m%n}  appender {stdout} : {%m%n} appender {logfile} : {%d{yyyy-mm-dd hh:mm:ss} %-5p %c [%t] - %m%n} 

here it's interesting extralogfile created (right location, name etc), it's empty though log events should end there. log-events printed in default appenders defined in xml-file. bytearrayoutputstream connect other appender empty.

hopefully i've missed obvious, can't figure out.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)