Override all log4j.properties for webapps in tomcat - java

i'm running Apache Tomcat 7.0.59 on a OpenSuse Leap 42.2 with Java 1.8.0_65 and have several webapplications deployed. Some of them are my own, and some of them are third-party.
Some webapps come with a log4j.properties file in their classpath. Now i want to have one global log4j.properties file which overrides all others so i can aggregate all logging information into one big my-tomcat.log file.
What i did was add the VM flag -Dlog4j.configuration=file:///path/to/my/custom-log4j.properties to tomcat's setenv.sh file which basically works great. Now i have my own my-tomcat.log file which contains all logging information.
But here is the problem: the "old" log files are still written. It seems that log4j is still reading all the app-specific log4j.properties files.
for example my custom-log4j.properties file looks like this:
# Set root logger level to error
log4j.rootLogger=INFO, File
###### Console appender definition #######
# All outputs currently set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
# use log4j NDC to replace %x with tenant domain / username
log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} %x %-5p [%c{3}] [%t] %m%n
#log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n
###### File appender definition #######
log4j.appender.File.Threshold=INFO
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=eWork.log
log4j.appender.File.Append=true
log4j.appender.File.DatePattern='.'yyyy-MM-dd
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{yyyy-MM-dd} %d{ABSOLUTE} %-5p [%c] [%t] %m%n
One of my webapps logging files looks like this:
# Set root logger level to error
log4j.rootLogger=error, Console, File
###### Console appender definition #######
# All outputs currently set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
# use log4j NDC to replace %x with tenant domain / username
log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} %x %-5p [%c{3}] [%t] %m%n
#log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n
###### File appender definition #######
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=alfresco.log
log4j.appender.File.Append=true
log4j.appender.File.DatePattern='.'yyyy-MM-dd
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{yyyy-MM-dd} %d{ABSOLUTE} %-5p [%c] [%t] %m%n
both my-tomcat.log as well as alfresco.log are created. Also the catalina.out file is written although i have not added the console appender in my custom-log4j.properties. However the contents of catalina.out is quite small.
Do you have any ideas how to completely override any log4j.properties files from all webapps?
Thank you very much for any help and have a nice day!

Related

Disable log4j console logging in linux

I have my log4j.properties file as -
# Root logger option
log4j.rootLogger=WARN, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/logging.log
log4j.appender.file.MaxFileSize=10MB
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
Problem is, log messages are also getting printed on console. How can I disable that? I have read some of the posts here on stackoverflow but other users were facing issues because they defined the consoleAppender into their file but I haven't defined it in my log4j.properties file and still it displays it on console.
Please help.
Try doing additivity false. like below in your property file.
log4j.additivity=false
log4j.rootLogger=ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %5p %c (%F:%L) - %m%n
#hide the Log4jMemoryAppender from console
log4j.logger.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender= ERROR, MEMORY_APPENDER
log4j.appender.MEMORY_APPENDER=nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender.Log4jMemoryAppender
log4j.additivity.rootLogger = false
log4j.additivity.console = false
log4j.additivity.MEMORY_APPENDER=false

Log4j not send logs to linux SYSLOG

I want implement log4j to my java project. I put the jar in lib folder and configure log4j.properties file. Logs successfuly shows in console, but I can't see that logs in SYSLOG app (KSYSLOG) of my kubuntu. this is my properties file:
log4j.rootLogger=INFO, stdout, SYSLOG, file
# Redirect log messages to console
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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a SYSLOG
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost=127.0.0.1
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.conversionPattern=%d{ISO8601} %-5p [%t] %c{2} %x - %m%n
log4j.appender.SYSLOG.Facility=LOCAL1
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/ log4j-application.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
what is incorrect in this properties file or what I do incorrect?
I found the solutin in this thread Writing log data to syslog using log4j
we must add
$ModLoad imudp
$UDPServerRun 514
to rsyslog.conf and restart rsyslog

Tomcat System.out redirection

We have got a legacy Java application, running on Tomcat. It logs its events in somewhat unconsistent way, sometimes even relying on System.out/err. These stdout/stderr records end up in catalina.log where they are mixed with "proper logged" events. We would like to separate the stdout/stderr log streams into separate files (and eventually get rid of them) but we don't know how to do it. The server runs default JULI configuration but we are open to any other option (such as SLF4J + Logback).
This explains it in full: Log4J Just put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application. Create a file called log4j.properties with the following content and save it into $CATALINA_BASE/lib
Example of log4j.properties that would cease output to stdout:
log4j.rootLogger=INFO, yourapp
#Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
#Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
#Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\INFO, HOST-MANAGER
#You can define your Applog here:
log4j.appender.yourapp=org.apache.log4j.RollingFileAppender
log4j.appender.yourapp.File=${catalina.base}/logs/yourapp.log
log4j.appender.yourapp.MaxFileSize=100MB
log4j.appender.yourapp.MaxBackupIndex=10
log4j.appender.yourapp.layout=org.apache.log4j.PatternLayout
log4j.appender.yourapp.layout.ConversionPattern=%d %p [%c] (%t) - <%m>%n
Hope this helps!

Issue with logging on Openshift jboss using log4j

i am having a couple of issues regarding logging using OpenShift with log4j on jboss.
I am using a log4j properties file as follows but seem to be getting no log messages in the file. I have the log4j properties file in the following locations in my app;
/src/log4j.properties
Deployed Resources/WEB-INF/log4j.properties
WebContent/WEB-INF/log4j.properties
Is there anything specific i need to do differently for JBoss ?
Thanks
log=/var/lib/openshift/52c0260d4382ec7329000120/app-root/runtime/dependencies/jbossas/deployments/Logs/HotOrNotLog.log
log4j.rootLogger=default.file.debug
# System.out.println appender
log4j.appender.default.out=org.apache.log4j.ConsoleAppender
log4j.appender.default.out.threshold=INFO
log4j.appender.default.out.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.default.out.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# File appender for DEBUG
log4j.appender.default.file.debug=org.apache.log4j.FileAppender
log4j.appender.default.file.debug.append=true
log4j.appender.default.file.debug.file=${log}
log4j.appender.default.file.debug.threshold=DEBUG
log4j.appender.default.file.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.debug.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# File appender for INFO log4j.appender.default.file.info=org.apache.log4j.FileAppender
log4j.appender.default.file.info.append=true
log4j.appender.default.file.info.file=${log}
log4j.appender.default.file.info.threshold=INFO
log4j.appender.default.file.info.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.info.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# File appender for WARN log4j.appender.default.file.warn=org.apache.log4j.FileAppender
log4j.appender.default.file.warn.append=true
log4j.appender.default.file.warn.file=${log}
log4j.appender.default.file.warn.threshold=WARN
log4j.appender.default.file.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.warn.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# File appender for ERROR
log4j.appender.default.file.error=org.apache.log4j.FileAppender
log4j.appender.default.file.error.append=true
log4j.appender.default.file.error.file=${log}
log4j.appender.default.file.error.threshold=ERROR
log4j.appender.default.file.error.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.error.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# File appender for FATAL
log4j.appender.default.file.fatal=org.apache.log4j.FileAppender
log4j.appender.default.file.fatal.append=true
log4j.appender.default.file.fatal.file=${log}
log4j.appender.default.file.fatal.threshold=FATAL
log4j.appender.default.file.fatal.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.fatal.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
EDIT
having looked in the jboss server logs (server.log in /var/lib/openshift/52c0260d4382ec7329000120/jbossas/logs) i can see the following
2014/03/31 17:29:58,393 ERROR [stderr] (MSC service thread 1-1) log4j:WARN No appenders could be found for logger (com.hotornot.database.DBFactory).
2014/03/31 17:29:58,425 ERROR [stderr] (MSC service thread 1-1) log4j:WARN Please initialize the log4j system properly.
2014/03/31 17:29:58,426 ERROR [stderr] (MSC service thread 1-1) log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
however stack overflow suggests the properties file isn't in the class path - but it is in source as well as two other places lol
Two things that jump out.
None of the following are on the classpath:
/src/log4j.properties
Deployed Resources/WEB-INF/log4j.properties
WebContent/WEB-INF/log4j.properties
Try WEB-INF/classes within your web application (depending on your build having it in /src should copy it over there).
Your first line is log4j.rootLogger=default.file.debug. Shouldn't this be log4j.rootLogger=DEBUG, default.file.debug? It should be a level, then a comma, then an appender.
Do you need all those appenders, anyway? Wouldn't this be the same thing:
log4j.rootLogger=DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.append=true
log4j.appender.FILE.file=${log}
log4j.appender.FILE.threshold=DEBUG
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
I needed to put log4 props in the src/resources folder - its a maven issue as it appears it has a standard dir it checks for log4j props
I don't know how it is in later versions, in JBoss 4 there is a jboss-log4j.xml and i'd say that is used by default. You could check that file..

Log messages except from a particular package to a file

I am using log4j for logging , my requirement is to log all the logs from package
pkg1 (except pkg1.pkg2) to pkg1.log
and pkg1.pkg2 (a sub package of the above package) to pkg2.log
is this possible ? I know i can achieve it through custom appenders but is there a way I can do it through configurations.
I am using the below properties file. Here the problem is PKG1.log contains logs from PKG2 also.
# Root logger option
log4j.rootLogger=WARN, CONSOLE
# Direct log messages to a log file
log4j.logger.com.pkg1=DEBUG, PKG1 log4j.appender.PKG1=org.apache.log4j.RollingFileAppender
log4j.appender.PKG1.File=logs/PKG1.log
log4j.appender.PKG1.MaxFileSize=1MB
log4j.appender.PKG1.MaxBackupIndex=1
log4j.appender.PKG1.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.com.pkg1.pkg2=DEBUG, PKG2
log4j.appender.PKG2=org.apache.log4j.RollingFileAppender
log4j.appender.PKG2.File=logs/PKG2.log
log4j.appender.PKG2.MaxFileSize=1MB
log4j.appender.PKG2.MaxBackupIndex=1
log4j.appender.PKG2.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
You can stop PKG2's logs from reaching it's ancestor's appenders by disabling the additivity flag in the configuration:
log4j.appender.PKG2.additivity=false

Categories

Resources