I have a log4j properties file defined like so:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
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
log4j.logger.com.github.user=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=target/cucumber-parser.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
Based on my understanding, INFO level logging should be routed to stdout (console) and DEBUG should be only written to file. However I get both, INFO and DEBUG level in console. What am I doing wrong?
I managed to solve my problem by applying the below line to log4j.appender.stdout:
log4j.appender.stdout.Threshold=INFO
With this line added to the original configuration that I posted in my original question, only the INFO level logs will be printed to the console and DEBUG will be rerouted to a file as desired.
Because INFO level covers DEBUG level. So that you see both.
See more Logging Levels
If you check log4j doc or this tutorial you can see more information about levels, but basically: ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
So log shows the selected level and all the greaters.
add log4j.additivity.com.github.user=false.
As in your config, level of com.github.user is being set to debug. And besides "file", it will also be additive to root logger, so you can see debug log of com.github.user in the console.
In fact, if you write the config this way, splitting loggers and appenders, you can understand better.
log4j.rootLogger=INFO, stdout
log4j.logger.com.github.user=DEBUG, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
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
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=target/cucumber-parser.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
For loggers, you specified com.github.user to be debug, and all others info with rootlogger setting.
Then com.github.user will be printed to file, and all logs will be printed to console . And if you set the additivity, com.github.user will be removed from rootlogger, so there will only be other logs in console.
Related
I have the following issue:
I have an enviroment with apache tomcat 7.0.72 and I have three logs, cmfront, cmback and catalina.out.
The configuration of both (cmfront and cmback) is the same, cmfront:
# Root logger option
log4j.rootLogger=OFF
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/CmFront.log
log4j.appender.file.MaxFileSize=20MB
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
# Direct log messages to stdout
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
log4j.logger.CmFront=INFO,file,stdout
and cmback:
# Root logger option
log4j.rootLogger=OFF
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/CmBack.log
log4j.appender.file.MaxFileSize=20MB
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
# Direct log messages to stdout
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
log4j.logger.CmBack=INFO,file,stdout
log4j.logger.o=OFF
Nevertheless, when I deploy cmfront war, I deploy without any problem, but when I try whit cmback, my file does not write anyhting and also has the following logging lines:
16:08:28.768 [localhost-startStop-1] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
16:08:28.768 [localhost-startStop-1] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
And a lot of this kind of lines, my question is why in one war the logging properties works and does not print o.s.... debug lines, and by another hand the logs does not is written and also print a lot of debug lines if the configuration is the same?
the properties are not application specific but spring related properties
if you have configured any spring bean related to mbean or any other , i think you can ignore those as long as the application is not affecting.
As far as the logging,
try the below
log4j.rootLogger=INFO,file,stdout
y r u setting root logger off, as you are not having any specific logger defined like com.****.com all your logs should go through the root logger.
For logs to work you should enable the root logger.
This is my guess might not be exact answer
I am new to log4j and worked to setup on eclipse and its running. I understood the chain of priority in the levels and this is my properties file config:
log4j.rootLogger=ALL, file, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
My question is
Is it possible to print only INFO and ERROR type of messages in log files with properties file configuration
Finally I got the solution for my requirement. Here I used concept of filters i.e LevelRangeFilter. And for every logging level we defined appender.
log4j.rootLogger=DEBUG, file, console, file1
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file.filter.a.LevelMin=INFO
log4j.appender.file.filter.a.LevelMax=INFO
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.file1=org.apache.log4j.RollingFileAppender
log4j.appender.file1.File=logs/justfortesting.log
log4j.appender.file1.Append=true
log4j.appender.file1.ImmediateFlush=true
log4j.appender.file1.Threshold=DEBUG
log4j.appender.file1.filter.g=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file1.filter.g.LevelMin=ERROR
log4j.appender.file1.filter.g.LevelMax=ERROR
log4j.appender.file1.MaxFileSize=1KB
log4j.appender.file1.MaxBackupIndex=2
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.logger.com.log4j=DEBUG, file, console, file1
log4j.additivity.com.log4j=false
If any one suggest to reduce the above properties configuration for same requirement,, please comment.
Try
log4j.appender.file.Threshold=INFO
Also see related SO question and documentation
There is a way to print different layout for different log level? For example:
logger.warn("Message");
print something like this: 2016-06-20 13:34:41,245 INFO (main:) Message
and for logger.info("Message2");
print just: Message2
Is possible to do that? To define one layout to warn e other layout for info
log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log4j.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%m%n %d %-5p [%c] (%t:%x) %m%n
# Direct log messages to stdout
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 %-5p [%c] (%t:%x) %m%n
I haven't tested but it should work. If you define two appenders and you assign a pattern for each appenders. Then the keyword Threshold should filter the level
:
# Root logger option
log4j.rootLogger=INFO, fileWarning, fileInfo, stdout
log4j.appender.fileWarning=org.apache.log4j.RollingFileAppender
log4j.appender.fileWarning.File=log4j.log
log4j.appender.fileWarning.MaxFileSize=10MB
log4j.appender.fileWarning.MaxBackupIndex=10
log4j.appender.fileWarning.layout=org.apache.log4j.PatternLayout
log4j.appender.fileWarning.layout.ConversionPattern=%m%n %d %-5p [%c] (%t:%x) %m%n
log4j.appender.fileWarning.Threshold=WARNING
log4j.appender.fileInfo=org.apache.log4j.RollingFileAppender
log4j.appender.fileInfo.File=log4j.log
log4j.appender.fileInfo.MaxFileSize=10MB
log4j.appender.fileInfo.MaxBackupIndex=10
log4j.appender.fileInfo.layout=org.apache.log4j.PatternLayout
log4j.appender.fileInfo.layout.ConversionPattern=%m%n
log4j.appender.fileInfo.Threshold=INFO
Hope it's help.
Everything works just as fine. But showing this error.
My log4j.properties file like :
# Root logger option
log4j.rootLogger=DEBUG, stdout, 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 log file, support file rolling.
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=D:\\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
Thanks in advance. Just tell me how to do it. I just want log file on daily rolling .
DailyRollingFileAppender doesn't support MaxFileSize, RollingFileAppender does.
DailyRollingFileAppender is for rolling files based on the date and time of the log entry, so if you want to use it you should remove the MaxFileSize property.
I changed the code to
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
from
log4j.appender.FILE=org.apache.log4j.FileAppender
and it worked fine in log4j.properties file
I want to redirect all messages with error level to file and messages with info level to console.
Here is my log4j.properties:
log4j.rootLogger=INFO, stdout
log4j.logger.java.lang.Exception=ALL, java.lang.Exception
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
log4j.appender.java.lang.Exception=org.apache.log4j.RollingFileAppender
log4j.appender.java.lang.Exception.File=\\clientexceptionlog.txt
log4j.appender.java.lang.Exception.MaxFileSize=2048KB
log4j.appender.java.lang.Exception.layout=org.apache.log4j.PatternLayout
log4j.appender.java.lang.Exception.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
But seems like it doesn't work as I expect: clientexceptionlog.txt is always empty.
What I'm doing wrong? Thanks!
You may work with the Treshold option. Doing it that way, you can set the rootLogger to both, stdout and your file appender (I named it myAppender in the following code snippet):
log4j.rootLogger=INFO, stdout, myAppender
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
log4j.appender.myAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myAppender.Threshold=WARN
log4j.appender.myAppender.File=\\clientexceptionlog.txt
log4j.appender.myAppender.MaxFileSize=2048KB
log4j.appender.myAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myAppender.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
What does it do? It sends everything with a loglevel INFO or higher to stdout and myAppender. While stdout will output all (including higher loglevels like WARN and ERROR) the myAppender will filter out everything that is lower than WARN (due to log4j.appender.myAppender.Threshold=WARN).
BTW: Don't use java.lang.Exception as name for the appender. That may confuse any one reading this configuration (including me!)