简体   繁体   中英

log4j not initialized and no appenders found

I'm trying to use log4j in my Java application (no web-app). Therefore I added the configuration file directly within my /src folder.

log4j.properties

log4j.rootLogger=DEBUG

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

My main class is in a package with a sub-namespace and within there I'm trying to log as follows:

final static Logger logger = Logger.getLogger(MainClass.class);
public static void main(String[] args) {
    //BasicConfigurator.configure();
    logger.warn("some warning");
}

But still I'm getting an error message and I don't know what to do. You see the commented line BasicConfigurator above. As far as I know, the log4j.properties should be read without calling that line, right?

log4j:WARN No appenders could be found for logger (at.company.project.poi.MainClass). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

According to documentation, file should be on classpath and named 'log4j 2 .properties'

You can also set directly the configuration by setting -Dlog4j.configurationFile="<your config filepath>"

Source: https://logging.apache.org/log4j/2.x/manual/configuration.html

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