views:

30

answers:

2

Hi All,

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

A: 

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

Adeel Ansari
BoCode
oops pressed enter accidentally.
BoCode
private static Logger logger = Logger.getLogger(Main.class); private static Logger logger2 = Logger.getLogger("AUDITLOG"); /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here logger.info("TESTS"); logger2.info("SDSDS"); System.out.println("ding dong");
BoCode
@BoCode: So, is it working as expected? And please add this code and your configuration to your question. This might get more people to answer your question.
Adeel Ansari
A: 

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>

alejo