Configure common log4j xml for multiple sub projects - log4j

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

Related

How wil get Context Path in log4j property files?

How wil get Context Path in log4j property files? Ex: My log file name would be contextpath.log.
You will have to create a different appender for each context path. And Implement each context in a different Controller class.
for example
<appender name="FILE-1" class="org.apache.log4j.FileAppender">
<param name="File" value="contextpath1.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
<appender name="FILE-2" class="org.apache.log4j.FileAppender">
<param name="File" value="contextpath2.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
<logger name="com.mycompany.apackage.Context1Class">
<level value="info"/>
<appender-ref ref="FILE-1"/>
</logger>
<logger name="com.mycompany.apackage.Context2Class">
<level value="info"/>
<appender-ref ref="FILE-2"/>
</logger>

log4j AsyncAppender doesn't show line number

I have a old project which use log4j(not log4j2),it will print file name and line number.
2017-10-21 17:08:54,198 INFO [com.fudy.log4j.PerformanceLogger.info:11
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c.%M:%L] : %m%n" />
</layout>
</appender>
after I add AsyncAppender to log4j.xml
<logger name="com.fudy.log4j.PerformanceLogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="async" />
</logger>
it print ? instead of method name and linenumber:
2017-10-21 17:24:17,909 INFO [com.fudy.log4j.PerformanceLogger.?:?
after googled, all solution is to add includeLocation in log4j2, is there any solution for logj4 1.x
I found the solution for log4j 1.x
<appender name="async" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="256"/>
<param name="LocationInfo" value="true"/> <!-- add this one -->
<appender-ref ref="console"/>
</appender>

log4j dont want write to consol and file with system variable in path

I have a problem.
Log4j dont want write to console and to file with system variable in path. Only write to file with simple path.
Configuration of my log4j.
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<!-- <param name="file" value="d:/lv-098_JAVA/logs/log.log" /> -->
<param name="file" value="${LV098_JAVA}/src/main/resources/logs/log.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p (%F:%L:%M) %c{1} %m%n" />
</layout>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="file1" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
You are declaring
appender name="file" class="org.apache.log4j.DailyRollingFileAppender"
and in root tag you write
appender-ref ref="file1"
As there is no such file1. Due to this log4j is not working for you.
I found where problem is. It is in Jboss, because jboss have his own logger and it doesn't allow to write to console. You must change configuration of Jboss logger. But I don't know why it does not allow to use system variable.

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

Using RollingFileAppender ,DailyRollingFileAppender,ConsoleAppender in log4j. Not working

My requirement is to log the messages to 3 separate log files.
I am using 3 appenders in a JBOSS server.
Three of them need to work in the way such as
1.RollingFileAppender, consoleAppender work for 'INFO' and 'ERROR' modes.
2.DailyRollingFileAppender for 'ERROR' mode and for a specific category only.
I have tried lots of combinations like changing the priority value and the categories, but I have so far found no way in which all the three work simulatneously. Either fileAppender and ConsoleAppender work(logging both ERRROR and INFO) and the DailyRollingFileAppender does not or DailyRollingFileAppender does but the other two log only ERRORS not INFO.
Please tell me a way so that the three of them work.
Below mentioned is the log4j.xml I am using.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- ===================================================================== -->
<!-- -->
<!-- Log4j Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- -->
<!--
| For more configuration infromation and examples see the Jakarta Log4j
| owebsite: http://jakarta.apache.org/log4j
-->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- file appender -->
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:/logs/mdmWSServer.log" />
<param name="MaxBackupIndex" value="5" />
<param name="MaxFileSize" value="5MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%t] [%-5p][ %c:] %m%n"/>
</layout>
</appender>
<appender name="dailyFileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:/logs/dailyApp.log"/>
<param name="MaxBackupIndex" value="5" />
<param name="MaxFileSize" value="5MB" />
<param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%t] [%-5p][%c:] %m%n"/>
</layout>
</appender>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%t] %m%n"/>
</layout>
</appender>
<category name="com">
<priority value="INFO"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="consoleAppender"/>
</category>
<category name="com.gsk">
<priority value="ERROR"/>
<appender-ref ref="dailyFileAppender"/>
</category>
<category name="org.apache">
<priority value="INFO"/>
<appender-ref ref="fileAppender"/>
</category>
<root>
<priority value="DEBUG"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>
You can restrict an appender to only log a certain level by using filter class specifying which level you would want to log . See the sample code below
<appender name="TRACE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %-5p %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="DEBUG" />
<param name="levelMax" value="DEBUG" />
</filter>
</appender>

Resources