Reputation:
Is it possible to have "debug" and "info" output written to the console while the "info" output is only written to some log file? For example, given this logging:
LOG.debug(fileContent);
LOG.info(fileLength);
What does a corresponding log4j.xml
look like?
Upvotes: 8
Views: 12081
Reputation: 10987
Do as rwwilden suggested but remove this part:
<logger name="com.mycompany.mypackage.MyClass">
<level value="info"/>
<appender-ref ref="otherAppender" />
</logger>
And add <param name="Threshold" value="INFO"/>
under the otherAppender.
Upvotes: 0
Reputation:
Ok, I've got it now:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
...
</appender>
<appender name="otherAppender"
class="org.apache.log4j.FileAppender FileAppender">
<param name="Threshold" value="INFO"/>
...
</appender>
<root>
<priority value="debug" />
<appender-ref ref="console" />
<appender-ref ref="otherAppender" />
</root>
</log4j:configuration>
Thanks for your help!
Upvotes: 11
Reputation:
With the configuration from Eddie I can only get the "info" output for MyClass. But what I would like to have is that the "info" output of MyClass goes to a file AND the "debug" output of MyClass goes to console.
Upvotes: 0
Reputation: 32094
That is definitely possible. The configuration would look something like this (not checked for syntactic correctness):
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
...
</appender>
<appender name="otherAppender"
class="org.apache.log4j.FileAppender FileAppender">
...
</appender>
<logger name="com.mycompany.mypackage.MyClass">
<level value="info"/>
<appender-ref ref="otherAppender" />
</logger>
<root>
<priority value="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
All debug and info messages go to the console
appender. Info messages go to otherAppender
.
Upvotes: 5