Reputation: 41
I've seen questions about it, but none of it helped me. I'm using log4j, but as it works fine on console, it doesn't write anything to declared files. What is more, files were created, but nothing is saved in them. Code:
#default
log4j.rootLogger=ERROR,console
#Console Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%5p] [%t %d{hh:mm:ss}] (%F:%M:%L) %m%n
#Custom assignments
log4j.logger.controller=DEBUG,console
log4j.logger.service=DEBUG,console
log4j.logger.dao=DEBUG,console
#Disable additivity
log4j.additivity.controller=false
log4j.additivity.service=false
log4j.additivity.dao=false
#MyLogger
log4j.logger.classPath.myClass = INFO, CACHE
log4j.appender.CACHE=org.apache.log4j.RollingFileAppender
log4j.appender.CACHE.File = ./logs/cache.log
log4j.appender.CACHE.bufferedIO = false
log4j.appender.CACHE.ImmediateFlush=true
log4j.appender.CACHE.Threshold=info
log4j.appender.CACHE.layout=org.apache.log4j.PatternLayout
log4j.appender.CACHE.layout.ConversionPattern=[%5p] [%t %d{hh:mm:ss}] (%F:%M:%L) %m%n
I'm sure that logger is defined properly, because when additivity=false
, logs are not showed in console as expected. And when log4j.logger.myClass = INFO, CACHE, console
is added as well, logs are showed in console again. So logger declaration seems fine. So why are they not showed in log file?
I'm using org.apache.log4j.Logger.getLogger(myClass.class).info('msg')
syntax to use logger.
org.apache.log4j.Logger.getLogger("classPath.myClass").info('msg')
is not working either.
When trying log4j.rootLogger=INFO,console,CACHE
also nothing appears in the file.
Upvotes: 1
Views: 5396
Reputation: 12890
The logger name is specified wrongly. Please change it as follows
#MyLogger
log4j.logger.MyClass = INFO, CACHE
instead of
#MyLogger
log4j.logger.myClass = INFO, CACHE
MyClass is wrongly denoted as myClass. You can also make the logger instantiation as follows
Logger.getLogger("MyClass").info('msg');
Hope this helps!
Upvotes: 1