Log4j2 - 错误处理元素Appender

时间:2016-05-02 08:26:38

标签: java log4j2

我想编写一个简单的程序来测试log4j2。我从manual复制了大部分代码。 这是我得到的错误:

2016-05-02 10:11:31,054 main ERROR Error processing element Appender ([Appenders: null]): CLASS_NOT_FOUND
2016-05-02 10:11:31,054 main ERROR Error processing element Appender ([Appenders: null]): CLASS_NOT_FOUND
2016-05-02 10:11:31,117 main ERROR Unable to locate appender "STDOUT" for logger config "root"
2016-05-02 10:11:31,117 main ERROR Unable to locate appender "File" for logger config "MyLogger"

我的java代码:

package log4jtest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4jtest {
    private static final Logger logger = LogManager.getLogger("MyLogger");
    public static void main(String[] args) {
        logger.error("Hello, World!");
    }
}

我的log4j2配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" name="Log4jTest" packages="">
    <Properties>
        <Property name="filename">test.log</Property>
    </Properties>

    <Appenders>
        <Appender type="Console" name="STDOUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Appender>

        <Appender type="File" name="File" fileName="${filename}">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Appender>
    </Appenders>

    <Loggers>
        <Logger name="MyLogger" level="ALL" additivity="false">
            <AppenderRef ref="File" />
        </Logger>
        <Root level="ALL">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>

如何修复此错误?

3 个答案:

答案 0 :(得分:10)

在Log4j 2中,appender的类型在元素的名称中指定,而不是<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!doctype html> <html> <head> </head> <body> <div id="container"> <div id="menu"> <ul> <li> <a href="#">Manage Books</a> <ul> <li><a href="#">Books</a> <li><a href="#">Add Books</a> </li> </ul> </li> </ul> </div> <!--- end menu ---> </div> <!--- end container ---> <div id="form"> </div> </body> </html>属性。

换句话说,替换

等行
    public static T Compile<T>(this Expression expression)
    {
        return Expression.Lambda<Func<T>>(expression).Compile()();
    }

.Compile<type>()

IEnumerable<C> classes = this.backend.cs.Where(
    c => c.Field<C>("Matter").Compile<string>().EndsWith(string.Empty));

type

另请参阅jQuery.get()

答案 1 :(得分:3)

在我看来,这是错误的元素名称

<Appenders>
    <RollingRandomAccessFileAppender name="stdout" fileName="../logs/app.log" filePattern="../logs/app.%d{yyMMdd}.%i.log.gz">
...

正确的是

<Appenders>
    <RollingRandomAccessFile name="stdout" fileName="../logs/app.log" filePattern="../logs/app.%d{yyMMdd}.%i.log.gz">
...

答案 2 :(得分:-3)

使用以下行

public static Logger logger = Logger.getLogger("MyLogger");