Trying to configure logback and not able to have my configuration used when running the app.
There is a lot of console output, even if I remove all my configuration, and there is no file output, so I suppose the console output is done with some default configuration.
Here is my configuration so far:
@Configuration
public class Log4j {
private final static String PATTERN = "%date %-5level [%thread] %logger{36} %m%n %rEx";
@Bean
public static LoggerContext loggerContext() {
return (LoggerContext) LoggerFactory.getILoggerFactory();
}
@Bean (initMethod = "start", destroyMethod = "stop")
public static PatternLayoutEncoder encoder (LoggerContext ctx) {
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(ctx);
encoder.setPattern(PATTERN);
return encoder;
}
@Bean (initMethod = "start", destroyMethod = "stop")
public static ConsoleAppender consoleAppender (LoggerContext context, PatternLayoutEncoder encoder) {
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setContext(context);
consoleAppender.setEncoder(encoder);
return consoleAppender;
}
@Bean (initMethod = "start", destroyMethod = "stop")
public static FileAppender fileAppender(LoggerContext context, PatternLayoutEncoder encoder) throws IOException {
RollingFileAppender fileAppender = new RollingFileAppender();
fileAppender.setContext(context);
fileAppender.setEncoder(encoder);
fileAppender.setFile("build.log");
return fileAppender;
}
@Bean (initMethod = "start", destroyMethod = "stop")
public static SMTPAppender mailAppender(LoggerContext context) {
SMTPAppender mailAppender = new SMTPAppender();
mailAppender.setContext(context);
mailAppender.setName("GmailAppender");
mailAppender.setSMTPHost("smtp.gmail.com");
mailAppender.setSMTPPort(465); // 587
mailAppender.setSTARTTLS(true);
mailAppender.setUsername("[email protected]");
mailAppender.setPassword("xxxxxx");
mailAppender.setFrom("[email protected]");
mailAppender.addTo("[email protected]");
mailAppender.setSubject("[LOG] Java - learnintouch");
PatternLayout patternLayout = new PatternLayout();
patternLayout.setPattern(PATTERN);
mailAppender.setLayout(patternLayout);
LevelFilter levelRangeFilter = new LevelFilter();
levelRangeFilter.setLevel(Level.ERROR);
mailAppender.addFilter(levelRangeFilter);
return mailAppender;
}
@Bean
public static Logger registerSpringLogger(ConsoleAppender consoleAppender) {
Logger logger = new Logger(...
logger.setLevel(Level.DEBUG);
logger.addAppender(consoleAppender);
return logger;
}
Even if I set all the register beans to OFF level there is still a lot of output in the console.
How can I have my above configuration NOT being ignored ?
UPDATE: In fact, the configuration was not ignored, it was simply drowned in a lot of console output. One way to reduce this console output was to add a logback.xml file in the src/main/resources
directory containing the following:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This empty file is one way to prevent logback from logging like crazy -->
<configuration />
Then, the following configuration allowed me to output only my application loggers and the sql statements:
@Bean
public static Logger registerThalasoftLogger(LoggerContext loggerContext,
ConsoleAppender consoleAppender, FileAppender fileAppender) throws IOException {
Logger logger = loggerContext.getLogger("com.nsn.nitro.project.data");
logger.setLevel(Level.DEBUG); // Display the application loggers
logger.addAppender(consoleAppender);
logger.addAppender(fileAppender);
return logger;
}
@Bean
public static Logger registerSpringLogger(LoggerContext loggerContext,
ConsoleAppender consoleAppender) {
Logger logger = loggerContext.getLogger("org.springframework");
logger.setLevel(Level.OFF);
logger.addAppender(consoleAppender);
return logger;
}
@Bean
public static Logger registerJdbcSqlOnlyLogger(LoggerContext loggerContext,
ConsoleAppender consoleAppender, FileAppender fileAppender) throws IOException {
Logger logger = loggerContext.getLogger("jdbc.sqlonly");
logger.setLevel(Level.DEBUG); // Display the sql statements with their parameters values
logger.addAppender(consoleAppender);
logger.addAppender(fileAppender);
return logger;
}
@Bean
public static Logger registerHibernateTypeLogger(LoggerContext loggerContext,
ConsoleAppender consoleAppender) {
Logger logger = loggerContext.getLogger("org.hibernate.type");
logger.setLevel(Level.OFF);
logger.addAppender(consoleAppender);
return logger;
}
@Bean
public static Logger registerHibernateSqlLogger(LoggerContext loggerContext,
ConsoleAppender consoleAppender) {
Logger logger = loggerContext.getLogger("org.hibernate.SQL");
logger.setLevel(Level.OFF);
logger.addAppender(consoleAppender);
return logger;
}
@Bean
public static Logger registerLog4JdbcLogger(LoggerContext loggerContext,
ConsoleAppender consoleAppender) throws IOException {
Logger logger = loggerContext.getLogger("log4jdbc.debug");
logger.setLevel(Level.OFF);
logger.addAppender(consoleAppender);
return logger;
}
Note that I see this update as a work around and not as a solution since it makes use of a logback.xml file. My original question was about an XML free configuration.
In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.
By default, Spring Boot picks up the native configuration from its default location for the system (such as classpath:logback. xml for Logback), but you can set the location of the config file by using the "logging. config" property.
We needed something like this implemented in order to be able to re-configure the logging of our application, so we decided to open source it (under an Apache 2.0 license) and put it on Github. Please have a look at the logback-configuration project.
If you find it useful, let me know (either e-mail me, you can find my e-mail on the project's pom.xml
, or reply here) and I'll make a release and push it to Maven Central.
The project is pretty small, but very straight-forward. You can have a look at the handful of tests to better understand how to do things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With