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.
Related
Our app runs on Tomcat 8 and Linux. We have log4j.xml shipped with the war that controls the level of logging for our applications logging. The log4j also defines logging levels for SQL.
In hibernate.cfg.xml, the "hibernate.show_sql" is set to true.
<property name="hibernate.show_sql">true</property>
In $CATALINA_BASE/conf/logging.properties
All references to ConsoleAppender:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Are removed, so nothing is sent to std out except for the SQL statements.
The: "hibernate.show_sql">true, sends all SQL statements being processed to catalina.out – please note that catalina.out is a file created only on Linux/Unix machines.
I read that "hibernate.show_sql">true, logs the SQL statements at DEBUG level. The problem is that I want to log those statements at ERROR level but don’t know how to control the level?
I read that that level for SQL logging can be controlled by log4j.xml, but changing it in log4j.xml has no effect on how the catalina.out is being logged. Does anyone know how can I control the SQL logging in catalina.out to be only at ERROR level?
Below is log4j.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mylog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${catalina.base}/logs/mylog.log" />
<param name="Threshold" value="TRACE" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="300MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="it.openutils.log4j.FilteredPatternLayout">
<param name="ConversionPattern"
value="%d{ISO8601} %5p [%t] Executor:%X{Executor} Type:%X{Type} A:%X{Account} C:%X{Campaign} %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="org.hibernate.SQL" additivity="false">
<level value="ERROR" />
<appender-ref ref="mylog" />
</logger>
<logger name="org.hibernate">
<level value="ERROR"/>
</logger>
<logger name="org.hibernate.jdbc.JDBCContext">
<level value="ERROR"/>
</logger>
<root>
<level value="info" />
<appender-ref ref="mylog" />
</root>
Since I was not able to find anyway to set the level to ERROR for Catalina.out, I decided to turn off the SQL logging all together. So I changed all levels in log4j.xml back to INFO and set this property to false in hibernate.cfg.xml.
<property name="hibernate.show_sql">false</property>
After a while logging, I noticed that there is nothing being logged in Catalina.out unless an error happens, so in this case, the errors and exceptions are being logged into Catalina.out and not the regular SQL statements, which is (almost) something I was trying to achieve at the first place. I do not know why that’s the case thou and where/how those logging levels are defined. But hope that helps somebody.
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
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>
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
I've set up a logfileAppender and a consoleAppender in my log4net config for my application. I would like the logfile appender to only write ERROR messages and above and the console appender to write DEBUG and above.
My config is:
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %M - %m%n" />
</layout>
<threshold value="ERROR"/>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %m%n" />
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
I'm finding that both ERROR and DEBUG is being output to my logfile appender. How to restrict it to only ERROR?
Note also that the level tag in the logger doesn't work the same way as threshold or a LevelMatchFilter.
Level indicates what log statements that actually will be generated. This is what you can test on in you code.
Threshold on the other hand, filters away all log messages that falls below your threshold.
This means that having a threshold that is higher than the highest logger level makes no sense. I have seen many times how one sets a level of INFO (because that is what most appenders will use), and then create an appender that has a threshold of DEBUG. And then you are surprised when no DEBUG messages actually appears on the appender...
To get very specific filtering for an appender, you need to configure a LevelMatchFilter or a LevelRangeFilter for the logfile appender to filter the events which are actually output.
For example:
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR"/>
<levelMax value="FATAL"/>
</filter>
or
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR"/>
</filter>
put one of these inside your <appender> tag, and this should work for you:
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR"/>
</filter>
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %M - %m%n" />
</layout>
<threshold value="ERROR"/>
</appender>
Note: Updated to remove mistake pointed out by kgiannakakis.
I've created a sample console application using your log4net config and I'm getting the exact behaviour you appear to be wanting....
using System;
using System.IO;
using log4net;
using log4net.Config;
namespace SO_1171258
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main()
{
XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString()));
log.Error(new Exception("error log statment"));
log.Debug("debug log statment");
}
}
}
When I run this application, the only thing in the logfile is:
2014-01-27 15:02:51,387 Main - System.Exception: error log statment
And to the screen I see:
2014-01-27 15:05:52,190 System.Exception: error log statment
2014-01-27 15:05:52,218 debug log statment
Here is the entirety of my app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %M - %m%n" />
</layout>
<threshold value="ERROR"/>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %m%n" />
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
The answers you are receiving are partly correct. The Threshold is only used to set a lower limit on the level appended. A threshold of ERROR will actually receive ERROR and FATAL (which is "above" ERROR).
You do want to implement a LevelMatchFilter with a level value of "ERROR" (and "DEBUG" for the other appender). However, you must also add a DenyAllFilter to the end of your filter chain.
For example:
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %M - %m%n" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
I've had to implement the DenyAllFilter since log4net 1.2.10 (you can see the question I had on this topic here).
You need to use additivity property. See here for an example. You need to define two loggers.