Log4j - Tomcat ignoring logging events with level below ERROR - java

I'm trying to use log4j-1.2.16 in an application.
First of all, the application uses GWT. In development the application is deployed in the embedded jetty of the GWT plugin for Eclipse.
In development the logging works fine: I can see the log messages in the console as expected (from DEBUG to WARN in stdout and from ERROR to FATAL in stderr as configured - check log4j.xml configuration file bellow).
In production the application is deployed in Tomcat 7.
In production I want the messages to be logged in the ${catalina.home}/logs/app.log daily rolling file (with level range from INFO to FATAL) plus the stdout and stderr (tomcat.stdout and tomcat.stderr) as mentioned above (check log4j.xml configuration file bellow).
The problem is that when I deploy the application in tomcat any log message with level bellow ERROR is not logged (not to the rolling file nor to the stdout). ERROR and FATAL messages are logged correctly though (to the rolling file and to the stderr).
Does anyone know why this is happening or have had the same problem?
Bellow you can see the log4j.xml configuration file. It looks fine to me and it works in jetty.
<throwableRenderer class="org.apache.log4j.EnhancedThrowableRenderer" />
<!-- daily rolling log file -->
<appender name="LOGFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="File" value="${catalina.home}/logs/app.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<!-- date and context come from a custom prefix -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p - %m%n" />
</layout>
</appender>
<!-- stdout -->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<!-- date and context come from a custom prefix -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p - %m%n" />
</layout>
<!-- range: TRACE to WARN -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="TRACE" />
<param name="levelMax" value="WARN" />
</filter>
</appender>
<!-- stderr -->
<appender name="stderr" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.err" />
<!-- date and context come from a custom prefix -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p - %m%n" />
</layout>
<!-- range: ERROR to FATAL -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="ERROR" />
<param name="levelMax" value="FATAL" />
</filter>
</appender>
<!-- disable non-application loggers with level below ERROR -->
<!-- axis -->
<category name="org.apache.axis">
<priority value="ERROR" />
<appender-ref ref="LOGFILE" />
<appender-ref ref="stdout" />
<appender-ref ref="stderr" />
</category>
<!-- jasper -->
<category name="org.apache.jasper">
<priority value="ERROR" />
<appender-ref ref="LOGFILE" />
<appender-ref ref="stdout" />
<appender-ref ref="stderr" />
</category>
<!-- jetty -->
<category name="org.mortbay">
<priority value="ERROR" />
<appender-ref ref="LOGFILE" />
<appender-ref ref="stdout" />
<appender-ref ref="stderr" />
</category>
<!-- gwt -->
<category name="com.google.gwt">
<priority value="ERROR" />
<appender-ref ref="LOGFILE" />
<appender-ref ref="stdout" />
<appender-ref ref="stderr" />
</category>
<!-- the root category -->
<root>
<priority value="DEBUG" />
<appender-ref ref="LOGFILE" />
<appender-ref ref="stdout" />
<appender-ref ref="stderr" />
</root>
Thanks in advance.
PS: stackoverflow removed the enclosing log4j:configuration tags. File structure is not the problem.

Related

Configuring log4j.xml in Tomcat

I would like to configure the logs for a simple app running in a tomcat and i'd like to send all logs except error level ones to one file, and the errors to other one.
How should i configure the log4j.xml to make it works?
<appender name="regular-container" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="regularLogs.log" />
</appender>
<appender name="error-container" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="errorLogs.log" />
</appender>
<logger name="regular-container">
?????????????????????
</logger>
<logger name="error-container">
?????????????????????
</logger>
Try this log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="errorsOnly" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="errorLogs.log" />
<param name="threshold" value="ERROR" />
</appender>
<appender name="regular" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="regularLogs.log" />
<param name="threshold" value="DEBUG" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="errorsOnly" />
<appender-ref ref="regular" />
</root>
</log4j:configuration>
Note that this includes FATAL logging in errorsOnly, though I assume that's okay. Having errorsOnly first with threshold set to ERROR means error level or above are handled by errorsOnly. DEBUG or higher not yet handled by errorsOnly are then handled by regular. I found a similar issue providing another example.
Thanks for answer, but it didnt work. Using filter parameter for each appender, yes.
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="ERROR" />
<param name="levelMax" value="FATAL" />
</filter>

i was create two log files using RollingFileAppender in log4j.xml

I want to create two log file.
Because my project has two modules and log each module in separate log files.
After that ,I have to log each module logging data independently .i have Single Log4j file.when i use appender -ref inside the root then i can achive but at that time aop or java logging cant appear in that file only manualy logger msg shown there
Try Something like :
<log4j:configuration>
<appender name="fileAppender1" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/Testing/Project_moduleOne.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<appender name="fileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/PAD_Testing/Project_moduleTwo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<!--sets the priority log level for org.springframework -->
<logger name="com.sp.logger1" additivity="false">
<appender ref="fileAppender1"/>
</logger>
<logger name="com.sp.logger2" additivity="false">
<appender ref="fileAppender2" />
</logger>
<!--sets the default priority log level -->
<root>
<priority value="all"></priority>
<appender-ref ref="fileAppender1" />
<appender-ref ref="fileAppender2" />
</root>
</log4j:configuration>

How to log jasper report logs to separate log file?

I am new to JasperReport and Log4j, Now I have to Log all log messages related to jasper report to a new log file. As of now it writes to log file and console bit I want it to log only on separate log file. How can achive this.
Here is my log4j.xml file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p :%L - %m%n" />
</layout>
</appender>
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="console" />
<appender-ref ref="file" />
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.home}/logs/ITS_Server.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p:%L - %m%n" />
</layout>
</appender>
<category name="org.hibernate">
<priority value="DEBUG" />
</category>
<category name="java.sql">
<priority value="debug" />
</category>
<root>
<level value="DEBUG" />
<appender-ref ref="file" />
</root> -->
</log4j:configuration>
Please Anyone can help me in this please.
you have to define a new appender:
<appender name="jasper" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.home}/logs/jasperReports.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p:%L - %m%n" />
</layout>
</appender>
And a new category with have the apender as reference:
<category additivity="false" name="net.sf.jasperreports">
<priority value="debug" />
<appender-ref ref="jasper"/>
</category>

SLF 4j Logs are not written to log

Logs are not written properly
slf version slf4j-api-1.7.5.jar, slf4j-log4j12-1.7.5.jar, log4j-1.2.16.jar
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- log4j generic catchall for adapters. -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Example DailyRollingFile appender, this is the preferred logging appender -->
<appender name="CommonAdapterAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/opt/adapter.log" />
<param name="encoding" value="UTF-8" />
<param name="append" value="true" />
<!-- Rollover at the top of every hour -->
<param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss.SSS-zzz} %5p [%t] %c{1} - %m%n" />
</layout>
</appender>
<!-- Categories -->
<category name="com.other" additivity="false">
<priority value="warn" />
</category>
<logger name="com.adapter" additivity="false">
<level value="debug" />
<appender-ref ref="CommonAdapterAppender"/>
</logger>
<logger name="com.adaptations" additivity="false">
<level value="debug" />
<appender-ref ref="CommonAdapterAppender"/>
</logger>
<root>
<priority value="error" />
<appender-ref ref="CommonAdapterAppender" />
<!-- <appender-ref ref="SyslogAppender"/> -->
</root>
</log4j:configuration>
The above log4j file is used by multiple adapters. First time it is writing to adapter.log and after that only some component logs are written. Also I have noticed that after the second time it is writing few logs to adapter.log..
I cannot understand what is going wrong here. Can someone please help me out?
I believe you should be using a single shared log 4J configuration and instance for all your adapters if you want to use the the same log file.
As is, Log 4j instances are probably competing for the control of the file.

Send Log4J INFO to database and DEBUG to Console?

I have com.mycompany.library.*
I want DEBUG go to console, and INFO go to DB. how I can achieve it?
this is part of my log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false"
xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- APPENDERS LIST -->
<!-- show log entries on console -->
<appender name="ConsoleApp" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} [%-5p][%-16.16t][%32.32c] - %m%n" />
</layout>
</appender>
<appender name="DBAppender" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="URL" value="jdbc:postgresql://localhost:5432/myDB" />
<param name="driver" value="org.postgresql.Driver" />
<param name="user" value="postgres" />
<param name="password" value="password" />
<param name="sql"
value="INSERT INTO logs(user_id, dated,logger,lev,message) VALUES('%t', '%d{yyyy-MM-dd HH:mm:ss}','%-50c{3}','%p','%m')" />
</appender>
<logger name="com.mycompany">
<level value="INFO" />
<appender-ref ref="DBAppender" />
</logger>
<logger name="com.mycompany.library">
<level value="DEBUG" />
<appender-ref ref="ConsoleApp" />
</logger>
<root>
<priority value="ERROR" />
<appender-ref ref="DBAppender" />
</root>
</log4j:configuration>
With this config:
INFO and DEBUG goes to console. But Also INFO and DUBUG with double entries go to DB.
I want that to DB goes events with INFO level(and higher), not DEBUG. How to do that?
You can set a treshold on your appenders (not on the loggers). For instance:
<appender name="std-out" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">...</layout>
<param name="Threshold" value="info"/>
</appender>
See http://wiki.apache.org/logging-log4j/LogByLevel
edit: You also don't have to set an appender on both the com.mycompany logger and the root logger. This is why you have duplicate entries, because they are treated as additive (so at the level of com.mycompany, you essentially have 2 DB appenders).
Just set DBAppender on root and remove it from the com.mycompany logger.

Categories