Log messages except from a particular package to a file - java

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

Related

Override all log4j.properties for webapps in tomcat

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!

Java Logging - Debug Level log not appeared in the eclipse console

For debugging my application, trying to display debug level logs in the eclipse console with the below configuration. But only INFO, ERROR and FATAL are displayed, but not the DEBUG level logs.
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=D:/log/oms-web.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} [%t] %p %c - %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the date/time, calling class and message.
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %p %C.%M(): %m%n
# Root logger option
log4j.rootCategory=INFO ,file,stdout,DEBUG
log4j.category.org.springframework.beans.factory=OFF
# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=OFF
# Log all JDBC parameters
log4j.logger.org.hibernate.type=OFF
log4j.logger.org.springframework=OFF
Please help me to resolve.
Change the rootCategory to:
log4j.rootCategory=DEBUG ,file,stdout

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

Disable log4j console logging and enable file logging

My log4j.properties file is:
log4j.rootLogger=WARN, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${jboss.server.log.dir}/afrodite.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
log4j.logger.info.release.scheduler=INFO, scheduler
log4j.appender.scheduler=org.apache.log4j.RollingFileAppender
log4j.appender.scheduler.File=${jboss.server.log.dir}/afrodite_scheduler.log
log4j.appender.scheduler.MaxFileSize=10MB
log4j.appender.scheduler.MaxBackupIndex=10
log4j.appender.scheduler.Append=true
log4j.appender.scheduler.layout=org.apache.log4j.PatternLayout
log4j.appender.scheduler.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
If I want to disable console output for scheduler logger what should I do? That means scheduler logger only log into file not in console.
Thanks and Regards.
Add this line to your config:
log4j.appender.scheduler.additivity=false
log4j.additivity.info.release.scheduler=false
(sorry, first hint was incorrect.)
you can add two lines in your properties file.
log4j.rootLogger=ALL, stdout, file,scheduler (#here ALL declare for all level of logs)
log4j.logger.info.release.scheduler=INFO, scheduler
log4j.additivity.info.release.scheduler=false

Categories

Resources