specific logger for levels - log4j

I am using log4j for logging. I have a scenario where I have to use seperate logging for different severity. For example, for package foo I have to print messages with severity ERROR in the console whereas I have to print messages with severity WARN in log file. How can I configure my log4j.xml for the same.

You may use LevelMatchFilter for your task. Also you should not forget to use another filter - DenyAllFilter - as the last filter in the chain of filters.
Your log4j.xml will look like this:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout" />
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="ERROR" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="error.log" />
<layout class="org.apache.log4j.SimpleLayout" />
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="WARN" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<logger name="foo">
<level value="WARN" />
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
</logger>
</log4j:configuration>

Related

Log4Net not logging error in release mode (.net console APP)

Hello i am using Log4Net in Console APP
Here is configuration in Assembly File
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Below is log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="InfoRollingLogFileAppender"
type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="WarningLogger.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="10240KB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<param name="Threshold" value="DEBUG"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%p [%d{dd MMM HH:mm:ss}][%l]- %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="WARN" />
</filter>
</appender>
<root>
<level value="debug" />
<appender-ref ref="InfoRollingLogFileAppender" />
<appender-ref ref="ErrorRollingLogFileAppender" />
</root>
</log4net>
</configuration>
For Initializing log4net i am using following configuration.
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
This things perfectly work in Debug mode. Same configuration is not working in release mode.
Let me know if any solutions
When you static method initializes the is no GetCurrentMethod().DeclaringType. That is probably why it does not work in release mode.
Got it resolved by adding up configuration on start up file or Program file in console .
XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["log4net-config-file"]));

Why i am getting the Warn Messages also in my logger when configured to get from INFO Level

I have this log4j.xml file which should only log from INFO Level
but i was also getting the WARN Level also in my log .
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender class="org.apache.log4j.RollingFileAppender" name="FILE">
<param value="D:\\RAM\\tst.log" name="File" />
<param value="10" name="MaxBackupIndex" />
<param value="200MB" name="MaxFileSize" />
<layout class="org.apache.log4j.PatternLayout">
<param value="%d[%t] %-5p(%F:<%M>:%L)- %m%n" name="ConversionPattern" />
</layout>
</appender>
<appender class="com.Log4JCustomAppender" name="CUSTAPPEN">
<layout class="org.apache.log4j.PatternLayout">
<param value="%d[%t] %-5p(%F:<%M>:%L)- %m%n" name="ConversionPattern" />
</layout>
</appender>
<appender class="org.apache.log4j.AsyncAppender" name="ASYNC">
<param name="Blocking" value="false"/>
<param name="BufferSize" value="1000"/>
<appender-ref ref="CUSTAPPEN" />
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FILE" />
<appender-ref ref="ASYNC" />
</root>
</log4j:configuration>
This is the output i was getting in my Custom Appender and RollingFileAppender
LOgged One is 2013-06-23 01:05:55,954[main] FATAL(Hi.java:<main>:14)- This is a fatal Message
LOgged One is 2013-06-23 01:05:55,965[main] WARN (Hi.java:<main>:15)- This is a warn Message
please tell me how to avoid this .
This behavior is normal. Setting a logger to a specific level value indicates that you want that level AND all level above.
Levels are :
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
OFF
If you want only the INFO level, you have to use filters on your appenders :
<filter type="org.apache.log4j.varia.LevelMatchFilter">
<acceptOnMatch value="true" />
<levelToMatch value="INFO" />
</filter>
<filter type="org.apache.log4j.varia.DenyAllFilter" />

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.

How do I use a StringMatchFilter to send logging events to different logs by their message content in log4net?

I'm using log4net for logging and sending e-mails. I need to send different e-mails with different configurations according to different processes. For example, when one of my methods finishes processing, it sends a mail to X, and when my service finishes successfully, it sends another mail to Y.
I think I need to use different appenders with filters, but I can't get to configure them properly.
I tried the following and tried to apply a StringMatchFilter so that messages containing "mandatory" will not be delivered to Y and messages containing "successfully" will not be delivered to X, but with no luck. I am also not sure whether this kind of filtering is the right way to do this kind of configuration.
<log4net>
<appender name="ManFieldMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="successfully" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="x#xxx.com" />
<from value="aaa#aaa.com" />
<subject value="Products not made online" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<appender name="ServiceFinishedMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="mandatory" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="y#yyy.com" />
<from value="bbb#bbb.com" />
<subject value="Product Publish Service has successfully finished operating" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<root>
<appender-ref ref="ManFieldMailAppender" />
<appender-ref ref="ServiceFinishedMailAppender"/>
</root>
</log4net>
What do you think the problem is?
Thank you.
First, evaluator tells buffering appenders when to flush. From the look of your configuration I don't think you're buffering. (so you can get rid of the bufferSize and lossy parameters, too). I see your appender filter chains have the required DenyAllFilters. But, your root logger configuration is showing no levels, try adding
<root>
<level value="ALL" />
<!-- appender-refs as normal -->
</root>
In general, I wouldn't make the decision to log to a particular appender based on the message text. It looks like you have a high-level service class for one of the operations. If you have the same thing for the other messages that need to be sent, and you're naming your loggers by typename, you can specify an appender per logger.
<logger name="MyCompany.Product.Whatever">
<level value="WARN" />
<appender name="ManFieldMailAppender" type="...">
<!-- the rest of the configuration, without filtering goes here -->
</appender>
</logger>
<logger name="MyCompany.Services.Publish">
<level value="WARN" />
<appender name="ServiceFinishedMailAppender" type="...">
<!-- the rest of the configuration, without filtering goes here -->
</appender>
</logger>

Log4net appender threshold not working

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.

Resources