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!
Related
I am using slf4j logging library and have provide logging pattern in log4j.properties file under resources folder in my maven project but still the pattern I have provided does not take affect and is printed with default logging pattern.
i am using slf4j logging library
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
I am initializing the logger by
private static final Logger logger = LoggerFactory.getLogger(CurrentClassName.class);
My log4j.properties file content is
# Direct log messages to stdout
log4j.rootLogger=DEBUG, STDOUT, file
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=%5p[] [%t] (%F:%L) - %m%n
log4j.appender.stdout.layout.ConversionPattern="%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%-30logger{20}] [${application.name}] [%X{request_id}] - %msg%n"
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylogs.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.layout.ConversionPattern="%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%-30logger{20}] [${application.name}] [%X{request_id}] - %msg%n"
ACTUAL LOG OUTPUT
11:07:45.091 [main] DEBUG com.abc.def.LoggingClassName - message logged from within the class
Expected LOG OUTPUT
2019-07-17 12:03:59 [INFO ] [c.b.CurrentLoggingClassName ] [aplication-name] [] - message logged from class
P.S.
I have also tried renaming the log4j.properties file to log4j2.properties
And I have also tried both properties file placing under /src folder one by one
try using this
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
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!
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
I have discovered some issues in my program.
I was using log4j for logging,
however, inside the log file, all line number become "?".
The conversation pattern is as follow:
log4j.appender.file.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS} %5p %c: %L - %m%n
You are most probably missing debug information in your compiled artifacts.
I.e. it is not log4j's fault, you need to make sure you are compiling your stuff with debug information included.
A quick test would be to try and debug your app with your favourite IDE.
If there is no debug info it will complain and won't establish debug session...
Set the options debug="true" debuglevel="lines,source" in your javac ant-task.
Check once.
i am using your pattern i my code,
and it works better....this is a log4j.properties file.
log4j.rootLogger=DEBUG
# AdminFileAppender - used to log messages in the admin.log file.
log4j.appender.AdminFileAppender=org.apache.log4j.FileAppender
log4j.appender.AdminFileAppender.File=admin.log
log4j.appender.AdminFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ReportFileAppender.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS} %5p %c: %L - %m%n
#log4j.appender.AdminFileAppender.layout.ConversionPattern=%L - %r %-5p %c %x - %m - %d - %t%n
# ReportFileAppender - used to log messages in the report.log file.
log4j.appender.ReportFileAppender=org.apache.log4j.FileAppender
log4j.appender.ReportFileAppender.File=report.log
log4j.appender.ReportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ReportFileAppender.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS} %5p %c: %L - %m%n
#log4j.appender.ReportFileAppender.layout.ConversionPattern=%L - %r %-5p %c %x - %m - %d- %t%n
log4j.logger.com.vaannila.admin=,AdminFileAppender
log4j.logger.com.vaannila.report=,ReportFileAppender
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