简体   繁体   中英

log4j doesn't find appenders

Always I try to use a formal and structurized log api is a nightmare (this explains why so many people just print to console)

I'm trying to use log4j in my project

to instantiate the logger I do:

   private static final Logger log = LoggerFactory.getLogger(Instagram.class.getPackage().getName());

when I want to register an event I do:

log.info("some information");

I've a log4j.properties file in the src folder like:

log4j.logger.com.tomatechines.instagramapi.api = INFO, CONSOLE, FILE, ERROR

log4j.rootLogger = INFO, FILE, ERROR

log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern = yyyy-MM-dd'.log'
log4j.appender.FILE.File = logs/log-
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n

log4j.appender.ERROR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.DatePattern = yyyy-MM-dd'.log'
log4j.appender.ERROR.File = logs/errorlog-
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
log4j.appender.ERROR.Threshold=WARN

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n

but when the code runs the only thing printed to console or file is:

log4j:WARN No appenders could be found for logger (com.tomatechines.instagramapi.api).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

why it doesn't find my file, I set 3 appenders and why can't it find just one

First of all, I can see that you are using SLF4J with log4j 1.2.x, because you are instantiating the logger with LoggerFactory.getLogger . If you want to use only log4j 1.2.x you should get the logger with Logger.getLogger . Making sure that you have imported the classes from org.apache.log4j package.

You must also make sure that you have put the log4j.properties file under the classpath. And in my opinion you should try first with a very simple configuration, to avoid configuration problems. Something like:

# Root logger option
log4j.rootLogger=INFO, 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

Probably your problem is about the location of log4j.properties file.

Your configuration file is fine; the problem is simply that this configuration file is not included into your classpath at runtime.

You should put your log4j.properties file under the src/main/resources folder.

(if you are using standard maven or gradle settings; otherwise it depends on which IDE you are using)

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