Log4j SMTPappender错误

时间:2010-08-29 21:36:01

标签: java log4j

我正在尝试使用log4j SMTPappender获取以下内容。错误。所有其他appender工作正常。

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

<!--                          -->
<!-- Declare the SMTPAppender -->
<!--                          -->
<appender name="EMAIL"  class="org.apache.log4j.net.SMTPAppender">
    <param name="BufferSize" value="512" />
    <param name="SMTPHost" value="relaymail.xxx.com" />
    <param name="From" value="xxx@xxx.com" />
    <param name="To" value="xxx@xxx.com" />
    <param name="Subject" value="[SMTPAppender] Application message" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
          value="[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="ERROR"/>
        <param name="LevelMax" value="FATAL"/>
    </filter>
</appender>

<!--                           -->
<!-- setup log4j's root logger -->
<!--                           -->
<root>
    <level value="all" />
    <appender-ref ref="EMAIL" />
</root>

我的Java代码

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class simpandfile{
   static Logger logger = Logger.getLogger(simpandfile.class);
   public static void main(String args[]) {
      DOMConfigurator.configure("xmllog4jconfig.xml");
      System.setProperty("mail.smtps.host", "smtp.gmail.com:465");
      System.setProperty("mail.smtps.auth", "true");
      logger.debug("Here is DEBUG");
      logger.info("Here is some INFO blah");
      logger.warn("Here is some WARN");
      logger.error("Here");
      logger.debug("prasanna Test");
   }
}
**Exceptions** 
log4j:WARN Continuable parsing error 31 and column 23
log4j:WARN The content of element type "log4j:configuration" must match "(render
er*,throwableRenderer?,appender*,plugin*,(category|logger)*,root?,(categoryFacto
ry|loggerFactory)?)".
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/internet/A
ddressException
        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
        at java.lang.Class.getConstructor0(Unknown Source)
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.ja
va:247)
        at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurat
or.java:176)
        at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfi
gurator.java:191)
        at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOM
Configurator.java:523)
        at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:4
92)
        at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:1001)

        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java
:867)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java
:755)
        at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:8
91)
        at simpandfile.main(simpandfile.java:6)
Caused by: java.lang.ClassNotFoundException: javax.mail.internet.AddressExceptio
n
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 15 more

2 个答案:

答案 0 :(得分:2)

您需要在类路径中使用mail.jar和activation.jar

答案 1 :(得分:0)

JoseK对于第二个错误是正确的。但是,第一个错误:

  

log4j:WARN可持续解析错误   31和第23列log4j:WARN   元素类型的内容   “log4j:configuration”必须匹配   “(渲染   ?呃*,throwableRenderer,附加器*,*插件(类别|记录器)*,根,(categoryFacto?   RY |的LoggerFactory))”

?。

通常意味着两件事之一:

  • 您没有根记录器
  • 您没有正确关闭&lt; log4j&gt;标签

由于你有一个根记录器,你可以通过确保文件中的最后一个标记如下来摆脱你的第一个错误:

</log4j:configuration>

在您的情况下,它丢失或采用以下格式:

</log4j>