尝试使用logback和spring boot在gmail上发送邮件时出错

时间:2016-05-30 14:14:55

标签: java spring email spring-boot logback

我使用的是春季启动:1.4.0。在logback中使用以下配置,我正在尝试向gmail发送邮件。

<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
    <smtpHost>smtp.gmail.com</smtpHost>
    <smtpPort>25</smtpPort>
    <SSL>true</SSL>
    <username>*</username>
    <password>*</password>

    <to>*</to>
    <from>*</from>
    <subject>TESTING: %logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>%date %-5level %logger{35} - %message%n</pattern>
    </layout>
</appender>

<root level="DEBUG">
    <appender-ref ref="EMAIL" />
</root>

但是得到以下异常

Exception in thread "main" java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.joran.action.NestedBasicPropertyIA - Unexpected aggregationType AS_BASIC_PROPERTY_COLLECTION
at       org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:152)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.reinitialize(LogbackLoggingSystem.java:195)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:65)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:50)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:106)
at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:289)
at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:262)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:231)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:207)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:121)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:68)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:336)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1182)

主程序Spring Boot

public static void main(String[] args) {
    System.setProperty("log.name", "dynamicLogs");
    logger.debug("Start running debug");
    SpringApplication.run(WebApplication.class, args);
    logger.error("Start running error");            
}

如果我尝试运行测试java程序,它运行正常。使用spring boot运行时不确定是什么问题。是否需要设置任何属性?

2 个答案:

答案 0 :(得分:1)

logback版本1.1.7存在一些问题。 Spring Boot 1.3.4和1.3.5依赖于这个版本,所以1.4.0也可能。

您可以尝试强制使用较旧/较新版本的logback。

https://github.com/logstash/logstash-logback-encoder/issues/160

中的更多信息

答案 1 :(得分:1)

您遇到了<properties> <logback.version>1.1.6</logback.version> </properties> 字段的问题。请参阅THIS错误报告。您必须等待1.1.8或降级到版本1.1.6。 使用Spring Boot,您可以执行以下操作

@Autowired
ConfigurableListableBeanFactory configurableListableBeanFactory;

@Autowired
MyService myService;

public MuleEventContext onCall(MuleEventContext eventContext){
Properties properties = new Properties();

    // get properties from the database
    Map<String,String> propertiesMap = getMuleAppPropertiesFromDB();
    if(null != propertiesMap && !CollectionUtilsIntg.isEmpty(propertiesMap))
        properties.putAll(propertiesMap);

PropertySourcesPlaceholderConfigurer cfg = new PropertySourcesPlaceholderConfigurer();
cfg.setProperties(properties);
cfg.postProcessBeanFactory(configurableListableBeanFactory);
}