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.
Related
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>
My environment:
CAS 4.0.0 project
dependencies
slf4j-log4j12-1.7.5
log4j-1.2.17
slf4j-api.1.7.5
(Whole process in remote debug) when the tomcat initialize the webapp, in DatasourceWrapper, logger does log(i checked). But the problem is the logger does not log after webapp initialized.
My java file
package sg.com.innovax;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DatasourceWrapper extends BasicDataSource implements Serializable
{
private static final long serialVersionUID = 4139847655780946796L;
private Logger logger = LoggerFactory.getLogger(getClass());
#Override
public Connection getConnection() throws SQLException
{
logger.debug("Number of active connections:" + super.getNumActive());
logger.info("Number of idle connections:" + super.getNumIdle());
logger.error("Number of max active:" + super.getMaxActive());
return super.getConnection();
}
#Override
public Connection getConnection(String username, String password) throws SQLException
{
logger.debug("Number of active connections:" + super.getNumActive());
logger.info("Number of idle connections:" + super.getNumIdle());
logger.error("Number of max active:" + super.getMaxActive());
return super.getConnection(username, password);
}
}
my xml
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--
This default ConsoleAppender is used to logger all NON perf4j messages
to System.out
-->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - <%m>%n"/>
</layout>
</appender>
<appender name="cas" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="cas123.log" />
<param name="MaxFileSize" value="512KB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
</layout>
</appender>
<!-- Perf4J appenders -->
<!--
This AsyncCoalescingStatisticsAppender groups StopWatch logger messages
into GroupedTimingStatistics messages which it sends on the
file appender defined below
-->
<appender name="CoalescingStatistics" class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="60000"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="graphExecutionTimes"/>
<appender-ref ref="graphExecutionTPS"/>
</appender>
<!-- This file appender is used to output aggregated performance statistics -->
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="perfStats.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<appender name="graphExecutionTimes" class="org.perf4j.log4j.GraphingStatisticsAppender">
<!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS -->
<param name="GraphType" value="Mean"/>
<!-- The tags of the timed execution blocks to graph are specified here -->
<param name="TagNamesToGraph" value="DESTROY_TICKET_GRANTING_TICKET,GRANT_SERVICE_TICKET,GRANT_PROXY_GRANTING_TICKET,VALIDATE_SERVICE_TICKET,CREATE_TICKET_GRANTING_TICKET,AUTHENTICATE" />
</appender>
<appender name="graphExecutionTPS" class="org.perf4j.log4j.GraphingStatisticsAppender">
<param name="GraphType" value="TPS" />
<param name="TagNamesToGraph" value="DESTROY_TICKET_GRANTING_TICKET,GRANT_SERVICE_TICKET,GRANT_PROXY_GRANTING_TICKET,VALIDATE_SERVICE_TICKET,CREATE_TICKET_GRANTING_TICKET,AUTHENTICATE" />
</appender>
<!-- Loggers -->
<!--
The Perf4J logger. Note that org.perf4j.TimingLogger is the value of the
org.perf4j.StopWatch.DEFAULT_LOGGER_NAME constant. Also, note that
additivity is set to false, which is usually what is desired - this means
that timing statements will only be sent to this logger and NOT to
upstream loggers.
-->
<logger name="org.perf4j.TimingLogger" additivity="false">
<level value="INFO" />
<appender-ref ref="CoalescingStatistics" />
</logger>
<!--
WARNING: Setting the org.springframework logger to DEBUG displays debug information about
the request parameter values being bound to the command objects. This could expose your
password in the logger file. If you are sharing your logger files, it is recommend you selectively
apply DEBUG level logging on a an org.springframework.* package level (i.e. org.springframework.dao)
-->
<logger name="org.springframework">
<level value="WARN" />
</logger>
<logger name="org.springframework.webflow">
<level value="WARN" />
</logger>
<logger name="org.jasig" additivity="true">
<level value="INFO" />
<appender-ref ref="cas" />
</logger>
<logger name="com.github.inspektr.audit.support.Slf4jLoggingAuditTrailManager">
<level value="INFO" />
<appender-ref ref="cas" />
</logger>
<!--
WARNING: Setting the flow package to DEBUG will display
the parameters posted to the login servlet including
cleartext authentication credentials
-->
<logger name="org.jasig.cas.web.flow" additivity="true">
<level value="INFO" />
<appender-ref ref="cas" />
</logger>
<logger name="sg.com.innovax">
<level value="INFO" />
<appender-ref ref="cas" />
</logger>
<!--
The root logger sends all logger statements EXCEPT those sent to the perf4j
logger to System.out.
-->
<root>
<level value="ERROR" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
another xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<description>
Log4J initialization. Configuration options are sourced from cas.properties. This allows deployers to externalize
both cas.properties and log4j.xml, so that a single cas.war file can be deployed to multiple tiers or hosts without
having to do any post configuration. This approach helps to preserve configuration between upgrades.
Deployers should not have to edit this file.
</description>
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:targetClass="org.springframework.util.Log4jConfigurer" p:targetMethod="initLogging" p:arguments-ref="arguments"/>
<util:list id="arguments">
<value>${log4j.config.location:classpath:log4j.xml}</value>
<value>${log4j.refresh.interval:60000}</value>
</util:list>
</beans>
My datasourceWrapper in sg.com.innovax, and i do specify in log4j
<logger name="sg.com.innovax">
<level value="INFO" />
<appender-ref ref="cas" />
</logger>
then it should be able to log in cas.log, however when i check from netbeans output, it show the error info in apache tomcat.
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.
My log4j.xml configuration was like ,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<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.comp.logger1">
<appender ref="fileAppender1"/>
</logger>
<logger name="com.comp.logger2">
<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>
And two log files also created in the specified location.
I need to know how to log two different data's in these two different log_files independently in JAVA class.
For example,
Logger logOne = Logger.getLogger("com.comp.logger1");
Logger logTwo = Logger.getLogger("com.comp.logger2");
The above code is not working for me. All the log informations are logged to both the created two log files. I need the separation of logging data.
My need is ,
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 .
Please make sure I used the logger name for logging in my java class correctly.
Any new or complete examples using log4j.xml are greatly appreciated.
EDIT :
If I add the additivity="false" in the logger as,
<logger name="com.comp.logger1" additivity="false">
<appender ref="fileAppender1" />
</logger>
<logger name="com.comp.logger2" additivity="false">
<appender ref="fileAppender2" />
</logger>
The log data didn't logged in the created log file.The log file was empty.
Please make sure my <root>...</root> is correct.
You problem is with the <root> section, the root section captures all logs, and you've told it to log to both appenders...
You could remove it, or set additivity="false" on each of your logger elements - this will tell log4j not to log the same log through 'root' if it's already been logged through one of your 'logger's.
Edit: you don't say which version of log4j you are using, but I'm using log4j-1.2.16, and using the file in your post, it fails completely for me because it doesn't like the <appender ref="fileAppender1"/>, it wants them to be <appender-ref ref="fileAppender1"/> (note the -ref after the appender). If I add these in, and also add the additivity attributes that I suggested, then for me it works as you expect.
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.