I'm attempting to log to two different log files, one "alert.log" to be monitored, and one "debug.log" for troubleshooting. In the configuration below, the "alert.log" grows with error messages, and the "debug.log" is created as a zero-length file that never grows.
I've tried swapping the contents between "root" and "logger", and the logged messages are written as expected. It seems whatever is in "root" gets written and whatever is in logger doesn't. What might I be doing wrong?
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="AlertableFileAppender" class="LoggerAppenderRollingFile">
<param name="file" value="/var/log/example/alert.log"/>
<param name="append" value="true"/>
<param name="maxBackupIndex" value="0"/>
<param name="maxFileSize" value="10000KB"/>
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%d{ISO8601} %p src="%M:%L" %m%n"/>
</layout>
</appender>
<appender name="DebugFileAppender" class="LoggerAppenderRollingFile">
<param name="file" value="/var/log/example/debug.log"/>
<param name="append" value="true"/>
<param name="maxBackupIndex" value="0"/>
<param name="maxFileSize" value="10000KB"/>
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%d{ISO8601} %p src="%M:%L" %m%n"/>
</layout>
</appender>
<appender name="ConsoleAppender" class="LoggerAppenderConsole">
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%d{ISO8601} %p src="%M:%L" %m%n"/>
</layout>
</appender>
<logger name="SecondLogger">
<level value="DEBUG"/>
<appender_ref ref="DebugFileAppender"/>
<appender_ref ref="ConsoleAppender"/>
</logger>
<root>
<level value="WARN"/>
<appender_ref ref="AlertableFileAppender"/>
</root>
</configuration>
Rewriting the config to use threshold="warn" on the AlertableFileAppender and putting both appender references in root worked for me. I removed logger and set the root level to "all".
Related
I have log4j config file, it seems to be working for normal flows, but when comes to batch it is not working, not sure what has to added in config file in specific to batch. Please help on this issue
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:\Users\Logs.log" />
............
<layout class="org.apache.log4j.PatternLayout">
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="FATAL" />
<param name="AcceptOnMatch" value="true" />
</filter>
<root>
<appender-ref ref="FILE" />
</root>
Blog i have found like this log4j.logger.com.mulesoft.module.batch=INFO. I'm not sure what all the config specific for batch has to be added in above xml.
Are we have any special config for Batch in Log4j Mule ESB?
I'm not sure how to be executed for batch.
Thanks in advance.
Try the following in your log4j.xml :-
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="rolling-out" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="info" />
<param name="File" value="C:/Users/Logs.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d] [%-5p] [%t] %m%n"/>
</layout>
</appender>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="info" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d] [%-5p] [%t] %m%n"/>
</layout>
</appender>
<logger name="log4j.logger.com.gigaspaces">
<level value="ERROR" />
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.com.j_spaces">
<level value="ERROR" />
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.com.sun.jini">
<level value="ERROR" />
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.net.jini">
<level value="ERROR" />
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.org.mule">
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.org.mulesoft">
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.org.apache.cxf">
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.org.apache">
<appender-ref ref="rolling-out" />
</logger>
<logger name="log4j.logger.org.springframework.beans.factory">
<appender-ref ref="rolling-out" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="rolling-out" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
Change the path :- <param name="File" value="C:/Users/Logs.log" /> according to your requirement .. This path is where the log file will be generated
In my project I'm using MVC, NHibernate and for logging html errors I wanna use log4net library.
Here is web.config configuration
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<!--<sectionGroup name="blowery.web">
<section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
</sectionGroup>-->
</configSections>
<log4net debug="false">
<!-- Define some output appenders -->
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="INFO" />
<appender-ref ref="console" />
</root>
</log4net>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Password=maserver.login12345;Persist Security Info=True;User ID=sa;Initial Catalog=Mashhad4;Data Source=DEVSERVER</property>
<property name="show_sql">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<!--<property name="hibernate.batch_size">20</property>-->
<!--<property name="command_timeout">5000</property>-->
</session-factory>
</hibernate-configuration>
<configuration>
In AssemblyInfo.cs file I have this:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
But in the controller, when I use this part of code, it doesn't create any txt file.
log4net.Config.BasicConfigurator.Configure();
Exception inner = filterContext.Exception.InnerException;
log4net.ILog log = log4net.LogManager.GetLogger("MainLogger");
log.Error("Unhandled exception", inner == null ? filterContext.Exception : inner);
What is the problem?
Try adding this under the root element:
<logger name="NHibernate.SQL">
<appender-ref ref="rollingFile" />
<level value="ALL" />
</logger>
Edit:
In order to use the logger called MainLogger, use this:
<logger name="MainLogger">
<appender-ref ref="rollingFile" />
<level value="ALL" />
</logger>
When in Debug mode, the file will be created in <projectfolder>\bin\Debug, since you did not specify a path but only the filename.
im kinda new with log4php..
i need to output the INFO and DEBUG levels in different files.
is that possible?
my xml looks like this:
<appender name="dlog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/dlog"></param></appender>
<appender name="ilog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/ilog/"></param></appender>
<root>
<level value="DEBUG" />
<appender_ref ref="dlog" ></appender_ref>
</root>
<logger name="myLogger">
<level value="INFO"/>
<appender-ref ref="ilog" />
</logger>
**This outputs only the INFO levels in the corresponding filename
please help :(
thanks a lot.
This can be achieved using multiple appenders with filters and one logger.
Overview
You can define multiple appenders to send the logging to different destinations. We shall define 1 appender for each level and each appender will be logging to different files.
In addition to these, we need to create a filter for each appender. We will use the filter LoggerFilterLevelRange and we shall specify a min and max level. Note the filter LoggerFilterLevelMatch cannot be used as it matches a filter but is neutral to the other filters.
As for the loggers, we shall attach all our appenders to the one and only logger.
This way, you can send the different logging messages to different files depending on the error level.
Configuration
<appender name="dlog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/dlog" />
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="debug" />
<param name="levelMax" value="debug" />
</filter>
</appender>
<appender name="ilog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/ilog" />
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="info" />
<param name="levelMax" value="info" />
</filter>
</appender>
<root>
<level value="INFO" />
<appender_ref ref="ilog" />
<appender_ref ref="dlog" />
</root>
</configuration>
Usage
require_once 'Logger.php';
Logger::configure('log_config.xml');
$logger = Logger::getLogger();
$logger->info('This message is meant to inform.');
$logger->debug('A debug message!');
I hope this helps.
I think the appropriate solution would be to apply a filter and a threshold to your appenders. Like so:
<appender name="dlog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/dlog" />
<filter class="LoggerFilterLevelMatch">
<param name="LevelToMatch" value="debug" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="LoggerFilterDenyAll" />
</appender>
<appender threshold="INFO" name="ilog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/ilog/" />
</appender>
<root>
<level value="DEBUG" />
<appender_ref ref="dlog" />
<appender_ref ref="ilog" />
</root>
This would only allow messages with debug level into dlog and (due to the threshold) only info and above into ilog. If you didn't want messages above info level in ilog, then you could apply the same filter as with the dlog appender.
I have this in my web.config file (edited to reflect some changes):
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net debug="true">
<appender name="DebugAppender" type="log4net.Appender.AspNetTraceAppender">
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="%d %p %m%n" />
</layout>
</appender>
<appender name="DataLog" type="log4net.Appender.RollingFileAppender">
<file value="c:\temp\nhlog.txt" />
<appendToFile value="true" />
<maximumFileSize value="100MB" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="DebugAppender" />
</root>
<logger name="NHibernate" additivity="false">
<level value="INFO"/>
<appender-ref ref="DebugAppender"/>
</logger>
</log4net>
I'm calling log4net.Config.XmlConfigurator.Configure() from Application_Start().
I have [assembly: log4net.Config.XmlConfigurator(Watch = true)] in the AssemblyInfo.cs file in my web project.
Why do I get no NHibernate debugging messages in my output window? (I trying to get ALL of the messages that NHibernate spits out.) If I use the RollingFileAppender it works fine. I've tried the DebugAppender, TraceAppender, OutputDebugStringAppender, and AspNetTraceAppender and none of them output stuff to the output window.
Try using a different appender.
ConsoleAppenders probably don't work for web applications. Use something like a FileAppender.
See this for examples on configuraiton.
Similar to quip's answer, ConsoleAppender is not the right appender to use in a web application. An OutputDebugStringAppender will give you messages in your debug window. You may also want to look into AspNetTraceAppender.
You could try to associate the specific NHibernate logger with a log4net appender.
If you want to log all the generated sql, try adding this to your log4net config section:
<logger name="NHibernate.SQL" additivity="false">
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</logger>
If, for some reason, you only wanted to log Select statements and diagnostic info, you could use NHibernate.Loader.Loader, like this:
<logger name="NHibernate.Loader.Loader" additivity="false">
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</logger>
I have 2 console apps projects in the same directory but different projects. There is some common code in the App_Code directory and a common app.config which gets build into seperate .exe.config files.
One module (VScanDemonStarter) starts up and writes to one logger with its own appender going to a seperate file. It uses an process.start() to execute the other module (VScanDemon) in another command prompt hidden window.
When I run VScanDemon by itself it puts entries into its log file. When I run VScanDemonStarter it puts entries into its (different) log file, the VScanDemon log file gets created, but no entries. I can see it is executing because some files get moved from one directory to another. Just no Log entries.
the config looks like
<root>
<level value="INFO" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log/vscandemonstarter.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd hh:mm:ss} - %m%n" />
</layout>
</appender>
<appender name="vsdemonlogfileappender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log/vscandemon.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd hh:mm:ss} - %m%n" />
</layout>
</appender>
and the code bodies set up and call the bodies with.
VScanDemonStarter:
top of module:
Private ReadOnly log As ILog = log4net.LogManager.GetLogger("default")
top of main:
log4net.Config.XmlConfigurator.Configure()
example calls:
If log.IsInfoEnabled Then log.Info("VScanDemonStarter:Main: ----called----")
VBScanDemon:
top of module:
Private ReadOnly log As ILog = log4net.LogManager.GetLogger("VSDemonLogger")
top of main:
log4net.Config.XmlConfigurator.Configure()
VBScanDemon:
If log.IsInfoEnabled Then log.Info("VScanDemon:Main: ----called----")
I don't get any log entries from VScanDemon.
Sorry left out the top of the configuration
<log4net debug="true">
<logger name="default">
<level value="INFO"/>
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="VSDemonLogger">
<level value="INFO"/>
<appender-ref ref="vsdemonlogfileappender" />
</logger>
I have one app.config but two projects in the same directory using it. They generate seperate configs from that one source .exe.config
I looked at the console output from VScanDemon and it looks like it is picking up the configuration with no issues. But still an empty log. I don't know if I need to add a flush or something.
Are you useing the same log4net configuration for both modules?
If not, are they really different at runtime?
I think "default" should return the Root-logger and "VSDemonLogger" the other one but that one is not included in your posted configuration.
EDIT:
AFAIK all logger in the configuration will be "created", which results in the generated log files for your VSDemonLogger and VScanDemonStarter even if you do not use them.
You are using relativ pathes in the configuration, are they still valid for VSDemonLogger if you call it from VScanDemonStarter?