I have a old project which use log4j(not log4j2),it will print file name and line number.
2017-10-21 17:08:54,198 INFO [com.fudy.log4j.PerformanceLogger.info:11
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c.%M:%L] : %m%n" />
</layout>
</appender>
after I add AsyncAppender to log4j.xml
<logger name="com.fudy.log4j.PerformanceLogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="async" />
</logger>
it print ? instead of method name and linenumber:
2017-10-21 17:24:17,909 INFO [com.fudy.log4j.PerformanceLogger.?:?
after googled, all solution is to add includeLocation in log4j2, is there any solution for logj4 1.x
I found the solution for log4j 1.x
<appender name="async" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="256"/>
<param name="LocationInfo" value="true"/> <!-- add this one -->
<appender-ref ref="console"/>
</appender>
Related
How wil get Context Path in log4j property files? Ex: My log file name would be contextpath.log.
You will have to create a different appender for each context path. And Implement each context in a different Controller class.
for example
<appender name="FILE-1" class="org.apache.log4j.FileAppender">
<param name="File" value="contextpath1.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
<appender name="FILE-2" class="org.apache.log4j.FileAppender">
<param name="File" value="contextpath2.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
<logger name="com.mycompany.apackage.Context1Class">
<level value="info"/>
<appender-ref ref="FILE-1"/>
</logger>
<logger name="com.mycompany.apackage.Context2Class">
<level value="info"/>
<appender-ref ref="FILE-2"/>
</logger>
I've got the following configuration using log4net. The problem is that the logger in my C# code is now logging the errors into the two log files. I've got two different service classes in the same windows service. I initialize the logger in one service using this line:
private static readonly ILog _logger = LogManager.GetLogger(typeof(EmployeeImportService));
But when this service runs and logs, it's writing to both log files.
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Temp\HRFiles\Sharp\Log\Log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="OvertimeLogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Temp\HRFiles\YTD\Log\Log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="OvertimeLogFileAppender" />
</root>
</log4net>
How do I configure it so that each service writes to it's own log file? I read that the logger tag in the configuration has an additivity attribute that solves this problem but I have no logger element in my configuration.
You can have two separate loggers, and define which appenders each logger will use in the configuration: you do have to declare additivity=false or else the loggers inherit the appenders from the root logger.
So, with made-up names:
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="OvertimeLogFileAppender" />
</root>
<logger name="Company.Project.EmployeeImportService" additivity="false">
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="Company.Project.EmployeeOvertimeService" additivity="false">
<appender-ref ref="OvertimeLogFileAppender" />
</logger>
Then in your service classes you get the approppriate logger:
// General log - EmployeeImportService
ILog logger = LogManager.GetLogger(typeof(EmployeeImportService));
// Log overtime - EmployeeOvertimeService
ILog logger = LogManager.GetLogger(typeof(EmployeeOvertimeService));
Oups yes I did a mistake. But it quiet the same. You have to create filter in each appender node
<filter type="log4net.Filter.LoggerMatchFilter">
<!-- allows this sub-namespace to be logged... -->
<loggerToMatch value="EmployeeImportService" />
</filter>
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
My requirement is to log the messages to 3 separate log files.
I am using 3 appenders in a JBOSS server.
Three of them need to work in the way such as
1.RollingFileAppender, consoleAppender work for 'INFO' and 'ERROR' modes.
2.DailyRollingFileAppender for 'ERROR' mode and for a specific category only.
I have tried lots of combinations like changing the priority value and the categories, but I have so far found no way in which all the three work simulatneously. Either fileAppender and ConsoleAppender work(logging both ERRROR and INFO) and the DailyRollingFileAppender does not or DailyRollingFileAppender does but the other two log only ERRORS not INFO.
Please tell me a way so that the three of them work.
Below mentioned is the log4j.xml I am using.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- ===================================================================== -->
<!-- -->
<!-- Log4j Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- -->
<!--
| For more configuration infromation and examples see the Jakarta Log4j
| owebsite: http://jakarta.apache.org/log4j
-->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- file appender -->
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:/logs/mdmWSServer.log" />
<param name="MaxBackupIndex" value="5" />
<param name="MaxFileSize" value="5MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%t] [%-5p][ %c:] %m%n"/>
</layout>
</appender>
<appender name="dailyFileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:/logs/dailyApp.log"/>
<param name="MaxBackupIndex" value="5" />
<param name="MaxFileSize" value="5MB" />
<param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%t] [%-5p][%c:] %m%n"/>
</layout>
</appender>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%t] %m%n"/>
</layout>
</appender>
<category name="com">
<priority value="INFO"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="consoleAppender"/>
</category>
<category name="com.gsk">
<priority value="ERROR"/>
<appender-ref ref="dailyFileAppender"/>
</category>
<category name="org.apache">
<priority value="INFO"/>
<appender-ref ref="fileAppender"/>
</category>
<root>
<priority value="DEBUG"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>
You can restrict an appender to only log a certain level by using filter class specifying which level you would want to log . See the sample code below
<appender name="TRACE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %-5p %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="DEBUG" />
<param name="levelMax" value="DEBUG" />
</filter>
</appender>
I want to set up log4j so that all log meessages produced from classes under package com.foo.bar go to bar.log, and all the log meessages produced from classes under package com.bar.blatz go to blatz.log.
Questions
How do I do this using log4j.xml?
I know its possible using property files, but how do I do it using the XML configuration?
This is based on my answer to a similar question:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="BarLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="bar.log" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<!-- additional fooSystem logging -->
<appender name="BlatzLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="blatz.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<logger name="com.foo.bar">
<appender-ref ref="BarLogFile"/>
</logger>
<logger name="com.bar.blatz">
<appender-ref ref="BlatzLogFile"/>
</logger>
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
</log4j:configuration>
If you add an appender-ref to the root element, it will also receive com.foo.bar etc messages. You can stop that by specifying 'additivity="false"' on the loggers.
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
We can add appenders here. It will work if the application is using root logger. for example quartz Scheduler API.