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?