Logback SMTPAppender无法找到JNDI位置并启动

时间:2013-06-04 17:45:53

标签: java jndi logback context.xml smtpappender

使用logback-1.0.13.jar和JDK 1.6u34。

我有一个Java Web应用程序(WAR),其中包含以下WEB-INF/classes/logback.xml

<configuration debug="true" scan="true" scanPeriod="5 minutes">
    <appender name="logManager-smtpAppender" class="ch.qos.logback.classic.net.SMTPAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>NEUTRAL</onMismatch>
        </filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <asynchronousSending>false</asynchronousSending>

        <sessionViaJNDI>true</sessionViaJNDI>
        <jndiLocation>java:comp/env/mail/Session-local</jndiLocation>

        <subject>%logger{20} - %m</subject>
        <layout class="ch.qos.logback.classic.html.HTMLLayout"/>
        <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
            <bufferSize>25</bufferSize>
        </cyclicBufferTracker>
    </appender>

    <root level="ALL">
        <appender-ref ref="logManager-smtpAppender" />
    </root>
</configuration>

以下${TOMCAT_HOME}/conf/context.xml(所有网络应用的全局context.xml):

<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <Resource
        name="mail/Session-local"
        auth="Container"
        type="javax.mail.Session"
        mail.smtp.host="my.smtp.server"
        mail.user="smtp_user"
        mail.password="smtp_password"
        mail.transport.protocol="smtp"
        mail.smtp.auth="true"
        mail.smtp.port="25"
        mail.smtp.starttls.enable="true"
    />
</Context>

javax.mail.Authenticator的运行时类路径上让Java Mail API 1.4.3(我已经验证了包含WEB-INF/lib/javax-mail-1.4.3.jar类)。我也在Tomcat的lib目录(${TOMCAT_HOME}/lib)中有它。

我得到以下堆栈跟踪:

13:34:09,178 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:34:09,179 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:34:09,181 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/myuser/sandbox/dsi/workbench/tomcat/6.0.32/apache-tomcat-6.0.32/webapps/myapp/WEB-INF/classes/logback.xml]
13:34:09,469 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 5 minutes
13:34:09,470 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[/home/myuser/sandbox/dsi/workbench/tomcat/6.0.32/apache-tomcat-6.0.32/webapps/myapp/WEB-INF/classes/logback.xml]] every 300 seconds. 
13:34:09,470 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
13:34:11,782 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
13:34:11,830 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [logManager-smtpAppender]
13:34:11,939 |-INFO in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Looking up javax.mail.Session at JNDI location [java:comp/env/mail/Session-local]
13:34:11,992 |-ERROR in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Failed to obtain javax.mail.Session from JNDI location [java:comp/env/mail/Session-local]
13:34:11,992 |-ERROR in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Failed to obtain javax.mail.Session. Cannot start.
13:34:11,993 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ALL
13:34:11,996 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [logManager-smtpAppender] to Logger[ROOT]
13:34:11,997 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:34:12,000 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@63220fd1 - Registering current configuration as safe fallback point
13:39:49,988 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:49,988 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:50,598 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:51,275 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].
13:39:51,833 |-WARN in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Attempted to append to non started appender [logManager-smtpAppender].

为什么Logback无法从Tomcat的context.xml检索我的JNDI邮件会话?这会阻止SMTPAppender启动并能够记录消息。提前谢谢!

1 个答案:

答案 0 :(得分:2)

首先,不要将邮件JAR放在两个位置,而是放在Tomcat的lib文件夹中。如果您使用的是Maven,请使用相应provided的{​​{1}}范围,以便不将其打包到WAR中。

其次,<dependency />中的邮件会话是否有<resource-ref />个元素?如果没有,请查看here以获取样本。

另一种方法是使会话web.xml全局(<resource />)并从server.xml链接到context.xml,如here所述。

HTH, 的Jukka