Using RollingFileAppender ,DailyRollingFileAppender,ConsoleAppender in log4j. Not working - log4j

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>

Related

Log4j.Debug=true not priniting anything on weblogic

I am using weblogic 10.3.6 and Iam trying to use log4j1.2 for logging purposes.
I have created log4j.xml in src/main/resources.
But log4j is not creating any file or nor even logging on console.
I tried giving jvm property -Dlog4j.debug=true but even that is not printing anything on console.
Any pointers?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<!-- Console Appender -->
<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 | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</layout>
</appender>
<!-- Info Log File Appender -->
<appender name="info-log" class="org.apache.log4j.FileAppender">
<param name="File" value="C:\\Users\\rajan.d.panchal\\Desktop\\newlog\\info.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="info" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<!-- Error Log File Appender -->
<appender name="error-log" class="org.apache.log4j.FileAppender">
<param name="Append" value="false" />
<param name="File" value="C:\\Users\\abc\\Desktop\\newlog\\error.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="warn" />
<param name="LevelMax" value="fatal" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<logger name="com.jcg.log4j.example" additivity="false">
<appender-ref ref="info-log" />
<appender-ref ref="error-log" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Ok. So I was using JRebel and It was not creating any logs. After disabling JRebel it worked.

File log on centos and jboss?

This is my configuration file of log4j, on Windows works fine but on centos only create the in /tmp/ControllerDebug.log file and nothing is written in it.
I have checked that it isnĀ“t a problem of permissions.
My server is jboss.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >
<appender name="FileDebugAp" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${log.dir}/Controller_Debug.log" />
<param name="MaxFileSize" value="8000KB"/>
<!-- Keep one backup file -->
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p %c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="DEBUG" />
<param name="levelMax" value="FATAL" />
</filter>
</appender>
<appender name="ConsoleErrorAp" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.err"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="ERROR" />
<param name="levelMax" value="FATAL" />
</filter>
</appender>
<logger name="com.optimi.xgeomanager" additivity="false">
<level value="ALL"/>
<appender-ref ref="FileDebugAp"/>
</logger>
<logger name="com.optimi.tps" additivity="false">
<level value="ALL"/>
<appender-ref ref="FileDebugAp"/>
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="ConsoleInfoAp"/>
</root>
</log4j:configuration>
Well, I resolved this adding jboss-deployment-structure.xml file with this information:
<jboss-deployment-structure>
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding
some dependencies -->
<exclusions>
<module name="org.apache.log4j" />
<module name="org.apache.commons.logging" />
</exclusions>
</deployment>
</jboss-deployment-structure>

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.

How to log bean validation (jsr-303) messages when using log4j and hibernate validator?

I am not sure how to log bean validation messages when using log4j and hibernate validator.
I tried using a "org.hibernate.validator" category in log4j resulting in a runtime exception...
I also tried using "javax.validation" category in log4j; however nothing gets logged.
Can anyone please help?
Regards,
Edit: Here is my log4j config:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d: %5p (%F:%L) %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="output.log"/>
<param name="MaxFileSize" value="2000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n"/>
</layout>
</appender>
<category name="org.apache.tapestry5">
<priority value="INFO"/>
</category>
<category name="com.bignibou">
<priority value="DEBUG"/>
</category>
<category name="org.hibernate.validator.engine.ValidatorImpl">
<priority value="TRACE"/>
</category>
<root>
<priority value="WARN"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
Do you have the log4j binding for sl4f on your classpath? You might also have a look at the log4j.properties file used for tests in Hibernate Validator.

log4j: errors to an errorFile

I have a class which events I want to log. Say, there are 2 levels: debug and error. How can I log errors to an errorFile and debug info to a debugFile?
So that I use log4j.xml there is a filter mechanism, but it seems to work only for different categories not inside one class.
Please, help to newbie. =)
You have to define and use two appender with different Threshold like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Error -->
<appender name="ErrorFile"
class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="error"/>
<param name="file" value="log.err" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %C.%M (%L) - %m%n" />
</layout>
</appender>
<appender name="LogFile"
class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="debug"/>
<param name="file" value="log.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %C.%M (%L) - %m%n" />
</layout>
</appender>
<logger name="foo.bar" additivity="false">
<level value="debug" />
<appender-ref ref="LogFile" />
<appender-ref ref="ErrorFile" />
</logger>
<root>
<priority value="warn" />
<appender-ref ref="ErrorFile"/>
</root>
</log4j:configuration>
This question is also answered in the log4j FAQs.
Just found the LogToAppenderByLevel solution (here) which may be also helpful for you.

Resources