Reputation: 8129
I am building a command line application using Spring Boot. In such an application, logback console logging is not appropriate. How can I completely disable the console appender, but still have the file appender working with the default Spring Boot support?
I have created a feature request for simpler support of this feature here:
https://github.com/spring-projects/spring-boot/issues/1612
Upvotes: 11
Views: 21439
Reputation: 127
Here is logback-spring.xml
in which you can enable, and disable the console log, and file log based on profile.
I have commented out the console appender for the prod profile and for default (when no profile is active). It will print the logs only in the file. It will print to both console and file for local, dev, etc. profiles.
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
<!-- include **/appLog*log* in global gitignore file
to avoid below log files showing up in git -->
<property name="log.path" value="log"/>
<property name="log.filename.prefix" value="appLog"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!-- logs folder created in user.dir folder,
value is printed by calling System.getProperty("user.dir")
if you want see this property for a running jvm process, then do the following
- execute command "jps -lm" and identify the pid of the server process
- execute command "jcmd <SERVER PROCESS PID> VM.system_properties" to see the properties
-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} || [%thread] || %-5level || %logger{36} || %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.filename.prefix}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log.path}/archive/year_%d{yyyy, aux}/month_%d{MM, aux}/${log.filename.prefix}.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<!-- each archived file, size max 10MB -->
<maxFileSize>10MB</maxFileSize>
<!-- total size of all archive files, if total size > 1GB, it will delete old archived file -->
<totalSizeCap>1GB</totalSizeCap>
<!-- 3653 days (10 years) to keep -->
<maxHistory>3653</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} || [%thread] || %-5level || %logger{36} || %msg%n</pattern>
</encoder>
</appender>
<springProperty scope="context" name="application_name" source="spring.application.name"/>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<!-- remove log related properties in application.yaml and application-test.yaml files
of application for below log level setting to be honored -->
<springProfile name="!(local | dev | test | test_common)">
<root level="INFO">
<!-- <appender-ref ref="jsonConsoleAppender" /> -->
<!-- <appender-ref ref="CONSOLE" /> -->
<appender-ref ref="FILE-ROLLING" />
</root>
</springProfile>
<!-- prod profile specific configuration -->
<springProfile name="prod | production">
<root level="WARN">
<!-- appenders included in the first spring profile tag, if included here,
then it will lead to duplicate logs with json format and console format-->
<!--
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE-ROLLING" />
-->
</root>
</springProfile>
<springProfile name="local | dev | test | test_common">
<root level="DEBUG">
<!-- <root level="TRACE"> -->
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE-ROLLING" />
</root>
</springProfile>
</configuration>
Upvotes: 0
Reputation: 64011
Just add a file called logback.xml
in src/main/resources
with content like (copied verbatim except for console part from Spring Boot's source):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex"/>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Note that
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
is needed in order to support setting the log file from Spring Boot's logging.file
and logging.path
.
If all you want to do is set some standard log file, you could set place it's path in property above.
Update (02-04-2015)
In newer versions of Spring Boot you can easily just include the base.xml
from Spring Boot and create the following logback.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Update (15-09-2017)
In order to get this working on Spring Boot 1.5.x and 2.0.0.M4 I added a file called logback-spring.xml
and added it in the resources
directory.
The file could look like this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n"/>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Upvotes: 11
Reputation: 7722
Add the below to your application.properties
logging.level.root=OFF
spring.main.banner-mode=OFF
Upvotes: 0
Reputation: 430
Include file-appender.xml
and not console-appender
with the following configuration in logback-spring.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
You also need to add logging.file
to your application.properties
This is in compliance with what is mentioned on spring boot documentation - http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
Upvotes: 0
Reputation: 4517
Tested with latest 1.3.1 spring boot release..
place logback.xml under resource folder
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<!-- Send debug messages to a file "application.log" -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>application.%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
Also make sure to remove exclusions of spring-boot-starter-logging from "spring-boot-starter" and "spring-boot-starter-web" dependency, if you added exclusions before.
No need of below dependencies
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
application.properties
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
Upvotes: 3
Reputation: 26858
Using Spring Boot 1.3.0, I created a file logback-spring.xml
(in src/main/resources
with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<springProfile name="dev">
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</springProfile>
<springProfile name="staging,prod">
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</springProfile>
</configuration>
Additionally, I added the logging.file
property in application-staging.properties
and application-prod.properties
to specify what the file name should be.
This will log to console for dev
and to file for staging
or prod
profiles.
Upvotes: 12
Reputation: 660
I tried removing console configuration from logback.xml. But, It was still logging in console. So What I did is, I just removed the appender being added in the logging configuration by springboot. Thereby, we can stop springboot logging in console and separate log file. Add the below lines at the end of your application specific appenders are added. Your custom appender should not match any of these appender names. It worked for me.
// get instance of your log4j instance
Logger logger = LogManager.getRootLogger();
logger.removeAppender("CONSOLE"); // stops console logging
logger.removeAppender("LOGFILE"); // stops file logging
Upvotes: 2