Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JavaConfig and the log4j logger

Tags:

java

config

log4j

I'm trying to move away from the log4j.xml file and only use the JavaConfig way in a Spring project.

The original logger elements look like:

<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>

I tried to replace them with:

@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;
    }

}

But I still get no logging output in the console.

like image 338
Stephane Avatar asked Jun 24 '13 11:06

Stephane


People also ask

Does the Springframework use Log4j?

Spring Boot also supports either Log4j or Log4j 2 for logging configuration, but only if one of them is on the classpath. If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead.

What is Log4j in Spring?

You know, Log4j is a popular and widely-used logging framework for Java development. It's pretty easy to setup and use log4j in a Spring MVC application, just by following a few steps described below.

What is spring boot starter log4j2?

Spring boot starter projects enable quick development boot applications. Starter projects has a default dependency on spring-boot-starter-logging. This library configures logback as the default logging implementation. However some may prefer to use log4J2 instead of Logback to write application logs.


1 Answers

I don't know what I did but it now works fine and the logging is displayed in the console.

like image 94
Stephane Avatar answered Oct 31 '22 06:10

Stephane