1. Practice with “Hello world”

1.1 Add the dependency to your POM


1.2 Create your log4j.properties file into your resource folder

# Root logger option
log4j.rootLogger=ERROR, stdout

# syntax : log4j.logger.[my class Fully qualified name]=[log level], appender
log4j.logger.mypackage.Lelogger=ERROR, stdout 

# Direct log messages to stdout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

1.3 Import log4J package into your class and Add a static object to your class

package mypackage;
import org.apache.log4j.Logger;
public class Lelogger {
    static final Logger logger = Logger.getLogger(Leloggerbis.class.getName());
    public static void main(String [ ] args)
        logger.error("Hello World!");

2. Configuration

2.1 Additivity

In order to avoid logging multiple time due to the hierarchy of LoggerConfigs, you may set to false the “additivity” parameter of your logger

log4j.additivity.mypackage.Leloggerbis = false
comments powered by Disqus