以编程方式使用Log4j 2文件追加程序时,日志文件中的垃圾

时间:2016-02-08 15:38:10

标签: java logging log4j log4j2

我有一个用例,我必须为我的所有记录器使用不同的文件名,我试图在Log4j 2中以编程方式使用文件appender

我正在使用以下XML文件进行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>

然后我使用以下代码将文件追加器添加到我的记录器:

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);
    }

以上代码是Log4j 2文档(http://logging.apache.org/log4j/2.x/manual/customconfig.html#AddingToCurrent)的逐字副本,但PatternLayout的创建方式除外。

我收到以下数据而不是我的日志

  

%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

我将不胜感激。

2 个答案:

答案 0 :(得分:0)

抱歉,我无法重现这一点。

在调整文件名后,我使用了以下包含getLogger方法的类:

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 is a 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);
    }
}

CLASSPATH环境变量仅包含当前目录.和log4j2 JAR log4j-api-2.5.jarlog4j-core-2.5.jar。您的log4j2.xml文件的副本位于同一目录中。

当我运行这个课程时,我得到了以下输出

2016-02-13 13:49:32,228 main WARN The bufferSize is set to 4000 but bufferedIO is not true: false

Test_created.log文件只包含以下行:

2016-02-13 13:49:32,235 [main] INFO Test - This is a test line

答案 1 :(得分:0)

定义AppenderRef时出错:

AppenderRef ref = AppenderRef.createAppenderRef(loggerName+"ref", Level.ALL, null);

第一个参数是 Appender的名称。您正在创建名称为&#34;文件&#34; 的FileAppender,因此应创建AppenderRef:

AppenderRef ref = AppenderRef.createAppenderRef("File", Level.ALL, null);