BoCode
BoCode

Reputation: 925

redirecting log4j messages from a method to a different log file

Right now i use log4j in its plain vanilla/out-of-the-box form. I've a log4j.properties file in the class path and various logger messages littered across the web application. Now i'm interested in redirecting log messages from a method "abc" in package "xyz" to go to a specific log file "pqr". I dont want all the logger messages from package xyz to go to "pqr" but just from that one method ("abc") in the package.

How do i achieve this?

TIA Bo

Upvotes: 1

Views: 3857

Answers (2)

alejo
alejo

Reputation: 307

Here's the config file I'm using. This will send some specific messages to a file and others to the console. This may help you a little bit hopefully. This is used in a standalone aplication.

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- ============================== -->
<!-- Append SQL messages to a file. -->
<!-- ============================== -->
<appender name="SQL" class="org.apache.log4j.RollingFileAppender">
    <param name="Threshold" value="TRACE" />
    <param name="File" value="sql-statement.log" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="5000KB" />
    <param name="MaxBackupIndex" value="100" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%m%n" />
    </layout>
</appender>

<!-- ============================== -->
<!-- Append messages to the console -->
<!-- ============================== -->
<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="[%p] %m%n" />
    </layout>
</appender>

<!-- =============================== -->
<!-- Application specific categories -->
<!-- =============================== -->
<category name="com.edusoft.crashtest.qsbi">
    <priority value="DEBUG" />
    <appender-ref ref="CONSOLE" />
</category>
<category name="com.edusoft.crashtest.qsbi.printer" additivity="true">
    <priority value="TRACE" />
    <appender-ref ref="SQL" />
</category>

<!-- Setup the Root category -->
<root>
    <priority value="ERROR" />
</root>

Upvotes: 2

Adeel Ansari
Adeel Ansari

Reputation: 39907

I don't think it can be that granular. Why not pull that method out to a separate class?

Upvotes: 0

Related Questions