Spring JavaConfig和log4j记录器

时间:2013-06-24 11:58:36

标签: java log4j config

我正试图摆脱log4j.xml文件,只在Spring项目中使用JavaConfig方式。

原始记录器元素如下所示:

<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="ALL" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
    </layout>
</appender>

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="Threshold" value="ALL" />
    <param name="File" value="build.log" />
    <param name="maxFileSize" value="100KB" />
    <param name="maxBackupIndex" value="1" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
    </layout>
</appender>

<appender name="mailAppender" class="org.apache.log4j.net.SMTPAppender">
    <param name="Threshold" value="ERROR" />
    <param name='SMTPDebug' value='true' />
    <param name="SMTPProtocol" value="smtps" />
    <param name="SMTPHost" value="smtp.gmail.com" />
    <param name='SMTPPort' value='465' />
    <param name="SMTPUsername" value="learnintouch@gmail.com" />
    <param name="SMTPPassword" value="xxxxxx" />
    <param name="From" value="stephane@learnintouch.com" />
    <param name="To" value="stephane@learnintouch.com" />
    <param name="Subject" value="[LOG] Java - learnintouch" />
    <param name="BufferSize" value="1" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="FATAL" />
    </filter>
</appender>

<logger name="org.springframework">
    <appender-ref ref="consoleAppender" />
</logger>

<logger name="com.thalasoft">
    <level value="DEBUG" />
    <appender-ref ref="consoleAppender" />
    <appender-ref ref='fileAppender' />
</logger>

<logger name="org.hibernate.type">
    <level value="TRACE" />
    <appender-ref ref="consoleAppender" />
</logger>

<logger name="org.hibernate.sql">
    <level value="DEBUG" />
    <appender-ref ref="consoleAppender" />
</logger>

<logger name='jdbc.sqlonly' additivity='false'>
    <level value='TRACE' />
    <appender-ref ref='consoleAppender' />
    <appender-ref ref='fileAppender' />
</logger>

我试图用以下内容替换它们:

@Configuration
@ImportResource({ "classpath:log4j.xml" })
public class Log4j {

    @Bean
    public ConsoleAppender consoleAppender() {
        ConsoleAppender consoleAppender = new ConsoleAppender();
        consoleAppender.setThreshold(Level.ALL);
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.setConversionPattern("%d %-5p  [%c{1}] %m %n");
        consoleAppender.setLayout(patternLayout);
        return consoleAppender;
    }

    @Bean
    public FileAppender fileAppender() {
        RollingFileAppender fileAppender = new RollingFileAppender();
        fileAppender.setThreshold(Level.ALL);
        fileAppender.setFile("build.log");
        fileAppender.setMaxFileSize("100KB");
        fileAppender.setMaxBackupIndex(1);
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.setConversionPattern("%d %-5p  [%c{1}] %m %n");
        fileAppender.setLayout(patternLayout);
        return fileAppender;
    }

    @Bean
    public SMTPAppender mailAppender() {
        SMTPAppender mailAppender = new SMTPAppender();
        mailAppender.setThreshold(Level.ERROR);
        mailAppender.setSMTPDebug(true);
        mailAppender.setSMTPProtocol("smtps");
        mailAppender.setSMTPHost("smtp.gmail.com");
        mailAppender.setSMTPPort(465);
        mailAppender.setSMTPUsername("learnintouch@gmail.com");
        mailAppender.setSMTPPassword("xxxxxx");
        mailAppender.setFrom("stephane@learnintouch.com");
        mailAppender.setTo("stephane@learnintouch.com");
        mailAppender.setSubject("[LOG] Java - learnintouch");
        mailAppender.setBufferSize(1);
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.setConversionPattern("%d{ABSOLUTE} %5p %c{1}:%L - %m%n");
        mailAppender.setLayout(patternLayout);
        LevelRangeFilter levelRangeFilter = new LevelRangeFilter();
        levelRangeFilter.setLevelMin(Level.DEBUG);
        levelRangeFilter.setLevelMax(Level.FATAL);
        mailAppender.addFilter(levelRangeFilter);
        return mailAppender;
    }

    @Bean
    public Logger registerSpringLogger() {
        Logger logger = Logger.getLogger("org.springframework");
        logger.addAppender(consoleAppender());
        return logger;
    }

    @Bean
    public Logger registerThalasoftLogger() {
        Logger logger = Logger.getLogger("com.thalasoft");
        logger.setLevel(Level.DEBUG);
        logger.addAppender(consoleAppender());
        logger.addAppender(fileAppender());
        return logger;
    }

    @Bean
    public Logger registerHibernateTypeLogger() {
        Logger logger = Logger.getLogger("org.hibernate.type");
        logger.setLevel(Level.TRACE);
        logger.addAppender(consoleAppender());
        return logger;
    }

    @Bean
    public Logger registerHibernateSqlLogger() {
        Logger logger = Logger.getLogger("org.hibernate.sql");
        logger.setLevel(Level.DEBUG);
        logger.addAppender(consoleAppender());
        return logger;
    }

    @Bean
    public Logger registerJdbcSqlOnlyLogger() {
        Logger logger = Logger.getLogger("jdbc.sqlonly");
        logger.setLevel(Level.TRACE);
        logger.setAdditivity(false);
        logger.addAppender(consoleAppender());
        logger.addAppender(fileAppender());
        return logger;
    }

}

但我仍然没有在控制台中获得日志记录输出。

1 个答案:

答案 0 :(得分:3)

我不知道我做了什么,但它现在运行正常,日志记录显示在控制台中。