java.lang.Class:在java程序

时间:2016-11-29 04:12:12

标签: java log4j java.lang.class

我正在尝试使用log4j运行独立的java程序,但在调试时接收以下内容(控制台上没有与log4j相关的日志):

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 / resources中。

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:\MyFirstProject\src\main\resources\log4j.properties

2 个答案:

答案 0 :(得分:3)

在我看来,你已经混淆了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 docs

答案 1 :(得分:0)

这不是错误!

它只是说您的记录器级别设置为ERROR,因此您不会看到记录到DEBUG / INFO级别的消息。

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

https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties https://logging.apache.org/log4j/2.x/manual/appenders.html#ConsoleAppender

使用XML格式通常更好。