0

abcI am trying to log messages in two different log files using log4j API. Below is my log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender class="org.apache.log4j.RollingFileAppender" name="OnlineFile">
    <param value="UTF-8" name="Encoding"/>
    <param value="D://abc//logs//Online.log" name="File"/>
    <param value="500" name="MaxBackupIndex"/>
    <param value="10000KB" name="MaxFileSize"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param value="%d{ISO8601} : %-6.6c : %-5p : %X{APP} : %-7X{USER} : %-2X{MODULE} : %-20X{REFDATA}  : %m%n" name="ConversionPattern"/>
    </layout>
</appender>
<appender class="org.apache.log4j.RollingFileAppender" name="ABCFile">
    <param value="UTF-8" name="Encoding"/>
    <param value="D://abc//logs//Sample.log" name="File"/>
    <param value="500" name="MaxBackupIndex"/>
    <param value="10000KB" name="MaxFileSize"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param value="%d{ISO8601} : %-6.6c : %-5p : %X{APP} : %-7X{USER} : %-2X{MODULE} : %-20X{REFDATA}  : %m%n" name="ConversionPattern"/>
    </layout>
</appender>
<logger name="Online">
    <level value="debug"/>
</logger>
 <logger name="ABC">
    <level value="INFO"/>
    <appender-ref ref="ABCFile"/>
</logger>
<root>
    <level value="debug"/>
    <appender-ref ref="OnlineFile"/> 
</root>

It is somehow only logging in Online.log.Even fetching the correct using in the following manner fails to log in Sample.log

Logger.getLogger("ABCFile").info("My message");

Not able to figure out what's wrong! Any help?

sherry
  • 352
  • 2
  • 8
  • 17

2 Answers2

0

"ABCFile" is the name of your appender, your log is named "ABC". try:

Logger.getLogger("ABC").info("My message");
Grim
  • 1,608
  • 9
  • 12
  • Yeah got it! Thank you :-) Another doubt, while I do this, it also logs to the other file which is Online.log? Why? – sherry Jun 19 '13 at 09:44
  • in log4j loggers are hierarchical, and if no explicit parent is specified for a logger, the root logger is implicitly used, so every message that is accepted by the logger (depending on its level) will also be logged to the appenders of all its parents climbing the hierarchy (in your case, just the root logger). This behaviour can be disabled by setting the "additivity" property of the child logger to false, thus "blocking" propagation of log messages.For more info take a look at the [log4j manual](http://logging.apache.org/log4j/1.2/manual.html) – Grim Jun 19 '13 at 10:31
  • Yes I did that. One last query can I have the same level (say DEBUG) defined for both the loggers? – sherry Jun 19 '13 at 11:44
  • Of course. Every logger keeps track of its own level, so nothing prevent you from setting the same level on both. Just keep in mind (when using different levels) that only the level on the logger that receives the message will decide if the message is to be logged, regardless of the level of any parent logger it may be propagated to. – Grim Jun 20 '13 at 09:18
0

check getLogger() API. You should use logger name not File Appender name. Change code like below.

Logger.getLogger("ABC").info("My message");

Mahadev Shinde
  • 119
  • 3
  • 9