In Struts 2 application we use log4j for logging. I want to log only warn but when i try to use in my log4j.properties
log4j.rootLogger=warn, stdout
it prints error and fatal log too. I want only warn log. I read the levels of log in some tutorials. So I know why error and fatal prints, because they are less priority than warn. How to print warn log only in my console?
My Java code:
import org.apache.log4j.Logger;
public class LogClass {
private static final org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);
public static void main(String[] args) {
log.trace("Trace");
log.debug("Debug");
log.info("Info");
log.warn("Warn");
log.error("Error");
log.fatal("Fatal");
}
}
Any help will be greatly appreciated!
Use the org.apache.log4j.varia.LevelRangeFilter filter. Your XML configuration file could be:
<?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="warn-out" class="org.apache.log4j.FileAppender">
<param name="File" value="warn.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="warn" />
<param name="LevelMin" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="warn" />
<param name="LevelMin" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<root>
<level value="warn" />
<appender-ref ref="warn-out" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
For more options go here.
It looks like properties file configuration does not support filters. The following is taken from log4j's official wiki:
Filter is not supported by PropertiesConfigurator.
EDIT: Added console appender as well to the XML and turned off debug for log4j. Now it should go to both file and console.
Related
My Java project contains following packages -
com.main.log4j.main , com.main.log4j.other.
I want log lines of package "com.main.log4j.other" to be removed from Console log, rather maintain a different log file for the same package. I am using Log4j version 1.2.16, with following log4j.xml config.
<appender name="CONSOLE-LOG" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="..." />
</layout>
</appender>
<appender name="OTHER-LOG" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/Others.log" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="..." />
</layout>
</appender>
<category name="com.main.log4j.other">
<appender-ref ref="OTHER-LOG" />
</category>
<root>
<level value="debug" />
<appender-ref ref="CONSOLE-LOG" />
</root>
Using this config, a separate log file Others.log is created with log line, but those log lines are still present in console log.
I want to exclude them.
Please suggest me how I can configure the log4j.xml?
Set the additivity to false in your "com.main.log4j.other" category :
<category name="com.main.log4j.other" additivity="false">
This will prevent those logs to be also logged in the root logger.
I am using log4j to manage logs in my java application.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="Threshold" value="DEBUG" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="C:/test_reports/infoToolsLog.log"/>
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
Test Code:
import org.apache.log4j.Logger;
public class TestLogFile {
private static final Logger LOGGER = Logger.getLogger(TestLogFile.class);
public static void main(String args[]){
System.out.println("In TestLogFIle");
LOGGER.info("in TestLogFile, info statement");
LOGGER.debug("in TestLogFile debug statement");
LOGGER.error("in TestLogfile, error statement");
int i=10;
try {
int j=i/0;
}catch(Exception e){
LOGGER.error("error occured in TestLogfile : " ,e);
}
}
}
messages displayed in log file :
2017-06-16 11:34:57 INFO TestLogFile:10 in TestLogFile, info statement
2017-06-16 11:34:57 ERROR TestLogFile:12 in TestLogfile, error statement
2017-06-16 11:34:57 ERROR TestLogFile:18 error occured in TestLogfile :
java.lang.ArithmeticException: / by zero...
When <level value="info" />, how to show the debug statements also included in log file.
What changes need to be done to to show info,error and debug statements printed in the log file when <level value="info" />
See the different types of log levels here:
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
This image can help you understand it better:
You have to use DEBUG to show FATAL, ERROR, WARN, INFO and DEBUG
<level value="DEBUG" />
Similarly you have to use TRACE to show FATAL, ERROR, WARN, INFO, DEBUG and TRACE
<level value="TRACE" />
Change
<level value="info" />
to
<level value="debug" />
If you set the level to info then you filter out messages that are lower (trace and debug). Since you want to see the debug messages, you need to set the level to debug.
I would like to configure the logs for a simple app running in a tomcat and i'd like to send all logs except error level ones to one file, and the errors to other one.
How should i configure the log4j.xml to make it works?
<appender name="regular-container" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="regularLogs.log" />
</appender>
<appender name="error-container" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="errorLogs.log" />
</appender>
<logger name="regular-container">
?????????????????????
</logger>
<logger name="error-container">
?????????????????????
</logger>
Try this log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="errorsOnly" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="errorLogs.log" />
<param name="threshold" value="ERROR" />
</appender>
<appender name="regular" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="regularLogs.log" />
<param name="threshold" value="DEBUG" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="errorsOnly" />
<appender-ref ref="regular" />
</root>
</log4j:configuration>
Note that this includes FATAL logging in errorsOnly, though I assume that's okay. Having errorsOnly first with threshold set to ERROR means error level or above are handled by errorsOnly. DEBUG or higher not yet handled by errorsOnly are then handled by regular. I found a similar issue providing another example.
Thanks for answer, but it didnt work. Using filter parameter for each appender, yes.
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="ERROR" />
<param name="levelMax" value="FATAL" />
</filter>
Problem:
We got 2 logs printed in the file like this:
Analysis
1)These 2 logs are printed one after the other in the code and here too but the time difference is 7 minutes which is huge.
2)This has been observed when there is only one War file as well as multiple wars in the Tomcat.
Below is the log4j configuration file to analyse the issue clearly:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
</layout>
</appender>
<appender name="callback" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/subs_engine.log" />
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern"
value="${catalina.base}/logs/%d{yyyy-MM-dd_HH}_subs_engine.gz" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="callback" />
</root>
</log4j:configuration>
Can anyone guide me how to solve or debug this problem:
Is this the problem of log4j?
We are using log4j-1.2.17.jar
#Edit:
Logger.sysLog(LogValues.APP_DEBUG, Utility.class.getName(),"Inside loadHttpReqToBean.");
Logger class Calling syslog method
I just created log4j.xml file like ,
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="512KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/Core_logs/application_log.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n"/>
</layout>
</appender>
<!--sets the priority log level for org.springframework -->
<logger name="org.springframework">
<level value="info" />
</logger>
<!--sets the default priority log level -->
<root>
<priority value="all"></priority>
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
But I have the exception as ,
java.io.FileNotFoundException: F:\Spring_Core_logs\pointel_Aop.log (The system cannot find the path specified)
If I created a folder Core_logs manually in the particular location means, it works fine and log file created.
How to create the folder , if the folder is not exist in a particular location?
EDIT:
This here could also help you/looks like the best solution for you:
Configuring Java FileHandler Logging to create directories if they do not exist
It seems like log4j version 1.2.15 does it. Look for an answer below from Arun P Johny, he posted a piece of code from the log4j sourcecode. I overlooked it because it was not accepted as an answer.
Log4j.xml file for creating in your eclipse
<?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="fileAppender" class="org.apache.log4j.FileAppender">
<param name="Threshold" value="INFO" />
<param name="Append" value="true" />
<param name="File" value="logfile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>"UTF-8"?>