我正在尝试使用Logback登录我的java应用程序。我相信我已经正确配置了它,但是当记录错误时
ch.qos.logback.classic.net.SMTPAppender中的INFO [EMAIL] - 即将发送 将SMTP消息“Testing Main”发送到[XXXX@optonline.net]
打印到控制台,之后没有打印任何其他内容,并且永远不会收到电子邮件。如果我在配置中输入了无效的smtp主机或用户名/密码,则在尝试发送时会立即失败,因此它正在建立连接。
我的POM:
<repositories>
<repository>
<id>Java.Net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.6</version>
</dependency>
</dependencies>
logback.xml
<configuration>
<!-- dump status message on the console as they arrive -->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>mail.optonline.net</smtpHost>
<username>xxxxx</username>
<password>xxxxxx</password>
<smtpPort>587</smtpPort>
<to>xxxxx@optonline.net</to>
<from>xxxxx@optonline.net</from>
<subject>Testing %logger
{20}
- %m</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d
{HH:mm:ss.SSS}
[%thread] %-5level %logger
{1}
-%msg%n
</pattern>
</layout>
</appender>
<root level="info">
<appender-ref ref="EMAIL" />
</root>
</configuration>
控制台
10:45:44,596 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
10:45:44,596 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
10:45:44,596 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Users/xxxxx/workspace/logback-test/target/classes/logback.xml]
10:45:44,650 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
10:45:44,658 |-INFO in ch.qos.logback.core.joran.action.StatusListenerAction - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
10:45:44,673 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
10:45:44,688 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [EMAIL]
10:45:44,765 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
10:45:44,765 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [EMAIL] to Logger[ROOT]
10:45:44,766 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
10:45:44,767 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@ddc652f - Registering current configuration as safe fallback point
10:45:44,774 |-INFO in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - SMTPAppender [EMAIL] is tracking [1] buffers
exiting
10:45:44,791 |-INFO in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - About to send out SMTP message "Testing Main" to [xxxxx@optonline.net]
10:45:44,791 |-INFO in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - About to send out SMTP message "Testing Main" to [xxxxx@optonline.net]
这是为了产生测试错误而运行的代码
logger.error("Entering app");
logger.error("exiting app");
System.out.println("exiting");
非常感谢任何有关调试此问题的帮助 谢谢
答案 0 :(得分:7)
我也遇到了这个问题并找到了解决方案。要运行上面的代码,您必须进行一些更改:
1. Add `<STARTTLS>true</STARTTLS>` in your configuration
2. Add <asynchronousSending>false</asynchronousSending>
答案 1 :(得分:2)
这似乎是我使用的SMTP服务器的一个问题。我将Papercut设置为本地smtp服务器,我可以看到它收到消息
答案 2 :(得分:0)
如果您使用 gmail 发送电子邮件,那么以下 logback 配置对我有用。
<springProperty scope="context" name="smtpHost" source="spring.mail.host" />
<springProperty scope="context" name="smtpPort" source="spring.mail.port" />
<springProperty scope="context" name="username" source="spring.mail.username" />
<springProperty scope="context" name="password" source="spring.mail.password" />
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>${smtpHost}</smtpHost>
<smtpPort>${smtpPort}</smtpPort>
<SSL>true</SSL>
<username>${username}</username>
<password>${password}</password>
<to>${username}</to>
<from>${username}</from>
<subject>Error From ERP: %logger{20} - %m</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout"/>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<!-- only 5 log entries on email -->
<bufferSize>5</bufferSize>
</cyclicBufferTracker>
</appender>
虽然我的配置存在于 application.yaml 文件中,可以看出我正试图从那里获取它们。 在我的情况下,smtp 端口设置为 465。