0
votes

I'm using Logback in a Spring Boot application. I have my configuration in logback.xml. I'm trying to write separate log files for different processes in the application.

In the configuration I'm creating the extra logger like the following:

<!-- Each can be broken off into its own appender. -->
<logger name="reportsLogger" level="info"
        additivity="false">
    <appender-ref ref="REPORTS_APPENDER" />
</logger>
<root level="ERROR">
    <appender-ref ref="GATEWAY_APPENDER" />
</root>

The reports logger is created using the following appender node:

<appender name="REPORTS_APPENDER"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${DEV_HOME}/reports.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${DEV_HOME}/archived/reports.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

The GATEWAY_APPENDER is defined similarly. The DEV_HOME node is defined in the configuration as:

<property name="DEV_HOME" value="c:/app-logs" />

When the application starts two log files are created in the DEV_HOME directory. In the application code I'm obtaining an instance of the log as the following:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    static Logger logger = LoggerFactory.getLogger("reportsLogger");

    public static void main(String[] args) {
        logger.info("*** REPORTING SERVICE STARTED ***");
        SpringApplication.run(Application.class, args);
    }
}

Any ways when I try to log to the file it always writes to the console, it does not write anything to the file itself. If I try inspecting the logger instance itself (after casting to a Logback Logger instance):

LoggerContext loggerContext = logger.getLoggerContext();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);

It shows me that the context of the logger is the root logger itself, and there's no file attached to it (hence the console writing.) What changes do I need to make to the configuration so that the information is written to the file and not to the console?

1

1 Answers

1
votes

The solution is two parts. First in the main method() the logging needs to occur after the application is starterd:

SpringApplication.run(Application.class, args);
logger.info("*** REPORTING SERVICE STARTED ***");

Secondly, Logger needs to be an instance of ch.qos.logback.classic.Logger instead of org.slf4j.Logger:

static Logger logger = (Logger) LoggerFactory.getLogger("reportsLogger");