My log4j.xml configuration was like ,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="fileAppender1" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/Testing/Project_moduleOne.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<appender name="fileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/PAD_Testing/Project_moduleTwo.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="com.comp.logger1">
<appender ref="fileAppender1"/>
</logger>
<logger name="com.comp.logger2">
<appender ref="fileAppender2" />
</logger>
<!--sets the default priority log level -->
<root>
<priority value="all"></priority>
<appender-ref ref="fileAppender1" />
<appender-ref ref="fileAppender2" />
</root>
</log4j:configuration>
And two log files also created in the specified location.
I need to know how to log two different data's in these two different log_files independently in JAVA class.
For example,
Logger logOne = Logger.getLogger("com.comp.logger1");
Logger logTwo = Logger.getLogger("com.comp.logger2");
The above code is not working for me. All the log informations are logged to both the created two log files. I need the separation of logging data.
My need is ,
I want to create two log file. Because my project has two modules and log each module in separate log files.
After that , I have to log each module logging data independently .
Please make sure I used the logger name for logging in my java class correctly.
Any new or complete examples using log4j.xml are greatly appreciated.
EDIT :
If I add the additivity="false" in the logger as,
<logger name="com.comp.logger1" additivity="false">
<appender ref="fileAppender1" />
</logger>
<logger name="com.comp.logger2" additivity="false">
<appender ref="fileAppender2" />
</logger>
The log data didn't logged in the created log file.The log file was empty.
Please make sure my <root>...</root> is correct.
You problem is with the <root> section, the root section captures all logs, and you've told it to log to both appenders...
You could remove it, or set additivity="false" on each of your logger elements - this will tell log4j not to log the same log through 'root' if it's already been logged through one of your 'logger's.
Edit: you don't say which version of log4j you are using, but I'm using log4j-1.2.16, and using the file in your post, it fails completely for me because it doesn't like the <appender ref="fileAppender1"/>, it wants them to be <appender-ref ref="fileAppender1"/> (note the -ref after the appender). If I add these in, and also add the additivity attributes that I suggested, then for me it works as you expect.
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 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>
Before with one class, logging was working well, but after I added 2nd Java class, it is logging twice in this second class. Can anyone help me, how to make it log only once. The problem seems to be in this log4j.xml file, because if I comment out appender, it is logging once, but I don't want to change the code in log4j sufficiently, because then I am afraid logging will not work properly in the 1st class.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<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: [%d{MMM-dd HH:mm:ss,SSS}] %c{3} - %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="file" value="/user/Dave/log.out"/>
<param name="immediateFlush" value="true"/>
<param name="threshold" value="debug"/>
<param name="append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: [%d{MMM-dd HH:mm:ss,SSS}] %c{3} - %m%n"/>
</layout>
</appender>
<logger name="com.asaqa.score">
<level value="all"/>
</logger>
<!-- Root Logger -->
<root>
<priority value="error"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
By default loggers inherit appenders, and each appender creates log entries. In your case the "com.asaqa.score" logger inherits from root. You can turn this off by setting additivity to false. And you should complete the config of your logger to include an appender
<logger name="com.asaqa.score" additivity="false">
<level value="all"/>
<appender-ref ref="console"/>
</logger>
Also, the error below suggests you're using the old version of log4j. You may want to consider upgrading to log4j 2. In fact, it's recommended by Apache themselves.
On August 5, 2015 the Logging Services Project Management Committee announced that Log4j 1.x had reached end of life. For complete text of the announcement please see the Apache Blog. Users of Log4j 1 are recommended to upgrade to Apache Log4j 2.
When you do, note that there are some changes to the config syntax.
Logs are not written properly
slf version slf4j-api-1.7.5.jar, slf4j-log4j12-1.7.5.jar, log4j-1.2.16.jar
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- log4j generic catchall for adapters. -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Example DailyRollingFile appender, this is the preferred logging appender -->
<appender name="CommonAdapterAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/opt/adapter.log" />
<param name="encoding" value="UTF-8" />
<param name="append" value="true" />
<!-- Rollover at the top of every hour -->
<param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss.SSS-zzz} %5p [%t] %c{1} - %m%n" />
</layout>
</appender>
<!-- Categories -->
<category name="com.other" additivity="false">
<priority value="warn" />
</category>
<logger name="com.adapter" additivity="false">
<level value="debug" />
<appender-ref ref="CommonAdapterAppender"/>
</logger>
<logger name="com.adaptations" additivity="false">
<level value="debug" />
<appender-ref ref="CommonAdapterAppender"/>
</logger>
<root>
<priority value="error" />
<appender-ref ref="CommonAdapterAppender" />
<!-- <appender-ref ref="SyslogAppender"/> -->
</root>
</log4j:configuration>
The above log4j file is used by multiple adapters. First time it is writing to adapter.log and after that only some component logs are written. Also I have noticed that after the second time it is writing few logs to adapter.log..
I cannot understand what is going wrong here. Can someone please help me out?
I believe you should be using a single shared log 4J configuration and instance for all your adapters if you want to use the the same log file.
As is, Log 4j instances are probably competing for the control of the file.
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.