Log4j SMTP Appender - java

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

Related

Why do I get DEBUG level logging in console?

I have a log4j properties file defined like so:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
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
log4j.logger.com.github.user=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=target/cucumber-parser.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Based on my understanding, INFO level logging should be routed to stdout (console) and DEBUG should be only written to file. However I get both, INFO and DEBUG level in console. What am I doing wrong?
I managed to solve my problem by applying the below line to log4j.appender.stdout:
log4j.appender.stdout.Threshold=INFO
With this line added to the original configuration that I posted in my original question, only the INFO level logs will be printed to the console and DEBUG will be rerouted to a file as desired.
Because INFO level covers DEBUG level. So that you see both.
See more Logging Levels
If you check log4j doc or this tutorial you can see more information about levels, but basically: ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
So log shows the selected level and all the greaters.
add log4j.additivity.com.github.user=false.
As in your config, level of com.github.user is being set to debug. And besides "file", it will also be additive to root logger, so you can see debug log of com.github.user in the console.
In fact, if you write the config this way, splitting loggers and appenders, you can understand better.
log4j.rootLogger=INFO, stdout
log4j.logger.com.github.user=DEBUG, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
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
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=target/cucumber-parser.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
For loggers, you specified com.github.user to be debug, and all others info with rootlogger setting.
Then com.github.user will be printed to file, and all logs will be printed to console . And if you set the additivity, com.github.user will be removed from rootlogger, so there will only be other logs in console.

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.

Tomcat Save Log History

I am using log4j in webapp deployed on tomcat, but cuurently my logs comes under catalina.log file, but only error logs remain in history, i want my all logs to be remain in history , additionally it will be good if if i get logs datewise.
my logs4j looks like this
log4j.rootLogger=INFO, myConsoleAppender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern= [%t] %d{dd MMM yyyy HH:mm:ss,SSS} : %c %x : %m%n
i am only using info level of logs.
Try this
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
# Define the daily rolling file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=/logs/app.log
log4j.appender.FILE.DatePattern='.'yyyy-MMM-dd
# Define the layout for the file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern= %-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
Above will create a log file on the tomcat installation directory under the folder logson daily basis. You can change the log file path by modifying the log4j.appender.FILE.File=/path/to/logfile

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");

Categories