You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Mikael Ståldal <mi...@univits.com> on 2007/07/04 10:34:40 UTC

How do I configure Log4J to send different levels to different appenders

I have two appenders, MAIN_LOG and DEBUG_LOG. I have the loggers set up to match Java class 
hierarchy as recommended. I have two interesting codebases, "com.mycompany" and 
"com.othercompany".

I want to send DEBUG and higher from "com.mycompany" and WARN and higher from 
"com.othercompany" to MAIN_LOG. And I want to send DEBUG and higher from "com.mycompany" and 
DEBUG and higher from "com.othercompany" to DEBUG_LOG.

Finally I want to handle stuff outside "com.mycompany" or "com.othercompany" (such as 
"org.thirdparty") by sending ERROR and higher to MAIN_LOG and INFO and higher to DEBUG_LOG.

How do I accomplish that?

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How do I configure Log4J to send different levels to different appenders

Posted by Mikael Ståldal <mi...@univits.com>.
Curt Arnold wrote:
> Set root threshold to debug, attach both appenders to root.  Write 
> custom filters for your rules and attach the filters to the appenders.

By "custom filter", do you mean writing my own Java class implementing 
org.apache.log4j.spi.Filter?

Is it really not possible to configure log4j in the way I want without writing Java code?
That's definitly something that needs to be fixed for log4j 1.3.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How do I configure Log4J to send different levels to different appenders

Posted by Curt Arnold <ca...@apache.org>.
On Jul 4, 2007, at 7:16 AM, Mikael Ståldal wrote:

> Norbert Toth-Gati wrote:
>> Concerning the :"log4j:ERROR Attempted to append to closed  
>> appender named
>> [MAIN_LOG]." Please follow the link, as a similar problem is  
>> discussed:
>> http://www.mail-archive.com/log4j-user@jakarta.apache.org/ 
>> msg01365.html
>
> I have read that thread, and it explains why the solution you  
> proposed doesn't work. But it doesn't give me any working solution.
>
>> Yep, you cannot use root-element for that.
>
> So what should I use?
>

Set root threshold to debug, attach both appenders to root.  Write  
custom filters for your rules and attach the filters to the appenders.

We have back ported some filters from the log4j 1.3 development  
effort to the "extras" companion.  It may be possible to use an  
ExpressionFilter, but I've never used it and someone would need help  
you with the syntax.




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How do I configure Log4J to send different levels to different appenders

Posted by Mikael Ståldal <mi...@univits.com>.
Norbert Toth-Gati wrote:
> Concerning the :"log4j:ERROR Attempted to append to closed appender named
> [MAIN_LOG]." Please follow the link, as a similar problem is discussed:
> http://www.mail-archive.com/log4j-user@jakarta.apache.org/msg01365.html

I have read that thread, and it explains why the solution you proposed doesn't work. But it 
doesn't give me any working solution.

> Yep, you cannot use root-element for that.

So what should I use?

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How do I configure Log4J to send different levels to different appenders

Posted by Norbert Toth-Gati <no...@gmail.com>.
Hi,
Concerning the :"log4j:ERROR Attempted to append to closed appender named
[MAIN_LOG]." Please follow the link, as a similar problem is discussed:
http://www.mail-archive.com/log4j-user@jakarta.apache.org/msg01365.html

Yep, you cannot use root-element for that.

Regards,
Norbert

On 7/4/07, Mikael Ståldal <mi...@univits.com> wrote:
>
> Norbert Toth-Gati wrote:
> > You may try to configure the following categories:
> >
> >    <category name="com.mycompany">
> >        <priority value="DEBUG"/>
> >        <appender-ref ref="MAIN_LOG"/>
> >        <appender-ref ref="DEBUG_LOG"/>
> >    </category>
> >
> >    <category name="com.othercompany">
> >        <priority value="WARN"/>
> >        <appender-ref ref="MAIN_LOG"/>
> >    </category>
> >
> >    <category name="com.othercompany">
> >        <priority value="DEBUG"/>
> >        <appender-ref ref="DEBUG_LOG"/>
> >    </category>
>
> It doesn't work. I end up with an empty MAIN_LOG and the following error
> message:
>
> log4j:ERROR Attempted to append to closed appender named [MAIN_LOG].
>
> > You may need to configure all the thirdparty logs to end up in the
> > following appenders:
> >
> >    <category name="org.thirdparty">
> >        <priority value="ERROR"/>
> >        <appender-ref ref="MAIN_LOG"/>
> >    </category>
> >    <category name="org.thirdparty">
> >        <priority value="INFO"/>
> >        <appender-ref ref="DEBUG_LOG"/>
> >    </category>
>
> I would prefer to have a catch-all rule. I guess that the <root> element
> can be used for
> that, but you can only have one <root> element.
>
> Here is the complete log4j.xml I tested with:
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
>
>     <appender name="DEBUG_LOG" class="
> org.apache.log4j.DailyRollingFileAppender">
>        <param name="File" value="debug.log" />
>        <param name="Append" value="false" />
>        <layout class="org.apache.log4j.PatternLayout">
>            <param name="ConversionPattern" value="%d %-5p [%t] %c -
> %m%n"/>
>        </layout>
>     </appender>
>
>     <appender name="MAIN_LOG" class="
> org.apache.log4j.DailyRollingFileAppender">
>        <param name="File" value="main.log" />
>        <param name="Append" value="false" />
>        <layout class="org.apache.log4j.PatternLayout">
>            <param name="ConversionPattern" value="%d %-5p [%t] %c -
> %m%n"/>
>        </layout>
>     </appender>
>
>     <category name="com.mycompany">
>         <priority value="DEBUG"/>
>         <appender-ref ref="MAIN_LOG"/>
>         <appender-ref ref="DEBUG_LOG"/>
>     </category>
>
>     <category name="com.othercompany">
>         <priority value="WARN"/>
>         <appender-ref ref="MAIN_LOG"/>
>     </category>
>
>     <category name="com.othercompany">
>         <priority value="DEBUG"/>
>         <appender-ref ref="DEBUG_LOG"/>
>     </category>
>
> </log4j:configuration>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: How do I configure Log4J to send different levels to different appenders

Posted by Mikael Ståldal <mi...@univits.com>.
Norbert Toth-Gati wrote:
> You may try to configure the following categories:
> 
>    <category name="com.mycompany">
>        <priority value="DEBUG"/>
>        <appender-ref ref="MAIN_LOG"/>
>        <appender-ref ref="DEBUG_LOG"/>
>    </category>
> 
>    <category name="com.othercompany">
>        <priority value="WARN"/>
>        <appender-ref ref="MAIN_LOG"/>
>    </category>
> 
>    <category name="com.othercompany">
>        <priority value="DEBUG"/>
>        <appender-ref ref="DEBUG_LOG"/>
>    </category>

It doesn't work. I end up with an empty MAIN_LOG and the following error message:

log4j:ERROR Attempted to append to closed appender named [MAIN_LOG].

> You may need to configure all the thirdparty logs to end up in the 
> following appenders:
> 
>    <category name="org.thirdparty">
>        <priority value="ERROR"/>
>        <appender-ref ref="MAIN_LOG"/>
>    </category>
>    <category name="org.thirdparty">
>        <priority value="INFO"/>
>        <appender-ref ref="DEBUG_LOG"/>
>    </category>

I would prefer to have a catch-all rule. I guess that the <root> element can be used for 
that, but you can only have one <root> element.

Here is the complete log4j.xml I tested with:

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

    <appender name="DEBUG_LOG" class="org.apache.log4j.DailyRollingFileAppender">
       <param name="File" value="debug.log" />
       <param name="Append" value="false" />
       <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="%d %-5p [%t] %c - %m%n"/>
       </layout>
    </appender>

    <appender name="MAIN_LOG" class="org.apache.log4j.DailyRollingFileAppender">
       <param name="File" value="main.log" />
       <param name="Append" value="false" />
       <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="%d %-5p [%t] %c - %m%n"/>
       </layout>
    </appender>

    <category name="com.mycompany">
        <priority value="DEBUG"/>
        <appender-ref ref="MAIN_LOG"/>
        <appender-ref ref="DEBUG_LOG"/>
    </category>

    <category name="com.othercompany">
        <priority value="WARN"/>
        <appender-ref ref="MAIN_LOG"/>
    </category>

    <category name="com.othercompany">
        <priority value="DEBUG"/>
        <appender-ref ref="DEBUG_LOG"/>
    </category>

</log4j:configuration>


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How do I configure Log4J to send different levels to different appenders

Posted by Norbert Toth-Gati <no...@gmail.com>.
Hi Mikael,

You may try to configure the following categories:

    <category name="com.mycompany">
        <priority value="DEBUG"/>
        <appender-ref ref="MAIN_LOG"/>
        <appender-ref ref="DEBUG_LOG"/>
    </category>

    <category name="com.othercompany">
        <priority value="WARN"/>
        <appender-ref ref="MAIN_LOG"/>
    </category>

    <category name="com.othercompany">
        <priority value="DEBUG"/>
        <appender-ref ref="DEBUG_LOG"/>
    </category>

You may need to configure all the thirdparty logs to end up in the following
appenders:

    <category name="org.thirdparty">
        <priority value="ERROR"/>
        <appender-ref ref="MAIN_LOG"/>
    </category>
    <category name="org.thirdparty">
        <priority value="INFO"/>
        <appender-ref ref="DEBUG_LOG"/>
    </category>

Hope that helps.

Regards,
Norbert



On 7/4/07, Mikael Ståldal <mi...@univits.com> wrote:
>
> I have two appenders, MAIN_LOG and DEBUG_LOG. I have the loggers set up to
> match Java class
> hierarchy as recommended. I have two interesting codebases, "com.mycompany"
> and
> "com.othercompany".
>
> I want to send DEBUG and higher from "com.mycompany" and WARN and higher
> from
> "com.othercompany" to MAIN_LOG. And I want to send DEBUG and higher from "
> com.mycompany" and
> DEBUG and higher from "com.othercompany" to DEBUG_LOG.
>
> Finally I want to handle stuff outside "com.mycompany" or "
> com.othercompany" (such as
> "org.thirdparty") by sending ERROR and higher to MAIN_LOG and INFO and
> higher to DEBUG_LOG.
>
> How do I accomplish that?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>