java.lang.Class:在java程序中初始化log4j属性文件时出现ERROR[英] java.lang.Class:ERROR while initializing log4j properties file in java program

本文是小编为大家收集整理的关于java.lang.Class:在java程序中初始化log4j属性文件时出现ERROR的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用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/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:\MyFirstProject\src\main\resources\log4j.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 docs . P>

其他推荐答案

这不是错误!

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

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

/2.x/manual/configuration.html#properties https://logging.apache.apache.org.org/logg/log4j/2. X/Manual/appenders.html#consoleappender

使用XML格式通常更好.

本文地址:https://www.itbaoku.cn/post/1575077.html

问题描述

I am trying to run a standalone java program using log4j, but receiving below while debugging (with no log4j related logs on console):

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

Can someone please suggest what is wrong here?

The code is as below:

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

}

And the log4j.properties file is as below which is kept at 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

I am running my java program with VM option

-Dlog4j.configurationFile=C:\MyFirstProject\src\main\resources\log4j.properties

推荐答案

It seems to me like you have mixed up log4j versions. The configuration file you use is using log4j 1.x format. You have to convert it to log4j 2.x format. I tried your example and it works. See details below.

pom.xml file

<?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>

Re-factored code

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 file (in log4j2.x format)

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

Output:

in main...
Hello this is a debug message
in main...2..
Hello this is an info message

Process finished with exit code 0

Refer the log4j docs.

其他推荐答案

It is not an error !

It just says that your logger level is set to ERROR, so you will not see the messages logged to DEBUG/INFO levels.

Maybe you should check if your configuration is compatible with 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

Use XML format is usually better.