I am using log4j (1.2.15) for logging in my application, where I have two file appenders for two different packages (darius.log4j.sql and darius.log4j.net) and a console appender. Its configuration is this:
# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# FILE A
log4j.appender.A=org.apache.log4j.FileAppender
log4j.appender.A.File=A.log
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# FILE B
log4j.appender.B=org.apache.log4j.FileAppender
log4j.appender.B.File=B.log
log4j.appender.B.layout=org.apache.log4j.PatternLayout
log4j.appender.B.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#
log4j.rootLogger=ERROR, CONSOLE
#
log4j.logger.darius.sql=DEBUG, A
log4j.logger.darius.net=INFO, B
What I need now is to add a new file appender programmatically if a specific data combination appears. I konw how to define this new file appender (ex C) but I don't know how to set the level and the new appender to a third package:
log4j.logger.darius.ws=INFO, C
where the C is the third file appender.
Any hit?
Simple! After you create your third appender (C) get a logger for your namespace (darius.ws), set the INFO level for this logger. At the end just append this C appender to the logger.
FileAppender fileAppender = new FileAppender();
fileAppender.setName("C");
fileAppender.setFile("C.log");
fileAppender.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fileAppender.setThreshold(Level.INFO);
fileAppender.setAppend(true);
...
Logger someLogger = Logger.getLogger("darius.ws");
someLogger.setLevel(Level.INFO);
someLogger.addAppender(fileAppender);
Best regards
Related
log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.FileAppender.
log4j:WARN No such property [maxFileSize] in org.apache.log4j.FileAppender.
What does this mean, what causes it, and how should you fix it?
# initialize root logger with level ERROR for stdout and file
log4j.rootLogger=DEBUG,stdout,file
# set the log level for these components
log4j.logger.com.endeca=DEBUG
log4j.logger.com.endeca.itl.web.metrics=INFO
# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# use a simple message format
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# add a FileAppender to the logger file
log4j.appender.file=org.apache.log4j.FileAppender
# create a log file
log4j.appender.file.File=E:\\temp\\GAPN.log
# take backup periodically
log4j.appender.file.MaxFileSize=2KB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
# use a more detailed message pattern
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
The appender 'file' is of type FileAppender. FileAppender does not have a field MaxFileSize. This is a property of RollingFileAppender class, which is a subclass of FileAppender and allows for log rotation based on size.
I have a problem with logging in tomcat.
I use a DailyRollingFileAppender, but the problem is: the file with date log.log.2015-05-25 have data from 2015-05-26... it happens with all files.
This is my log4j.properties:
log4j.appender.GDebug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.GDebug.File=/opt/tomcat/logs/log.log
log4j.appender.GDebug.DatePattern='.'yyyy-MM-dd
log4j.appender.GDebug.layout=org.apache.log4j.PatternLayout
log4j.appender.GDebug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %X{username} %c{1} %m%n
log4j.appender.GDebug.Threshold=DEBUG
Thanks in advance!
I am trying to get my log4j property working, I messed it up I think.
log4j.rootLogger=ALL
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{ddMMMyy HH:mm:ss,SSS} %-4r [%t] %-5p %c{1} %x - %m%n
log4j.logger.org.hibernate=ERROR
log4j.debug=TRUE
log4j.logger.net.spy.memcached=ERROR
log4j.logger.com.mcruiseon.server=ALL
My package is com.mcruiseon.server
You might try something like this:
log4j.rootLogger=ERROR, CA
log4j.logger.com.mcruiseon.server=INFO, CA
log4j.additivity.com.mcruiseon.server=false
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{ddMMMyy HH:mm:ss,SSS} %-4r [%t] %-5p %c{1} %x - %m%n
This does: define a CA console logger, tell rootLogger to log ERROR and above to CA, define a logger for your package that logs INFO and above to CA, and sets its additivity to false to prevent messages from appearing in more than one appender (which is no issue here since you only have CA, but will become one once you route your logger to its own file appender).
My log4j configuration is as follows
log4j.rootLogger=INFO, CA, FA, DA
#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=/home/admin/logs/sysout.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.FA.Threshold = WARN
#File Appender 2
log4j.appender.DA=org.apache.log4j.FileAppender
log4j.appender.DA.File=/home/admin/logs/debug.log
log4j.appender.DA.layout=org.apache.log4j.PatternLayout
log4j.appender.DA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.DA.Threshold = TRACE
My understading is
INFO will be logged to console
WARN will be logged to sysout.log
TRACE will logged to debug.log
But WARN is getting logged to both debug.log and sysout.log. Also TRACE is not logging in any of the file.
Console is having TRACE and WARN both.
Can you please tell me what am I doing wrong
You need to separate the logger and appender concepts in your mind.
For the three appenders, remember that the threshold is the lowest level of message that the appender will process. An appender will process messages at its threshold level or any higher level.
CA has no threshold set, so it will log all messages that are sent to it regardless of level. Similarly DA has a threshold of TRACE so it will also log everything that is sent to it (since TRACE is the lowest level). FA has a threshold of WARN so it will filter out any messages at levels below WARN - it will contain only WARN, ERROR and FATAL messages.
The important part of that previous paragraph is "all messages that are sent to it". Since you have configured your root logger with a level of INFO and have not configured any specific loggers to a lower level, only messages at INFO and above will be sent to the appenders - DEBUG and TRACE messages will be silently dropped. This is why you see no TRACE output in any of your loggers.
I'd like to have 2 different log4j ConsoleAppenders defined with different layouts. I tried the following:
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{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.stdoutMDC=org.apache.log4j.ConsoleAppender
log4j.appender.stdoutMDC.Target=System.out
log4j.appender.stdoutMDC.layout=org.apache.log4j.PatternLayout
log4j.appender.stdoutMDC.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L (hibernateLoadPlanWalkPath->%X{hibernateLoadPlanWalkPath}) - %m%n
However, when I attempt to use these appenders I am running into problems. I have the first appender attached to root and then attempt to attach the second to certain ancestor loggers:
log4j.rootLogger=info, stdout
log4g.logger.org.hibernate.loader.plan=trace, stdoutMDC
log4g.additivity.org.hibernate.loader.plan=false
log4g.logger.org.hibernate.persister.walking=trace, stdoutMDC
log4g.additivity.org.hibernate.persister.walking=false
The trouble I am having is that the messages from both of those ancestor loggers end up going to the stdout appender and not the stdoutMDC appender. I tried both with and without disabling additivity, but no difference.
Any ideas?
please try this.
It may help out you
Note : %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC)
note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC)
log4j.appender.consoleAppender.layout.ConversionPattern = %-4r [%t] %5p %c %x - %m - %X{userName}%n
log4j.rootLogger = DEBUG, consoleAppender