views:

976

answers:

2

In log4j, is there a way to have logger.error("") and logger.debug("") include different output layouts?

I'd like errors to include method names and line numbers, both of which slow down application performance.

EDIT:

After adding apache-log4j-extras, the following config file works.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;

    <appender name="WARNINGS" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="warnings.log"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss} %-5p %c - %m%n%n"/>
        </layout>
        <filter class="org.apache.log4j.filter.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG"/>
            <param name="LevelMax" value="WARN"/>
        </filter>
    </appender>

    <appender name="ERRORS" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="errors.log"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">
            <!-- %M for method name , %L for line number  -->
            <param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss} %-5p %c %M %L - %m%n%n"/>
        </layout>
        <filter class="org.apache.log4j.filter.LevelRangeFilter">
            <param name="LevelMin" value="ERROR"/>
            <param name="LevelMax" value="FATAL"/>
        </filter>
    </appender>

    <logger name="com.foo.bar">
        <appender-ref ref="WARNINGS"/>
        <appender-ref ref="ERRORS"/>
    </logger>

</log4j:configuration>

This code was used to test

// goes to errors.txt
     log.error("error");
//goes to warnings.txt
     log.warn("warn");
+1  A: 

I would just setup two different loggers. One called ERROR and one called DEBUG. Then you can easily setup different output layouts and only call ERROR.error or DEBUG.debug.

Gandalf
Bad advice. Unsuspecting users might follow your advice which would be quite unfortunate.
Ceki
If you feel so please tell me why - don't just blindly say something is bad advice without giving better advice. Why would it be unfortunate?
Gandalf
The better advice was already given by "Alex B". Calling ERROR.error or DEBUG.debug is pretty dumb. Do read the documentation on log4j or get a clue.
Ceki
Lol - I bet your co-workers love working with you.
Gandalf
Even smart people say stupid things from time to time. I would nottake my critical comments on your advice on log4j and on thisparticular occasion as personal.
Ceki
@Gandalf, this is how I am doing it right now and it didn't like having multiple loggers for a class. It seemed too hacky even for me.
sal
+7  A: 

Within your log4j.xml configuration, you can create two appenders, each using a LevelMatchFilter and each with a separate pattern output.

Alex B
I like this. Is there an example you can point me to?
sal