java - Garbage in Log Files when using Log4j 2 file appender programmatically -
i have use case have use different file name loggers , trying using file appender programmatically in log4j 2
i using following xml file startup configuration of log4j
<?xml version="1.0" encoding="utf-8"?> <configuration status="warn" name="myapp" packages=""> <appenders> <file name="myfile" filename="c:\mylogs.log"> <patternlayout> <pattern>"%d{hh:mm:ss.sss} [%t] %-5level %logger{36} - %msg%n</pattern> </patternlayout> </file> </appenders> <loggers> <root level="debug"> <appenderref ref="myfile"/> </root> </loggers> </configuration>
i using following code add file appender loggers:
public static logger getlogger(string loggername) { final loggercontext ctx = (loggercontext) logmanager.getcontext(false); final org.apache.logging.log4j.core.config.configuration config = ctx.getconfiguration(); patternlayout layout = patternlayout.newbuilder().withpattern(patternlayout.simple_conversion_pattern).build(); appender appender = fileappender.createappender("c:/"+loggername+".log", "false", "false", "file", "true", "false", "false", "4000", layout, null, "false", null, config); appender.start(); config.addappender(appender); appenderref ref = appenderref.createappenderref(loggername+"ref", level.all, null); appenderref[] refs = new appenderref[] {ref}; loggerconfig loggerconfig = loggerconfig.createlogger("false", level.all, loggername, "true", refs, null, config, null ); loggerconfig.addappender(appender, null, null); config.addlogger(loggername, loggerconfig); ctx.updateloggers(); return logmanager.getlogger(loggername); }
the above code verbatim copy log4j 2 documentation (http://logging.apache.org/log4j/2.x/manual/customconfig.html#addingtocurrent) except how patternlayout created.
i getting following data instead of logs
%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n%d [%t] %p %c - %m%n
i appreciate help.
sorry, couldn't reproduce this.
i used following class, contains getlogger
method, after adjusting file name:
import org.apache.logging.log4j.*; import org.apache.logging.log4j.core.config.*; import org.apache.logging.log4j.core.appender; import org.apache.logging.log4j.core.appender.fileappender; import org.apache.logging.log4j.core.layout.patternlayout; import org.apache.logging.log4j.core.loggercontext; public class log4j2test { public static void main(string[] args) throws exception { getlogger("test").info("this test line"); } public static logger getlogger(string loggername) { final loggercontext ctx = (loggercontext) logmanager.getcontext(false); final org.apache.logging.log4j.core.config.configuration config = ctx.getconfiguration(); patternlayout layout = patternlayout.newbuilder().withpattern(patternlayout.simple_conversion_pattern).build(); appender appender = fileappender.createappender("/home/luke/stack overflow/"+loggername+"_created.log", "false", "false", "file", "true", "false", "false", "4000", layout, null, "false", null, config); appender.start(); config.addappender(appender); appenderref ref = appenderref.createappenderref(loggername+"ref", level.all, null); appenderref[] refs = new appenderref[] {ref}; loggerconfig loggerconfig = loggerconfig.createlogger("false", level.all, loggername, "true", refs, null, config, null ); loggerconfig.addappender(appender, null, null); config.addlogger(loggername, loggerconfig); ctx.updateloggers(); return logmanager.getlogger(loggername); } }
the classpath
environment variable contains current directory .
, log4j2 jars log4j-api-2.5.jar
, log4j-core-2.5.jar
. copy of log4j2.xml
file in same directory.
when ran class, got following output
2016-02-13 13:49:32,228 main warn buffersize set 4000 bufferedio not true: false
the test_created.log
file contained following line:
2016-02-13 13:49:32,235 [main] info test - test line
Comments
Post a Comment