开发者

Email log4j errors to different groups

开发者 https://www.devze.com 2023-04-09 22:20 出处:网络
Am using log4j for my error reporting. Ive set my log 4j to also append errors to emails. However i need it to send emails to separate groups depending on the level of the message. For example

Am using log4j for my error reporting. Ive set my log 4j to also append errors to emails. However i need it to send emails to separate groups depending on the level of the message. For example

      info messages go to management- john@management.com, peter@management.com, simon@management.com                   
       debug messages go to programmers - paul@programmer.com, matt@programmer.com
       warn messages go to administrator - admin@admin.com

In such a fashion, is it possible? And has anyone implemented said procedure (a sample properties file would be app开发者_运维百科reciated)


Is should be easy to configure something like this using the SMTPAppender from log4j. But be aware, that emailing logs takes quite a bit of load, especially if you consider mailing DEBUG reports to developers.


You can use a log4j Filter to do this (http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/Filter.html)

The example below writes messages to different files according to the log level. This can be easily adapted to an SMTPAppender instead of a FileAppender.

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

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="management" class="org.apache.log4j.FileAppender">
        <param name="File" value="management.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="INFO" />
            <param name="LevelMax" value="INFO" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
    </appender>

    <appender name="programmers" class="org.apache.log4j.FileAppender">
        <param name="File" value="programmers.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="DEBUG" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
    </appender>

    <appender name="admin" class="org.apache.log4j.FileAppender">
        <param name="File" value="admin.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="WARN" />
            <param name="LevelMax" value="WARN" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
    </appender>

    <category name="a">
        <level value="ALL" />
        <appender-ref ref="management"/> 
        <appender-ref ref="programmers"/>
        <appender-ref ref="admin"/> 
    </category>

</log4j:configuration>
0

精彩评论

暂无评论...
验证码 换一张
取 消