8

我试图远离 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;
    }

}

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

4

1 回答 1

3

我不知道我做了什么,但它现在可以正常工作,并且日志记录显示在控制台中。

于 2013-06-29T19:16:49.450 回答