java.lang.class:在Java程序中初始化Log4j属性文件时错误



我正在尝试使用log4j运行独立的java程序,但是在调试时接收下面(没有log4j在控制台上的logs):

log= {Logger@1343} "java.lang.Class:ERROR in 18b4aac2"

有人可以建议这里有什么问题吗?

代码如下:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import java.io.IOException;
import java.sql.SQLException;
public class log4jExample {
static org.apache.logging.log4j.Logger log = LogManager.getLogger(log4jExample.class.getClass());
public static void main(String[] args)throws IOException,SQLException {
    System.out.println("in main...");
    log.debug("Hello this is a debug message");
    System.out.println("in main...2..");
    log.info("Hello this is an info message");
}
}

和log4j.properties文件如下,该文件保存在SRC/MAIN/RESOUDIONA中。

log4j.rootLogger=DEBUG, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\test\log4j-example.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

我正在使用VM选项运行我的Java程序

-Dlog4j.configurationFile=C:MyFirstProjectsrcmainresourceslog4j.properties

在我看来,就像您混合了log4j版本。您使用的配置文件是使用log4j 1.x格式。您必须将其转换为log4j 2.x格式。我尝试了您的示例,它有效。请参阅下面的详细信息。

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.bft.</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
    </dependencies>
</project>

重新成分代码

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class log4jExample {
    static final Logger logger = LogManager.getLogger(log4jExample.class);
    public static void main(String[] args) {
        System.out.println("in main...");
        logger.info("Hello this is a debug message");
        System.out.println("in main...2..");
        logger.info("Hello this is an info message");
    }
}

log4j.properties文件(以log4j2.x格式)

status = error
dest = err
name = PropertiesConfig
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT

输出:

in main...
Hello this is a debug message
in main...2..
Hello this is an info message
Process finished with exit code 0

参考log4j文档。

这不是错误!

它只是说您的记录器级别设置为错误,因此您不会看到已记录到调试/信息级别的消息。

也许您应该检查您的配置是否与log4j-2.x兼容:

https://logging.apache.org/log4j/2.x/manual/configuration.html#propertieshttps://logging.apache.org/log4j/2.x/manual/appenders.html#consoleappender

使用XML格式通常更好。

最新更新