简体   繁体   中英

Why is my log4j class only logging FATAL messages?

I have the following log4j Java class:

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

public class HelloWorldLog4J {

    private static final Logger logger = LogManager.getLogger("HelloWorld");


    public static void main(String[] args) {

        System.out.println("pre_loggerDOTinfo");
        System.out.println();
        //logger.setLevel(Level.FATAL);   //used to be.. 

        logger.info("Hello, this is an INFO message");
        logger.warn("Hello, this is an WARN  message");
        logger.fatal("Hello, this is an FATAL  message");
        logger.fatal("Hello, this is an FATAL  message ALSO, #2");
        logger.fatal("Hello, this is an FATAL  message ALSO, #3");
        logger.fatal("Hello, this is an FATAL  message ALSO, #4");
        logger.debug("Hello, this is an FATAL  message");

        logger.info("Hello, this is an INFO message");
        logger.info("Hello, this is an INFO message");
        System.out.println();
        System.out.println("post_loggerDOTinfo");

    }

}

And in the same directory, I have a log4j properties file:

# Root logger option
log4j.rootLogger=WARN, Console


# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= C:\\Users\\adel\\Desktop\\Misc_Stuff\\Java_Code\\logging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
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

As you can see, I commented out the logger.setLevel(Level.FATAL) code, as it caused my program to crash (said it can't find setLevel, and even when I tried toLevel it did not work)

My output is this :

pre_loggerDOTinfo

16:47:09.575 [main] FATAL HelloWorld - Hello, this is an FATAL message 16:47:09.575 [main] FATAL HelloWorld - Hello, this is an FATAL message ALSO, #2

16:47:09.575 [main] FATAL HelloWorld - Hello, this is an FATAL message ALSO, #3

16:47:09.575 [main] FATAL HelloWorld - Hello, this is an FATAL message ALSO, #4

post_loggerDOTinfo

I'm a little confused about which properties file is being used in my class. Is there a way to trace back the version of log4j I use, and where it gets the log4j properties info?

any tips appreciated, thanks !~

Your application uses log4j2 classes (the package is org.apache.logging.log4j , while log4j-1.x is in the org.apache.log4j namespace).

However, your configuration is a properties file aiming at log4j-1.x.

Log4j2 does not understand properties file-based configurations. You must provide an XML (or Jason or YAML) configuration file. Log4j2 will look for a config file named log4j2.xml in the classpath. If you put it elsewhere you need to tell log4j2 the path using the log4j.configurationFile system property.

The log4j2 manual has many examples configurations (the docs are much better than for log4j-1.x); I suggest starting with the example config shown for the RollingFile appender .

If Log4j2 cannot find a configuration it will use a default config that only logs at ERROR and FATAL level to the console.

Finally, be aware that with log4j2, you need both the log4j-api and the log4j-core jar files in the classpath.

If you are using IntelliJ, make sure that the log4j2.xml are placed at the src/main/resource folder. If project only logs FATAL messages, that often mean that you placed the log4j2.xml file at the wrong location (or there are 2 files with same name at different locations in the project, etc) and framework log4j could not find it and prints FATAL messages only as default.

project (root)
|_src
  |_main
    |_java
    |_resource (add log4j2.xml here)
  |_test
    |_java
    |_resource

I am using IntelliJ, latest log4j-core and taken config file examples from: http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM