java – PropertyConfigurator for Log4j does not exists anymore?

java – PropertyConfigurator for Log4j does not exists anymore?

Log4j 2 also supports configuration in properties files, but be aware:

  • the file needs to be called log4j2.properties
  • the configuration syntax is different from the Log4j 1.2 syntax (the configuration in the question uses the old Log4j 1.2 syntax, Log4j 2 will not understand this)

The Log4j 2 user manual has mostly XML examples, so many people find it easier to use the XML configuration syntax. (Again, the config file should be named log4j2.xml, Log4j 2 will ignore log4j.xml.)

define log4j property file

@ prueba.java

import org.apache.log4j.Logger;

public class prueba {

    private static Logger LOGGER = Logger.getLogger(prueba.class);

    public static void main(String[] args) {
        LOGGER.debug(This is Debug);
        ...
    }
}

@ pom.xml

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>

at src/resources/log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, file, stdout
# Direct log messages to stdout
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
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log/log4j-application.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
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

output to stdout and log/log4j-application.log file

2018-05-11 14:29:43 DEBUG prueba:8 - This is Debug
2018-05-11 14:29:43 INFO  prueba:9 - This is Info
2018-05-11 14:29:43 WARN  prueba:10 - This is Warn
2018-05-11 14:29:43 ERROR prueba:11 - This is Error
2018-05-11 14:29:43 FATAL prueba:12 - This is Fatal

java – PropertyConfigurator for Log4j does not exists anymore?

Leave a Reply

Your email address will not be published.