Log4J - Logger don't work - log4j

I use Log4J for an Java Application.
Please find an extract of my log4j.xml file next:
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<logger name="my.package.name">
<priority value="debug" />
</logger>
I want to print each log with a info level and debug logs for my package my.package.name.
But, these debug logs don't appear... :(
Can someone help me ?

Change
<param name="Threshold" value="INFO"/>
to
<param name="Threshold" value="debug"/>
Since you have placed Threshold to INFO only info and above will logged . Debug level is below info level. So that is why debug levels are not logged.
log4j hierarchy is TRACE Level < DEBUG Level< INFO Level< WARN Level < ERROR Level < FATAL Level.
Hope this helps

Related

Configure common log4j xml for multiple sub projects

I am trying to use a common log4j xml for subprojects in tomcat. There is a Parent project deployed already and part of the parent project are three other projects. Two projects A and B already exist and the logging works fine. I am adding a new project C and updated the log4j like below. I do see the ProjectC.log file being created (which is happening when tomcat starts up), but there are no Project C related log statements in this file (or any other file). This is my current log4j xml :
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
<appender name="rootAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="ProjectAAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/projectA.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="100000KB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<appender name="ProjectBAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/ProjectB.csv"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout"/>
</appender>
<appender name="ProjectCAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/ProjectC.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout"/>
</appender>
<logger name="projA" additivity="true">
<level value ="DEBUG" />
<appender-ref ref="ProjectAAppender"/>
</logger>
<logger name="projA.Performance" additivity="true">
<level value ="INFO" />
<appender-ref ref="ProjectBAppender"/>
</logger>
<logger name="projC" additivity="true">
<level value ="DEBUG" />
<appender-ref ref="ProjectCAppender"/>
</logger>
<root>
<priority value ="INFO" />
<appender-ref ref="rootAppender"/>
</root>
The way I get my log4j instance is using the slf4j LoggerFactory :
LoggerFactory.getLogger(clazz)
I have declared dependencies for log4j(1.2.14), slf4j-log4j12(1.4.1) jar files in my pom.
This setup works fine when I execute Project C independently (when running junit test cases).
How can I make logging work for project C ? Any changes that I should be making to my log4j xml ? Thank you.
Turns out I was using the wrong properties file. In the actual log4j.properties file all I had to do was to create a new appender named Project C and added this line instead of registering with the rootCategory :
log4j.logger.com.projectC.related.package=DEBUG, ProjectCAppender

perf4j provide wrong Count parameter

I use perf4j in a a multi thread request processor application with high request rate that is deployed on jboss app server.
I use it with log4j and AsyncCoalescingStatisticsAppender for providing run-time statistics data. this is partial log4j.xml file:
<appender name="timeFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:\\JavaEE\\JCATestLog\\TimeInfo.log"/>
<param name = "MaxFileSize" value = "15000KB"/>
<param name = "MaxBackupIndex" value = "10" />
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="CoalescingStatistics"
class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="10000"/>
<appender-ref ref="timeFileAppender"/>
</appender>
<logger name="org.perf4j.TimingLogger" additivity="false">
<level value="INFO"/>
<appender-ref ref="CoalescingStatistics"/>
</logger>
MY problem is that the Count parameter in result, is not equal to exact no of requests. for example in a test, I send 10000 request, but Count parameter that is written to log file is 9560.
Is there any guide?
I found the answer. The problem was queueSize of AsyncCoalescingStatisticsAppender. Because high rate of requests in my app, the queue capacity was insufficient.
We can set queue size in log4j.xml as the following:
<appender name="CoalescingStatistics"
class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="10000"/>
<param name="QueueSize" value="100000"/>
<appender-ref ref="timeFileAppender"/>
</appender>

log4j:ERROR Failed to rename

I get this error "log4j:ERROR Failed to rename" in my tomcat server log.
I have multiple DailyRollingFileAppender in my log4j XML. And each appender points to different file. Only once instance of the application is running.
I used Handle utility and run the command (with log file name as parameter)
handle -f ams-app.log
and the result is
Tomcat7.exe pid: 5032 type: File 54C: E:\apps\ams\logs\ams-app.log
From this I assume there is only one lock on the file.
My environment Windows Server 2008, tomcat 7.0 and Java 6 and slf4j-log4j12(1.5.6)
This problem was not happening when same application hosted on AIX server.
I googled this problem and it seems to be windows specfic.
Also I am not keen to use DatedFileAppenders or any forked log4j.
Does logback solve such issues.
Any suggestions on how to solve this problem is appreciated.
Thanks
here my log4j xml
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE_APPENDER" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<appender name="APP_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="datePattern" value="yyyy-MM-dd"/>
<param name="file" value="E:/apps/ams/logs/ams-app.log"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<appender name="SQL_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="datePattern" value="yyyy-MM-dd"/>
<param name="file" value="E:/apps/ams/logs/ams-sql.log"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<appender name="Quartz_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="datePattern" value="yyyy-MM-dd"/>
<param name="file" value="E:/apps/ams/logs/ams-Quartz.log"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<logger name="org.springframework"><level value="WARN"/></logger>
<logger name="flex.messaging.log.ServletLogTarget"><level value="WARN" /></logger>
<logger name="net.sf.jasperreports"><level value="WARN" /></logger>
<logger name="net.sf.jasperreports.engine.fill"><level value="WARN" /></logger>
<logger name="org.apache.commons.beanutils"><level value="WARN" /></logger>
<logger name="jdbc.sqltiming" additivity="false"><level value="DEBUG"/><appender-ref ref="SQL_APPENDER"/></logger>
<logger name="org.quartz" additivity="false"><level value="WARN" /><appender-ref ref="Quartz_APPENDER"/></logger>
<logger name="org.quartz.SchedulerException"><level value="ERROR" /><appender-ref ref="MAIL_APPENDER"/></logger>
<logger name="org.hmil.ams.BatchDAO" additivity="false"><level value="INFO"/><appender-ref ref="Quartz_APPENDER"/></logger>
<root>
<priority value="debug"></priority>
<appender-ref ref="APP_APPENDER" />
<appender-ref ref="CONSOLE_APPENDER"/>
</root>
It appears that if you're running windows this is a known bug:
see here:
http://do.whileloop.org/2014/02/14/log4j-rolling-file-appenders-in-windows/
See here:
https://issues.apache.org/bugzilla/show_bug.cgi?id=29726
And also here:
http://www.coderanch.com/t/424837/java/java/Log-log-file-rolled-day
I had the same issue and fixed it by updating log4j to version 1.2.15 as per this discussion

Log4j - priority value and param name concept explanation

My log4j.xml:
<appender name="B2BAPP" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/LOGS/SAM/B2B_VJ.log"/>
<param name="Threshold" value="ERROR"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="10"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss,SSS} %5p [%c:%L] %m%n"/>
</layout>
</appender>
<logger name="com.sas">
<priority value="DEBUG"/>
<appender-ref ref="B2BAPP"/>
</logger>
I would like to understand the behaviour of priority value="DEBUG" and param name="Threshold" value="DEBUG".
In my logger (com.sas) I have set the priority value "DEBUG" and appender of this logger is "B2BAPP" and in "B2BAPP" I have defined "Threshold" as "ERROR".
So log level for "com.sas" would be set to "DEBUG" or "ERROR"?
Cases :
priority value="DEBUG" and param name="Threshold" value="ERROR"
priority value="ERROR" and param name="Threshold" value="DEBUG"
What would be the output of the above cases? How does it work?
The Logger component accepts logging instructions (logger.debug(), logger.error() etc calls) and sends them to appropriate destinations to the Appenders.
You can set a "priority" on the Logger and instruct it to accept only logging instructions of a certain level. The levels are (in ascending importance order): TRACE, DEBUG, INFO, WARN, ERROR and FATAL.
A configuration like this:
<logger name="com.sas">
<priority value="WARN" />
....
</logger>
instructs the com.sas logger to only accept levels with a level of importance of WARN or higher (i.e. WARN, ERROR and FATAL).
The logging statements are then sent to Appenders. The appenders can also be configured to accept only statements of a certain importance level, one above a certain "threshold".
A configuration like:
<appender name="B2BAPP" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ERROR"/>
....
</appender>
tells the appender to only accept statements of ERROR importance or above (i.e. ERROR and FATAL).
So log level for "com.sas" would be set to "DEBUG" or "ERROR"?
In your example the log level is set to DEBUG. What gets written by the appenders is orthogonal to the issue.
As for your two examples:
priority value="DEBUG" and param name="Threshold" value="ERROR"
priority value="ERROR" and param name="Threshold" value="DEBUG"
1. Logger priority set to DEBUG and appender threshold set to ERROR means that the logger passes along DEBUG, INFO, WARN, ERROR and FATAL but appender only accepts ERROR and FATAL so you get only ERROR and FATAL into your log.
2. Logger priority set to ERROR and appender threshold set to DEBUG means that the logger passes along only ERROR and FATAL while the appender accepts DEBUG, INFO, WARN, ERROR and FATAL. You again get only ERROR and FATAL into your log.
But that's just an unfortunate case. Mixing the priority and the threshold can get you some nice functionality. For example...
... assume you just placed an application in staging and you need to monitor it for a bit until you move it to production. You have a developer and a system administrator doing the monitoring. While the developer want all the logs, the system administrator is busy and only wants to see the errors.
How do you configure that? How about something like this:
<appender name="developerLogs" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/LOGS/SAM/developerLogs.log" />
<param name="Threshold" value="DEBUG" />
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<appender name="sysAdminLogs" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/LOGS/SAM/sysAdminLogs.log" />
<param name="Threshold" value="ERROR" />
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="com.test">
<priority value="DEBUG" />
<appender-ref ref="developerLogs" />
<appender-ref ref="sysAdminLogs" />
</logger>
If you run code like:
Logger logger = Logger.getLogger("com.test");
logger.debug("some debug statement");
logger.info("some info statement");
logger.warn("some warn statement");
logger.error("some error statement");
logger.fatal("some fatal statement");
you get this in sysAdminLogs.log:
some error statement
some fatal statement
and this in developerLogs.log:
some debug statement
some info statement
some warn statement
some error statement
some fatal statement
Hope this explanation better describes the concepts.

How do I configure log4j using log4j.xml to append to different log files based on class name?

I want to set up log4j so that all log meessages produced from classes under package com.foo.bar go to bar.log, and all the log meessages produced from classes under package com.bar.blatz go to blatz.log.
Questions
How do I do this using log4j.xml?
I know its possible using property files, but how do I do it using the XML configuration?
This is based on my answer to a similar question:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="BarLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="bar.log" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<!-- additional fooSystem logging -->
<appender name="BlatzLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="blatz.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<logger name="com.foo.bar">
<appender-ref ref="BarLogFile"/>
</logger>
<logger name="com.bar.blatz">
<appender-ref ref="BlatzLogFile"/>
</logger>
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
</log4j:configuration>
If you add an appender-ref to the root element, it will also receive com.foo.bar etc messages. You can stop that by specifying 'additivity="false"' on the loggers.
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
We can add appenders here. It will work if the application is using root logger. for example quartz Scheduler API.

Resources