In my case,
I have configured my log4j.xml like this.
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />
<param name="File" value="F:/myLogger.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="20000KB" />
<param name="MaxBackupIndex" value="400" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %d{dd MMM yyyy HH:mm:ss.SSS} %-5l - %m%n%n" />
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="error" />
<appender-ref ref="FILE" />
</root>
and put log4.xml file in source package.
but logger file is not created at specified folder.Let me know the exact path.
(it may be tha t i am using some external JAR which may have log4j.xml) so how to give priority to root application log4j.xml file.
Related
I have a problem.
Log4j dont want write to console and to file with system variable in path. Only write to file with simple path.
Configuration of my log4j.
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<!-- <param name="file" value="d:/lv-098_JAVA/logs/log.log" /> -->
<param name="file" value="${LV098_JAVA}/src/main/resources/logs/log.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p (%F:%L:%M) %c{1} %m%n" />
</layout>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="file1" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
You are declaring
appender name="file" class="org.apache.log4j.DailyRollingFileAppender"
and in root tag you write
appender-ref ref="file1"
As there is no such file1. Due to this log4j is not working for you.
I found where problem is. It is in Jboss, because jboss have his own logger and it doesn't allow to write to console. You must change configuration of Jboss logger. But I don't know why it does not allow to use system variable.
I am using org.apache.log4j.rolling.RollingFileAppender to generate daily logs.
It generates daily logs in windows, but it is not generating logs in linux.
Help is greatly appreciated!
Below is the log4j.xml file:
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="D:/source/logs/passports.log.%d{yyyy-MM-dd}.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<!-- The log message pattern -->
<param name="ConversionPattern" value="%d [%t] %-5p {%c} %m%n" />
</layout>
<root>
<priority value="info,debug,error" />
<appender-ref ref="ROLL" />
</root>
Your FileNamePattern needs to reflect the location of the file, specific to the server/workstation the application is running in.
In the snippet you gave, I see that the value for FileNamePattern is D:/source/logs/passports.log.%d{yyyy-MM-dd}.log.
The "D:" suggests to me that this is for your windows box.
Replace this value with the location that your linux box would hold the log file, for example: /var/source/logs/passports.log.%d{yyyy-MM-dd}.log
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 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.
I'm using log4net v1.2 with a Windows Service App. My RollingFileAppender seems not to work. I'm pasting the logging sections of my service.exe.config below. Can anyone advise where I'm going wrong?
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
.....(lots of other config stuff)
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="D:\\Trinity\\Booking\\OneDay_PostTrade\\logs\\Trinity.log" />
<param name="MaximumFileSize" value="20MB" />
<param name="MaxSizeRollBackups" value="10" />
<param name="StaticLogFileName" value="true" />
<param name="Threshold" value="ALL" />
<param name="RollingStyle" value="Composite" />
<param name="appendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
...(stuff in between)
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
.....(stuff in between)
<logger name="CSFB.PostTradeRulesEngine">
<level value="ALL"/>
</logger>
The user your windows service is running as might not have write permission for the log file.
Another possibility is that you forgot to execute XmlConfigurator.Configure();
try writing:
<log4net debug="true">
it will post all errors to console.
thanks to everyone who responded. I dont know what i changed but my logging has started working fine.
Posting my logging sections. I didnt change anything in the code, except a line in the AssemblyInfo.cs:
[assembly: log4net.Config.Domain(UseDefaultDomain=true)]
Thanks again.:)