log4j errors go to INFO.log - log4j

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");
}
}

Related

Log4net - display full exception information including inner exception(s)

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>

configuring log4j2.xml from log4j.xml

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>

TomEE and Log4j RollingFileAppender

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.

Filter log by matching pattern - log4j

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>

How to restrict debug message printed in the application.log file

I have a log4j.xml
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="appenderConsole" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="appenderFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log\\AuditComp.log"/>
<param name="Append" value="false"/>
<param name="Threshold" value="INFO"/>
<param name="MaxFileSize" value="15MB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<level value="info"/>
<!-- <appender-ref ref="appenderConsole" /> -->
<appender-ref ref="appenderFile"/>
</root>
</log4j:configuration>
I need only Info message present in program code should be printed in log file . But while executing the program it is printing all the message (debug, info , warn). How do i do that?
Try to use this element instead of Threshold parameter under appender element:
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="OFF"/>
</filter>

Resources