log4j: Doesn't reflect settings in log4j.properties - java

I have an Java-SE app with log4j over slf4j.
At some phase of development, it stopped logging DEBUG level, and I can't figure out why.
I already excluded the causes I encountered earlier:
No WARNings
slf4j implementation used is log4j (AFAICT)
log4j.properties at right place (classpath root)
log4j.properties does end up in the .jar
The structure of the app didn't change
No log4j.properties or log4j.xml hidden in some .jar (like Weld-se-core has)
log4j.category. entries match (they didn't change since it worked)
Any ideas?
log4j.rootLogger=DEBUG, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %-5r %-5p [%c] (%t:%x) %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t] %c %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=JawaBot.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t] %c %m%n
log4j.category.org = DEBUG
log4j.category.org.jboss.jawabot = DEBUG
log4j.category.org.jboss.jawabot.irc = DEBUG
log4j.category.org.mortbay.log = WARN
log4j.category.org.apache.wicket = INFO
log4j.category.org.jboss.weld = DEBUG
#log4j.category.org.jboss.weld.bootstrap = DEBUG
log4j.category.org.jboss.weld.environment.se.jpa = DEBUG

Related

log4j: WARN No such property [conversationPattern] in org.apache.log4j.PatternLayout

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

Why do I get DEBUG level logging in console?

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.

Log4j does not write into file and cannot disable spring log

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

log4j, need to display only INFO and ERROR message in log file

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

Tomcat Save Log History

I am using log4j in webapp deployed on tomcat, but cuurently my logs comes under catalina.log file, but only error logs remain in history, i want my all logs to be remain in history , additionally it will be good if if i get logs datewise.
my logs4j looks like this
log4j.rootLogger=INFO, myConsoleAppender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern= [%t] %d{dd MMM yyyy HH:mm:ss,SSS} : %c %x : %m%n
i am only using info level of logs.
Try this
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
# Define the daily rolling file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=/logs/app.log
log4j.appender.FILE.DatePattern='.'yyyy-MMM-dd
# Define the layout for the file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern= %-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
Above will create a log file on the tomcat installation directory under the folder logson daily basis. You can change the log file path by modifying the log4j.appender.FILE.File=/path/to/logfile

Categories