Tried to find an answers in other questions but nothing helped.
While running my program the log4j sent at the beginning:
log4j:WARN No such property [conversationPattern] in org.apache.log4j.PatternLayout.
This is my log4j.properties file:
# 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=C:\\Users\\xxxcx\\Desktop\\automationTest.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversationPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p
%c{1}:%L - %m%n
I don't understand how to solve it because i can find whats the problem with my conversationPattern property.
Any help would be great!
For log4j 1 it is ConversionPattern and not ConversationPattern. That is simply change your pattern config to this:
log4j.appender.file.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p
I think the property for log4j2 is simply called pattern and not ConversationPattern:
appender.file.layout.pattern=%d{yyyy/MM/dd HH:mm:ss} %-5p
Related
I need to create a new log file once a day, or if log file size become 250 MB. So how can i do this through log4j.properties file? Dividing by day is working, but if size become more than 250MB nothing happens.
My current property file is:
# Root logger option
log4j.rootLogger=INFO,console, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.encoding=UTF-8
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000
log4j.appender.rollingFile.MaxFileSize=10MB
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
# configuration to print on 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
As per the following log4j link https://logging.apache.org/log4php/docs/appenders/rolling-file.html, you have to add the following lines.
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000
Note:
File Appender should be RollingFileAppender
Triggering Policy should be SizeBasedTriggeringPolicy
EDIT:
# Direct log messages to a log filelog4j.appender.file.encoding=UTF-8
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.File=${LOG_DIR}/application.log
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.MaxFileSize=250MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.append=true
You can refer complete example below.
https://howtodoinjava.com/log4j/log4j-rolling-file-appender/
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.
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
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