Log4j2 SmtpAppender to include exceptions without the stack trace source code location - java

We have a SmtpAppender configured with a custom RateLimiter filter and ERROR log level, everything is working as expected except for the exceptions format, they are including the source code location which we would like to remove, we add such appender programmatically to each logger after the logging facility has been initialized, example of log:
2019-05-03 10:39:58,871 Thread-9 ERROR s.utils.Startup - Testing error
java.lang.RuntimeException: Testing error
at com.somepackage.SomeClass.lambda$enableEmailLogging$0(Startup.java:619) ~[classes/:?]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
The ~[classes/:?] and [?:1.8.0_212] are annoying to us, also; in general we would like our logging to always include the exceptions which is well accomplished in our RollingRandomAccessFile appender as well, but having the same problem of stack trace source code location being included, here is also a snippet of our rolling file appender:
<Appenders>
<RollingRandomAccessFile name="RollingFile" fileName="/opt/tomcat/logs/some-webapp.log"
filePattern="/opt/tomcat/logs/some-webapp.log-%d{yyMMdd-HH}"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{DEFAULT} %15.15t %-5p %15.15c - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
I have looked all over for a way to accomplish this with no luck.

Changing the PatternLayout's pattern from:
%d{DEFAULT} %15.15t %-5p %15.15c - %m%n
to
%d{DEFAULT} %15.15t %-5p %15.15c - %m%n%throwable
and removing the option ignoreExceptions from the appenders seem to accomplish what I wanted.

Related

Log4J throws illegal char <:> exception while processing Appender File in Springboot WebApp

I am using Log4J along with Springboot in my J2EE based WebApp.
It throws illegal char <:> exception while processing Appender File.
Log4J version: 2.11.1
The logger properties in my application.properties file are as below
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.file=log\\web.log
I understand that it's because of the character colon ':' , but even after trying out different patterns, I still get the same error message.
I don't know where the ${sys:LOG_PATH} part is coming from and how to fix this.
Can someone please help.
Attaching the error stack trace as well.
Thanks in advance :)

After disabling console appender in log4j still getting info message in catalina.out

I initialize root logger with level ALL for R.
log4j.rootLogger=ALL, R
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.R.File=${catalina.home}/logs/synconextcontroller.log
log4j.appender.R.DatePattern='-'yyyyMMdd'.log'
log4j.appender.R.Threshold=ERROR
log4j.logger.org.apache.catalina=DEBUG, R
log4j.logger.org.hibernate=ERROR
log4j.logger.org.quartz=ERROR
log4j.logger.com.mchange=ERROR
jersey.level=INFO
Despite this, logs are getting written to file.
How can I prevent this from happening?

Why doesn't Slf4j/Logback log filename and row number

I'm having problems with my logging where slf4j doesn't log the filename and row number of the message/stacktrace.
Code:
private static Logger log = LoggerFactory.getLogger(SomeService.class);
#Override
public void aService(ServiceAdmin sa) throws Exception {
log.debug(LoggerFactory.class.toString());
log.debug(log.getClass().getName());
log.debug("Setup Example");
SomeService.setDefault(Example.getInstance());
log.debug("Example finished");
}
Log:
2015-04-20 14:47:26.573 DEBUG [main] null:-1 - class org.slf4j.LoggerFactory
2015-04-20 14:47:26.574 DEBUG [main] null:-1 - ch.qos.logback.classic.Logger
2015-04-20 14:47:26.574 DEBUG [main] null:-1 - Setup Example
2015-04-20 14:47:26.575 DEBUG [main] null:-1 - SomeService finished
This is pars of my logback.xml that relates to this class
<logger name="com.bredband.nexusgw.services">
<level value="debug" />
<appender-ref ref="nexusservice" />
</logger>
<appender name="nexusservice"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>/var/log/nexus/nexusjgw/jgw/Service.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>/var/log/nexus/nexusjgw/jgw/Service.log.%i
</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>5</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>200MB</MaxFileSize>
</triggeringPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %F:%L - %m%n</Pattern>
</layout>
</appender>
Has anyone seen this before and can point me in the right direction?
BR
Stefan
-- Update --
I changed the %F to %C, but that didn't change anything in the logs. Then I tried to set debug = "on" in the javac header in my ant file and now it's logging classname and line number.
2015-04-21 08:28:55.910 DEBUG [main] Service.java:20 - class org.slf4j.LoggerFactory
2015-04-21 08:28:55.911 DEBUG [main] Service.java:21 - ch.qos.logback.classic.Logger
2015-04-21 08:28:55.911 DEBUG [main] Service.java:22 - Setup Example
2015-04-21 08:28:55.912 DEBUG [main] Service.java:24 - Example finished
Although this is working, it's not something I've changed before. The only thing that has changed in this project over time is the addition of other external jars. I'm not really sure how adding a duplicate/newer/older version of slf4j in another external jar would affect the logging though, any version should look for the logback file?
-- Update --
After checking alot of historical checkins of the build.xml file I found that we actually had debug = "on" earlier and the problem may actually have been introduced by this parameter beeing set to "off".
Thank you for your help.
BR
Stefan
You should first make sure that you compile your application with debug information. If calling javac directly, then it is the -g command-line parameter. However most build tools (like maven or ant) have a specific way to provide this parameter.
You may also want to replace %F with %C, which should work without the debug information.

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

multiple fileappenders in log4j

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

Categories