如何在Spring下配置Logback

时间:2015-08-15 08:42:25

标签: java spring logback

配置Logback的大多数示例都使用logback.xml。但是,我的应用程序使用profilesPropertySourcesPlaceholderConfigurer的Spring功能将特定于环境的配置注入需要它的组件中。像我的其他Spring组件一样,以编程方式配置Logback的正确方法是什么?

2 个答案:

答案 0 :(得分:6)

正如orid所指出的,在Spring中使用Logback的官方方法是使用the Logback Spring extension

非常酷的是Logback Spring扩展在Spring或servlet之前启动并存储日志消息,直到配置了Appender。 (简单的appender将立即可用,而需要Spring的那些代理通过DelegatingLogbackAppender)。

此外,它允许您:

  • 使用SLF4J的java.util.logging网桥。
  • 使用Spring resource pathsystem property占位符指定logback.xml位置。
  • webapp.root内使用预设系统属性logback.xml(解压缩WAR目录的路径)来设置日志文件路径等。

答案 1 :(得分:3)

在我们的一个项目中,我们将其编码为:

import java.io.IOException;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

@Component
public class InitializationService implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${logbackErrorMailPassword}")
    private String logbackErrorMailPassword;

    @Value("${supportEmail}")
    private String supportEmail;

    @Value("${spring.profiles.active}")
    private String env;

    @Value("${log.dir}")
    private String logDir;

    @Value("${log.name}")
    private String logName;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        try {
            configureLogback();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void configureLogback() throws IOException {

        // assume SLF4J is bound to logback in the current environment
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        try {
            JoranConfigurator jc = new JoranConfigurator();
            jc.setContext(context);
            context.reset(); // override default configuration
            // inject the name of the current application as "application-name"
            // property of the LoggerContext
            context.putProperty("LOG_DIR", logDir);
            context.putProperty("LOG_NAME", logName);

            context.putProperty("ERROR_MAIL_PASSWORD", logbackErrorMailPassword);
            context.putProperty("SUPPORT_EMAIL_ID", supportEmail);
            context.putProperty("ENV", env);
            //jc .doConfigure(servletContext.getRealPath("/WEB-INF/my-logback.xml"));
            jc.doConfigure(new ClassPathResource("my-logback.xml").getInputStream());
        } catch (JoranException je) {
              // StatusPrinter will handle this
        }
        StatusPrinter.printInCaseOfErrorsOrWarnings(context);

    }
}

my-logback.xml居住在src/main/resources,看起来像这样:

<configuration debug="true" scan="true" scanPeriod="10 minutes"> 
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>%date %level [%thread] %logger %msg%n</Pattern>
        </encoder>
    </appender>
    <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_DIR}${LOG_NAME}.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <FileNamePattern>
           ${LOG_DIR}${LOG_NAME}-%d.%i.log.gz
          </FileNamePattern>
          <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <MaxFileSize>10MB</MaxFileSize>
          </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
          <Pattern>%date %level [%thread] %logger %msg%n</Pattern>
        </encoder>
    </appender>
    <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <!-- deny all events with a level below ERROR -->
            <level>ERROR</level>
        </filter>

        <smtpHost>xxxxxx.bluehost.com</smtpHost>
        <smtpPort>465</smtpPort>
        <username>xxxxxxx+xxxxx.com</username>
        <password>${ERROR_MAIL_PASSWORD}</password>
        <SSL>true</SSL>

        <to>${SUPPORT_EMAIL_ID}</to>
        <!-- Multiple to elements are permitted -->

        <from>xxxxx@xxxxx.com</from>
        <subject>[${ENV}] ERROR in ${LOG_NAME}</subject>
        <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
              <bufferSize>1</bufferSize>
        </cyclicBufferTracker>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%date %-5level %logger{35} - %message%n</pattern>
        </layout>
    </appender>
    <logger name="javax.net" level="info"/>
    <logger name="javax.management" level="info"/>
    <logger name="org.springframework" level="info"/>
    <logger name="org.compass" level="info"/>
    <logger name="org.tuckey" level="info"/>
    <root level="info">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="ROLLING" />
        <appender-ref ref="EMAIL" />
    </root>

</configuration>