0

我正在开发一个移动应用程序,我正在使用 log4j 来显示信息。我想使用 log4j 在控制台上显示它正确执行的信息,但是当我尝试添加 SMTPAppender 以在发生错误时发送电子邮件时遇到问题。它没有显示错误,但是当我添加 SMTPAppender 启动它时,tomcat 无法正确启动。

我正在使用 Apache Tomcat 7.0.23

这是我的 log4j.properties 文件

log4j.rootCategory=TRACE, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} [application] %-5p %m%n


log4j.rootCategory=ERROR, mail

log4j.appender.mail=org.apache.log4j.net.SMTPAppender
#defines how often emails are send
log4j.appender.mail.BufferSize=1
log4j.appender.mail.SMTPHost=smtp.gmail.com
log4j.appender.mail.SMTPUsername=*******   #not the username im using
log4j.appender.mail.SMTPPassword=*******   #not the password im using
log4j.appender.mail.From=info@gmail.com
log4j.appender.mail.To=noc@gmail.com
log4j.appender.mail.Subject="Application.log error occurred"
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d{ISO8601} [application] %-5p %m%n

这是我的控制台

Found binding in [jar:file:/C:/Documents%20and%20Settings/noconnor/.m2/repository/org/slf4j/slf4j-simple/1.6.1/slf4j-simple-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Documents%20and%20Settings/noconnor/.m2/repository/org/slf4j/slf4j-log4j12/1.6.2/slf4j-log4j12-1.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

这是添加第二个附加程序的正确方法还是我在某处犯了错误?

4

1 回答 1

2

You can only have one log4j.rootCategory property.

You can have multiple appenders:

log4j.rootCategory=TRACE, stdout, mail

But you probably don't want all messages go to mail. For this, you need to add priority filter to your mail appender, but I think that's only possible to do using XML syntax instead of log4j.properties.

Here is an example for using both appenders, but "mail" appender is only activated for ERROR and FATAL messages.

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 
    <param name="Target" value="System.out"/> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%d{ISO8601} [application] %-5p %m%n"/> 
    </layout> 
  </appender> 

  <appender name="mail" class="org.apache.log4j.net.SMTPAppender">
    <!-- mail configuration here... -->

    <layout class="org.apache.log4j.PatternLayout"> 
        <param name="ConversionPattern" value="%d{ISO8601} [application] %-5p %m%n"/> 
    </layout> 

    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="levelMin" value="ERROR" />
        <param name="levelMax" value="FATAL" />
    </filter>
  </appender>

  <root> 
    <priority value="trace" /> 
    <appender-ref ref="stdout" /> 
    <appender-ref ref="mail" />
  </root>

</log4j:configuration>

You need to add more mail configuration options to match your original properties file. This file must be called "log4j.xml" and must be used instead of "log4j.properties" (remove this one).

于 2012-05-04T14:09:56.977 回答