Announcement Announcement Module
Collapse
No announcement yet.
Moving log4j from xml to a Java configuration... what about the logger ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Moving log4j from xml to a Java configuration... what about the logger ?

    Hello lovely Spring using people,

    I'm moving from a log4j.xml based configuration to a Java based one.

    I could easily configure the appenders:
    Code:
    @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;
        }
    
    }
    But I wonder now how to go about for the remaining:
    Code:
    	<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 have googled to no luck on this.

    Thanks for even the slightest tip !

  • #2
    I tried replacing the above logger elements by the following JavaConfig code:

    Code:
        @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 it still does not log anything out in the console.

    Comment


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

      Comment

      Working...
      X