currently i am upgrading log4j version from 1.2.17 to 2.8.2 , to do that i need to configure log4j2.xml, how to convert the below mentioned log4j.xml file to log4j2.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="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout" />
</appender>
<appender name="RootFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/webuif.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p %c: %m%n" />
</layout>
</appender>
<appender name="ApplicationFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/ab.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p %c: %m%n" />
</layout>
</appender>
<category name="com.abc" additivity="false">
<priority value="debug" />
<appender-ref ref="ApplicationFileAppender" />
<appender-ref ref="ConsoleAppender" />
</category>
<root>
<priority value="warn" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RootFileAppender" />
</root>
</log4j:configuration>
Related
Our log4net (config below) gives us the Exception.Message, but that's all. I need it to log the stack trace and all inner exceptions, with the stack trace for each of those.
How do I do this?
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:/temp/AutoTag_Word.log"/>
<param name="AppendToFile" value="true"/>
<param name="MaxSizeRollBackups" value="10"/>
<param name="MaximumFileSize" value="1000KB"/>
<param name="RollingStyle" value="Size"/>
<param name="StaticLogFileName" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] ac.server %-5p %c - %m%n%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<logger name="WindwardArrow.net.windward.arrow.datasource.ado">
<level value="DEBUG"/>
</logger>
</log4net>
This works:
Add our custom log formatter.
Use the below:
<log4net debug="false">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\temp\CleanTemplates.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="2" />
<param name="MaximumFileSize" value="50KB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="Kailua.net.windward.utils.ExceptionFormatter">
<param name="ConversionPattern" value="%date [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
I am using log4j RollingFileAppender to roll the log files based on file size. The max file size we have set is "10 MB". It works fine in tomcat 6 but when we migrate to TomEE Plus.. the log files rolling not happening..the file size keep on growing.
log4j configuration is below:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- Not needed for Production -->
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<!-- Appender to log Gateway information messages -->
<appender name="FA_GATEWAY_INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/freshgate_info.log" />
<param name="encoding" value="UTF-8" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="25" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<!-- For production use this -->
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
<!-- For Development use this pattern -->
<!-- <param name="ConversionPattern" value="%m%n" /> <param name="ConversionPattern"
value="%-4r [%t] %-5p %c %x - %m%n" /> -->
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="DEBUG" />
<param name="AcceptOnMatch" value="false" />
</filter>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="WARN" />
<param name="AcceptOnMatch" value="false" />
</filter>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="ERROR" />
<param name="AcceptOnMatch" value="false" />
</filter>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="FATAL" />
<param name="AcceptOnMatch" value="false" />
</filter>
</appender>
<!-- Appender to log Gateway information messages -->
<appender name="FA_GATEWAY_DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/freshgate_debug.log" />
<param name="encoding" value="UTF-8" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="25" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<!-- For production use this -->
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
<!-- For Development use this pattern -->
<!-- <param name="ConversionPattern" value="%m%n" /> <param name="ConversionPattern"
value="%-4r [%t] %-5p %c %x - %m%n" /> -->
</layout>
</appender>
<!-- Appender to log bC scale command messages -->
<appender name="FA_BC_SCALE_COMMAND_DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/bc_scale_command_debug.log" />
<param name="encoding" value="UTF-8" />
<param name="MaxFileSize" value="5MB" />
<param name="MaxBackupIndex" value="25" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<!-- For production use this -->
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
<!-- For Development use this pattern -->
<!-- <param name="ConversionPattern" value="%m%n" /> <param name="ConversionPattern"
value="%-4r [%t] %-5p %c %x - %m%n" /> -->
</layout>
</appender>
<!-- Appender to log Gateway error messages -->
<appender name="FA_GATEWAY_ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/freshgate_error.log" />
<param name="encoding" value="UTF-8" />
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="1MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<logger name="com.mt.gw">
<level value="DEBUG" />
<appender-ref ref="FA_GATEWAY_DEBUG" />
<appender-ref ref="FA_GATEWAY_INFO" />
<appender-ref ref="FA_GATEWAY_ERROR" />
</logger>
<logger name="BcScaleCommandLogger">
<level value="DEBUG" />
<appender-ref ref="FA_BC_SCALE_COMMAND_DEBUG" />
</logger>
<!-- Blocks the hibernate table creation logs -->
<logger name="com.mt.gw.core.bl.persistence">
<level value="INFO" />
</logger>
<!-- Blocks hibernate logs -->
<logger name="org.hibernate">
<level value="ERROR" />
</logger>
<!-- Blocks both Struts and Catalina logs -->
<logger name="org.apache">
<level value="ERROR" />
</logger>
<!-- Blocks c3po connection pooling log messages -->
<logger name="com.mchange">
<level value="ERROR" />
</logger>
<!-- Blocks Quartz Scheduler logs -->
<logger name="org.quartz">
<level value="ERROR" />
</logger>
<!-- Not needed for Production -->
<root>
<level value="DEBUG" />
<appender-ref ref="CA" />
</root>
<!-- Appender to log bC TAF messages -->
<appender name="FA_BC_TAF_DEBUG_LOGGER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/bc_taf_debug.log" />
<param name="encoding" value="UTF-8" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="25" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<!-- For production use this -->
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
<!-- For Development use this pattern -->
<!-- <param name="ConversionPattern" value="%m%n" /> <param name="ConversionPattern"
value="%-4r [%t] %-5p %c %x - %m%n" /> -->
</layout>
</appender>
<logger name="BcTafLogger">
<level value="DEBUG" />
<appender-ref ref="FA_BC_TAF_DEBUG_LOGGER" />
</logger>
<appender name="FA_TAF_LOGGER_DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/taf_debug.log" />
<param name="encoding" value="UTF-8" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="25" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<appender name="FA_TAF_LOGGER_ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/taf_error.log" />
<param name="encoding" value="UTF-8" />
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="1MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<logger name="TafLogger">
<level value="DEBUG" />
<appender-ref ref="FA_TAF_LOGGER_DEBUG" />
<appender-ref ref="FA_TAF_LOGGER_ERROR" />
</logger>
<appender name="FA_MANAGER_ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/manager_error.log" />
<param name="encoding" value="UTF-8" />
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="1MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<!-- Appender to log Gateway manager messages -->
<appender name="FA_GATEWAY_MANAGER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/manager.log" />
<param name="encoding" value="UTF-8" />
<param name="Threshold" value="INFO" />
<param name="MaxFileSize" value="5MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<appender name="FA_GATEWAY_MANAGER_DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/manager_debug.log" />
<param name="encoding" value="UTF-8" />
<param name="Threshold" value="DEBUG" />
<param name="MaxFileSize" value="5MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<logger name="com.mt.manager">
<level value="DEBUG" />
<appender-ref ref="FA_GATEWAY_MANAGER_DEBUG" />
<appender-ref ref="FA_GATEWAY_MANAGER" />
<appender-ref ref="FA_MANAGER_ERROR" />
</logger>
<appender name="FA_ACTIVEMQ" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${gateway.logs}/logs/activemq.log" />
<param name="encoding" value="UTF-8" />
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="1MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c] - %m%n"/>
</layout>
</appender>
<logger name="org.apache.activemq">
<level value="ERROR" />
<appender-ref ref="FA_ACTIVEMQ" />
</logger>
</log4j:configuration>
I had resolved this issue by separating the log4j.xml into two separate configurations. that solved my problem.
i have a log4j.xml and a scala file. I use slf4j.
this is the xml file
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/config-server/config-server-error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="error"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="info" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/config-server/config-server-info.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="info"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="debug" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/config-server/config-server-debug.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="debug"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value="debug"/>
<appender-ref ref="info"/>
<appender-ref ref="error"/>
<appender-ref ref="debug"/>
</root>
but when I log an error as
logger.error("Error")
that error goes to error.log file. But it also goes to info.log file. can someone help me to get this thing correct.
I use this following configuration and able to see the log messages in separate log files
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/config-server/config-server-error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="error"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="info" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/config-server/config-server-info.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="info"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="debug" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/config-server/config-server-debug.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="debug"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value="debug"/>
</root>
<logger name="debug">
<level value="debug"/>
<appender-ref ref="debug" />
</logger>
<logger name="info">
<level value="info"/>
<appender-ref ref="info" />
</logger>
<logger name="error">
<level value="error"/>
<appender-ref ref="error" />
</logger>
</log4j:configuration>
This is how I am using the logger
import org.apache.log4j.Logger;
public class TestLogger {
private static Logger debugLog = Logger.getLogger("debug");
private static Logger infoLog = Logger.getLogger("info");
private static Logger errorLog = Logger.getLogger("error");
public static void main(String[] args) {
debugLog.debug("Debug message");
infoLog.info("Info message");
errorLog.error("Error message");
}
}
I have the following layout patter in my log4j xml file:
"%d{ISO8601} %c %p [%t] [%x] 9.5.4.RC12 %m%n"
What I want is that when ever I get a log containing message process [proc#] completed, it should be skipped. I mean every log except the one containing this message should be printed. [proc#] will contain the process number of max lenght 4.
What can I desing a filter with this function in my xml config file. If so, then how?
ExpressionFilter can do that.
In a filter definition inside an appender definition, use an expression similar to (note LIKE is the regex match operator):
"MSG LIKE 'process \[.*\] completed'"
See ExpressionFilter javadoc here:
http://logging.apache.org/log4j/companions/apidocs/org/apache/log4j/filter/ExpressionFilter.html
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE">
<param name="Target" value="System.out"/>
<layout>
<param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
</layout>
<filter class="org.apache.log4j.filter.ExpressionFilter">
<param name="expression" value="EXCEPTION ~= com.company.BackendNotAvailableException" />
<param name="acceptOnMatch" value="false"/>
</filter>
</appender>
<root>
<priority value ="INFO" />
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>
Example taken from here: http://blog.trifork.com/2011/08/23/filtering-specific-exceptions-when-using-log4j/
This is how I've configured log4j to filter out by IP address. In particular 127.0.0.1 as illustrated in log4j.xml below. Note, you must also include log4j.dtd alongside log4j.xml. You must use the XML syntax.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CATALINA"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<param name="Encoding" value="UTF-8" />
<param name="File" value="${catalina.base}/logs/catalina" />
<layout class="net.logstash.log4j.JSONEventLayoutV1" />
</appender>
<appender name="CATALINAOUT"
class="org.apache.log4j.ConsoleAppender">
<param name="Append" value="true" />
<param name="Encoding" value="UTF-8" />
<param name="File" value="${catalina.base}/logs/catalina.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
<filter class="org.apache.log4j.filter.ExpressionFilter">
<param name="expression" value="MSG LIKE '127.0.0.1'" />
<param name="acceptOnMatch" value="false"/>
</filter>
</appender>
<appender name="LOCALHOST" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<param name="Encoding" value="UTF-8" />
<param name="File" value="${catalina.base}/logs/localhost" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c- %m%n" />
</layout>
</appender>
<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]">
<level value="INFO" />
<appender-ref ref="LOCALHOST" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="CATALINA" />
<appender-ref ref="CATALINAOUT" />
</root>
</log4j:configuration>
This is my configuration:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Logs\StubLog.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]
" />
<param name="Footer" value="[Footer]
" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\n" />
<param name="Footer" value="[Footer]\n\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<logger name="NHibernate">
<level value="WARN" />
</logger>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
And here is the log:
[Header]
[Footer]
[Header]
2012-10-04 14:22:01,203 [1] INFO Stub.Program Enter application
2012-10-04 14:22:01,328 [1] DEBUG Stub.FrmMain Test
2012-10-04 14:22:03,265 [1] INFO Stub.Program Exit application
[Footer]
Where does the first pair of Header / Footer coming from? Can I eliminate them?
Putting XmlConfigurator.Configure() at more than one places may result in having more than one header and footer pair.
XmlConfigurator.Configure() should be defined only at one place in the solution. Better approach is to put it in AssemblyInfo.cs