multiple fileappenders in log4j - java

I have to put the log info in two separate log files based on some condition.how to do that.
here is my logging.properties file
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.RollingFileAppender
log4j.appender.stdout.File=${catalina.home}/logs/std.log
log4j.appender.stdout.MaxFileSize=200KB
log4j.appender.stdout.MaxBackupIndex=2
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%c] %p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/demo.log
log4j.appender.R.MaxFileSize=200KB
log4j.appender.R.MaxBackupIndex=2
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%c] %p - %m%n

You can define multiple appenders and assign java packages to the appenders. In the example below all classes in com.mycorp.web will log to web.log at DEBUG level and classes in com.mycorp.db will log to db.log at INFO level.
log4j.rootLogger=debug, weblogger, dblogger
#Define which packages use which appenders
log4j.logger.com.mycorp.web=DEBUG,weblogger
log4j.logger.com.mycorp.db=INFO,dblogger
#Ensure the logs don't add to each other
log4j.additivity.com.mycorp.web=false
log4j.additivity.com.mycorp.db=false
#Define web appender
log4j.appender.weblogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.weblogger.File=/var/log/myapp/web.log
log4j.appender.weblogger.DatePattern='.'yyyy-MM-dd
log4j.appender.weblogger.Append=true
log4j.appender.weblogger.layout=org.apache.log4j.PatternLayout
log4j.appender.weblogger.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m %n
#Define db appender
log4j.appender.dblogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dblogger.File=/var/log/myapp/db.log
log4j.appender.dblogger.DatePattern='.'yyyy-MM-dd
log4j.appender.dblogger.Append=true
log4j.appender.dblogger.layout=org.apache.log4j.PatternLayout
log4j.appender.dblogger.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m %n

Based on your condition, you can grab different loggers.
Logger x = predicate() ? Logger.getLogger("wombat") : Logger.getLogger("other");
http://logging.apache.org/log4j/1.2/manual.html (Search for "wombat" for the relevant code.)

If you want different file for some logger, you should write something like this:
log4j.logger.LOGGER_ONE=DEBUG, stdout
log4j.logger.LOGGER_TWO=WARN, R

Related

log4j, need to display only INFO and ERROR message in log file

I am new to log4j and worked to setup on eclipse and its running. I understood the chain of priority in the levels and this is my properties file config:
log4j.rootLogger=ALL, file, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
My question is
Is it possible to print only INFO and ERROR type of messages in log files with properties file configuration
Finally I got the solution for my requirement. Here I used concept of filters i.e LevelRangeFilter. And for every logging level we defined appender.
log4j.rootLogger=DEBUG, file, console, file1
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file.filter.a.LevelMin=INFO
log4j.appender.file.filter.a.LevelMax=INFO
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.file1=org.apache.log4j.RollingFileAppender
log4j.appender.file1.File=logs/justfortesting.log
log4j.appender.file1.Append=true
log4j.appender.file1.ImmediateFlush=true
log4j.appender.file1.Threshold=DEBUG
log4j.appender.file1.filter.g=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file1.filter.g.LevelMin=ERROR
log4j.appender.file1.filter.g.LevelMax=ERROR
log4j.appender.file1.MaxFileSize=1KB
log4j.appender.file1.MaxBackupIndex=2
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.logger.com.log4j=DEBUG, file, console, file1
log4j.additivity.com.log4j=false
If any one suggest to reduce the above properties configuration for same requirement,, please comment.
Try
log4j.appender.file.Threshold=INFO
Also see related SO question and documentation

Custom layout for log4j

There is a way to print different layout for different log level? For example:
logger.warn("Message");
print something like this: 2016-06-20 13:34:41,245 INFO (main:) Message
and for logger.info("Message2");
print just: Message2
Is possible to do that? To define one layout to warn e other layout for info
log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log4j.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%m%n %d %-5p [%c] (%t:%x) %m%n
# 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 %-5p [%c] (%t:%x) %m%n
I haven't tested but it should work. If you define two appenders and you assign a pattern for each appenders. Then the keyword Threshold should filter the level
:
# Root logger option
log4j.rootLogger=INFO, fileWarning, fileInfo, stdout
log4j.appender.fileWarning=org.apache.log4j.RollingFileAppender
log4j.appender.fileWarning.File=log4j.log
log4j.appender.fileWarning.MaxFileSize=10MB
log4j.appender.fileWarning.MaxBackupIndex=10
log4j.appender.fileWarning.layout=org.apache.log4j.PatternLayout
log4j.appender.fileWarning.layout.ConversionPattern=%m%n %d %-5p [%c] (%t:%x) %m%n
log4j.appender.fileWarning.Threshold=WARNING
log4j.appender.fileInfo=org.apache.log4j.RollingFileAppender
log4j.appender.fileInfo.File=log4j.log
log4j.appender.fileInfo.MaxFileSize=10MB
log4j.appender.fileInfo.MaxBackupIndex=10
log4j.appender.fileInfo.layout=org.apache.log4j.PatternLayout
log4j.appender.fileInfo.layout.ConversionPattern=%m%n
log4j.appender.fileInfo.Threshold=INFO
Hope it's help.

Duplicate exception and warning in separate file with log4j

log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=sip.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n
Above is my log4j configuration. How to duplicate all exceptions and warning in separate log file? I would like to avoid code mofidications
Just add another logger like "R2".
log4j.rootLogger=debug, stdout, R, R2
...
log4j.appender.R2=org.apache.log4j.RollingFileAppender
log4j.appender.R2.File=sip2.log
log4j.appender.R2.Threshold=WARN
...

log4j properties: LevelMatchFilter doesn't work

I was trying to route my Logging to two different files: one for INFO messages and another one for ERRORs. LevelMatchFilter seemed the right way to go. Unfortunately, all messages are logged to my info.log, not just the INFO messages. Any ideas what I did wrong?
Here's my config:
# Define the root logger with appender file
log4j.logger.com.my.class.ClassName=DEBUG, FILE, ERR, CA
# Define the info file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=info.log
log4j.appender.FILE.filter.a=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.FILE.filter.a.LevelToMatch=INFO
log4j.appender.FILE.filter.a.AcceptOnMatch=true
# Define the layout for info file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d %-5p %c - %m%n
# Define the error file appender
log4j.appender.ERR=org.apache.log4j.FileAppender
log4j.appender.ERR.File=error.log
log4j.appender.ERR.Threshold=ERROR
# Define the layout for error file appender
log4j.appender.ERR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERR.layout.conversionPattern=%d %-5p %c - %m%n
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d %-5p %c - %m%n
I think your file appender config should look something like this:
# Define the info file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=info.log
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE.filter.a=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.FILE.filter.a.LevelToMatch=INFO
log4j.appender.FILE.filter.a.AcceptOnMatch=true
log4j.appender.FILE.filter.b=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.FILE.filter.b.LevelToMatch=ERROR
log4j.appender.FILE.filter.b.AcceptOnMatch=false
log4j.appender.FILE.filter.c=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.FILE.filter.c.LevelToMatch=FATAL
log4j.appender.FILE.filter.c.AcceptOnMatch=false
I finally get a solution (using filters) to a similar problem without making an external XML file, here is the resulting code:
log4j.rootLogger=info, R, ERROR
#### only INFO
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=FacturaElectronica.log
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.R.filter.a.LevelMin=INFO
log4j.appender.R.filter.a.LevelMax=INFO
#### only ERROR
log4j.appender.ERROR=org.apache.log4j.RollingFileAppender
log4j.appender.ERROR.File=FacturaElectronicaError.txt
log4j.appender.ERROR.MaxFileSize=500KB
log4j.appender.ERROR.MaxBackupIndex=1
log4j.appender.ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.ERROR.filter.b=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.ERROR.filter.b.LevelToMatch=ERROR
log4j.appender.ERROR.filter.b.AcceptOnMatch=true
log4j.appender.ERROR.Threshold=ERROR
LevelMatchFilter has nothing to do with what you need.
You need to define 2 loggers, one will have the ERR appender and the other the FILE appender:
log4j.logger.my.info.logger=INFO, FILE
log4j.logger.my.err.logger=ERROR, ERR
And in the code you need to provide the logger name when you initialize the logger:
static Logger logger = Logger.getLogger("my.info.logger");

Log4j SMTP Appender

Hi I am a novice and the application I am currently coding for uses struts 1.2 and java. We currently use Log4j for log files but I need to implement SMTP Appender so that our errors are emailed to us.
I've tried everything to get errors to be emailed with no luck. Below is our log4j.properties file.
Any suggestions?
Thanks!
log4j.rootLogger= INFO, stdout, logfile, mail
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
# Keep three backup files
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=C:/LOGS/WIRE.log
log4j.appender.logfile.MaxFileSize=2048KB
#email appender
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.BufferSize=1
log4j.appender.mail.SMTPHost=smtp.serverhere.com
log4j.appender.mail.From=johndoe#serverhere.com
log4j.appender.mail.To=johndoe#serverhere.com
log4j.appender.mail.Subject=Application Error
log4j.appender.mail.threshold=error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d %p [%c] - <%m>%n
# Hibernates use of the org.apache classes spews out stuff like mad.
log4j.logger.org.apache=INFO
# Springframework is very talkative too.
log4j.logger.org.springframework=INFO
# acegisecurity
#log4j.logger.org.acegisecurity = INFO
# Quartz trigger checking
log4j.logger.org.quartz.impl.jdbcjobstore=INFO
#log4j.logger.org.springframework.scheduling.quartz=INFO
By default, the appender only sends an email when something is logged at the ERROR or FATAL levels.
As a side note, it appears that your threshold property may have improper case. I believe:
log4j.appender.mail.threshold=error
should be
log4j.appender.mail.Threshold=error
Edit...
Log4j can be put into debug mode by configuring the log4j.debug configuration property. This might provide some output regarding the SMTP appender.
Define the root logger in error level and override this for selected packages. The mail appender will remain in the error level.
# Log appenders
#log4j.rootLogger=INFO, CONSOLE
log4j.rootLogger=ERROR, FILE, MAIL
# Log levels
log4j.logger.com.example.application=INFO
log4j.logger.com.example.application.package=DEBUG
# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n
log4j.appender.CONSOLE.Encoding=UTF-8
# FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.File=/tmp/application.log
log4j.appender.FILE.MaxFileSize=1000KB
log4j.appender.FILE.MaxBackupIndex=99
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n
log4j.appender.FILE.Encoding=UTF-8
# MAIL
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.BufferSize=1
log4j.appender.MAIL.SMTPHost=smtp.example.com
log4j.appender.MAIL.From=application#example.com
log4j.appender.MAIL.To=developer#example.com
log4j.appender.MAIL.Subject=Exception in Application
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n

Categories