Log4j separate Log files [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating multiple log files of different content with log4j
I want to store Debug logs and info logs in separate files can anyone give me the sample.
log4j.rootLogger=debug,R1,R2
log4j.appender.R2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R2.File=/Test/ex/ex.log
log4j.appender.R1.DatePattern='.'dd-MM-yy
log4j.appender.R2.layout=org.apache.log4j.PatternLayout
log4j.appender.R2.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m%n
When I try the above, debug and info logs are storing in sample file. But how can I store separately.

The key is to use a LevelMatchFilter provided by log4j.
Here an updated version of your configuration
log4j.rootLogger=debug,R1,R2
log4j.appender.R1.DatePattern='.'dd-MM-yy
log4j.appender.R1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R1.File=/Test/error.log
log4j.appender.R1.layout=org.apache.log4j.PatternLayout
log4j.appender.R1.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m
log4j.appender.R1.filter.1=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.R1.filter.1.LevelToMatch=ERROR
log4j.appender.R1.filter.1.AcceptOnMatch=true
log4j.appender.R1.filter.2=org.apache.log4j.varia.DenyAllFilter
log4j.appender.R2.DatePattern='.'dd-MM-yy
log4j.appender.R2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R2.File=/Test/info.log
log4j.appender.R2.layout=org.apache.log4j.PatternLayout
log4j.appender.R2.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m
log4j.appender.R2.filter.1=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.R2.filter.1.LevelToMatch=INFO
log4j.appender.R2.filter.1.AcceptOnMatch=true
log4j.appender.R2.filter.2=org.apache.log4j.varia.DenyAllFilter
log4j.appender.debug.DatePattern='.'dd-MM-yy
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File=/Test/debug.log
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m
log4j.appender.debug.filter.1=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.debug.filter.1.LevelToMatch=DEBUG
log4j.appender.debug.filter.1.AcceptOnMatch=true
log4j.appender.debug.filter.2=org.apache.log4j.varia.DenyAllFilter
References :
PropertyConfiguration
LevelMatchFilter

Related

How to create multiple log files using log4j

I want to create separate log file
one for info another for debug,
I am using below log4j.property file,
Please suggest how to modify for two level logging in different file,
# Root logger option
log4j.rootLogger=info, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=infoLog.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} - %m%n
log4j.rootCategory=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} %-5p %c{1} - %m%n
log4j.appender.console.Target=System.out
I would like to request Please send me updated property file rather than giving pointer for search or explore, because I have already spent 3-4 Hours on searching this solution in Google and StackOverflow,
Thanks in advance.
I hope this will help
log4j.rootLogger=INFO,Appender2
log4j.logger.infoMessagesLogger=INFO, infoMessagesAppender
log4j.additivity.infoMessagesLogger=false
log4j.logger.debugMessagesAppender=DEBUG, debugAppenderAll
log4j.additivity.debugMessagesAppender=false
log4j.appender.Appender2=org.apache.log4j.RollingFileAppender
log4j.appender.Appender2.File=generic.log
log4j.appender.Appender2.MaxFileSize=20MB
log4j.appender.Appender2.MaxBackupIndex=10
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss } %-5p %c %x -%m%n
log4j.appender.infoMessagesAppender=org.apache.log4j.RollingFileAppender
log4j.appender.infoMessagesAppender.File=info.log
log4j.appender.infoMessagesAppender.MaxFileSize=20MB
log4j.appender.infoMessagesAppender.MaxBackupIndex=10
log4j.appender.infoMessagesAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.infoMessagesAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss } %-5p %c %x -%m%n
log4j.appender.debugAppenderAll=org.apache.log4j.RollingFileAppender
log4j.appender.debugAppenderAll.File=debug.log
log4j.appender.debugAppenderAll.MaxFileSize=20MB
log4j.appender.debugAppenderAll.MaxBackupIndex=10
log4j.appender.debugAppenderAll.layout=org.apache.log4j.PatternLayout
log4j.appender.debugAppenderAll.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss } %-5p %c %x -%m%n

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!

Log4j PatternLayout - Prepend value to %c

My current Conversion Pattern within my log4j.xml file is as follows:
<param name="ConversionPattern" value="%d (%F:%L) %-5p %c - %m%n"/>
I have a simple requirement to prepend %c (which is 'ERROR') with the word 'Runtime'. Using the pattern layout is there anyway I can simply add this word into the pattern for it to print out to the log file?
Thanks in advance.
Just add the string to the pattern:
%d (%F:%L) %-5p Runtime%c - %m%n

Line number not showing?

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

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