I am trying to learn about log4j so I just tried to do something which is very simple;
Logger logger = Logger.getLogger("ClientApplicationLog");
logger.info("Logger Test");
But after making this I got;
log4j:WARN No appenders could be found for logger (ClientApplicationLog).
log4j:WARN Please initialize the log4j system properly.
Do you know where I am wrong ?
Thank you all
You're missing the log4j.properties
or log4j.xml
in your classpath.
You can bypass this by using
BasicConfigurator.configure();
But beware this will ONLY log to System.out and is not recommended. You should really use one of the files above and write to a log file.
A very simple example of log4j.properties
would be
#Log to Console as 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 %3x - %m%n
#Log to file FILE
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logfile.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n
#Root Logger
log4j.rootLogger=INFO, stdout, file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With