2013-06-24 19 views
8

私はlog4j.xmlファイルから離れ、SpringプロジェクトでのみJavaConfigを使用しようとしています。私はそれらを交換しようとしたSpring JavaConfigとlog4j logger

<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="[email protected]" /> 
    <param name="SMTPPassword" value="xxxxxx" /> 
    <param name="From" value="[email protected]" /> 
    <param name="To" value="[email protected]" /> 
    <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("[email protected]"); 
     mailAppender.setSMTPPassword("xxxxxx"); 
     mailAppender.setFrom("[email protected]"); 
     mailAppender.setTo("[email protected]"); 
     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; 
    } 

} 

しかし、私はまだコンソールには、ログ出力を取得しないように

元ロガー要素が見えます。

答えて

3

私は何をしたのかわかりませんが、今は正常に動作し、ログがコンソールに表示されます。

関連する問題