Learn to use and configure logging to console in spring boot application. Also learn to configure and use console appenders available in logback and log4j2.
1. Default console logging
Spring boot internally uses apache’s common logging and uses logback as default logging provider. If we do not make any logging specific configuration, still we see lots of logging output to console which is good enough for POC (proof of concept) purposes.
We can find all default console logging configuration in these packaged files.
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}) {faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}) {cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root>
2. Customize default console logs with properties/yaml
Generally, in the console logs, we would want to two things i.e. log levels and output pattern.
2.1. Log level
To change the log level, we can change it at two levels – root logger and/or specific logger instance.
To change the level at root logger, 'logging.level.root=trace'
(with desired level) in properties file. Additionally, to control logging at individual logger level, use logging.level.LOGGER_NAME=trace
.
logging.level.root=INFO logging.level.org.springframework=ERROR logging.level.org.hibernate=ERROR
2.2. Logging output pattern
The pattern is controlled vis property logging.pattern.console
.
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n
3. Customize console logging with logback
If we have configured logging via logback – using own logback.xml
file then use below configuration to control various options of console logging.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n" /> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <logger name="com.howtodoinjava.app" level="trace" additivity="false"> <appender-ref ref="console" /> </logger> <root level="info"> <appender-ref ref="console"/> </root> </configuration>
4. Customize console logging with log4j2
To configure console logging with log4j2, place given definition in log4j2.xml
file.
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property> </Properties> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="${LOG_PATTERN}" /> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="console" /> </Root> </Loggers> </Configuration>
5. Disable console logging
To disable logging in console, cleanest way is to create custom logging configuration file (logback.xml
or log4j2.xml
) and do not provide any definition of console appender.
If not using configuration files, we can use the property
logging.pattern.console
with empty value to disable console logging. It is not the recommended approach and do it only for POCs.logging.pattern.console=
Only include file appenders in configuration file which will prevent any logging output to console.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="LOG_LOCATION" value="c:/temp" /> <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n" /> <appender name="file" class="ch.qos.logback.core.FileAppender"> <file>${LOG_LOCATION}/mylog.log</file> <encoder> <pattern>${LOG_PATTERN}</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_LOCATION}/archived/mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <logger name="com.howtodoinjava.app" level="trace" additivity="false"> <appender-ref ref="file" /> </logger> <root level="info"> <appender-ref ref="file"/> </root> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property> </Properties> <RollingFile name="file" fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log" filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="${LOG_PATTERN}" /> <Policies> <SizeBasedTriggeringPolicy size="19500KB" /> </Policies> <DefaultRolloverStrategy max="1" /> </RollingFile> <Loggers> <Root level="info"> <AppenderRef ref="file" /> </Root> </Loggers> </Configuration>
Drop me your questions related to spring boot console appender example.
Happy Learning !!
Leave a Reply