Spring LogBack Configuration Not Logging - java

I am working on Spring LogBack configuration because i would have 2 log files (info.log and error.log with respectively only INFO logs and only ERROR logs).
Actually, when I run the application, that 2 files are created but error.log is empty and ERROR logs are not logged.
What is wrong if my configuration file is the following one?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<!-- APPENDERS DEFINITION -->
<appender name="LOG_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/myProject/info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/myProject/info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>500MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="LOG_ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/myProject/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/myProject/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>500MB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<!--START ASYNC APPENDERS DEFINITION -->
<appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="LOG_INFO" />
</appender>
<appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="LOG_ERROR" />
</appender>
<!--END ASYNC APPENDERS DEFINITION -->
<!--START LOCAL CONFIGURATION -->
<springProfile name="local">
<logger name="my.package" level="INFO" additivity="false" >
<appender-ref ref="ASYNC_LOG_INFO" />
<appender-ref ref="ASYNC_LOG_ERROR" />
</logger>
</springProfile>
<!-- END LOCAL CONFIGURATION -->
</configuration>

Related

How to log org.springframework.web.context.ContextLoader with logback

I have little problem with logging Spring bootstrap. I cant log org.springframework.web.context.ContextLoader with logback. Can someone give a point how to log Spring boot?
Here is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- load props file -->
<define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
<resource>META-INF/crdb.prod.properties</resource>
</define>
<if condition='${propExists}'>
<then><property resource="META-INF/crdb.prod.properties" /></then>
</if>
<define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
<resource>META-INF/crdb.test.properties</resource>
</define>
<if condition='${propExists}'>
<then><property resource="META-INF/crdb.test.properties" /></then>
</if>
<define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
<resource>META-INF/crdb.dev.properties</resource>
</define>
<if condition='${propExists}'>
<then><property resource="META-INF/crdb.dev.properties" /></then>
</if>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%X{distribute_trace_id}] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.home}/logs/crdb.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] [%X{distribute_trace_id}] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>${catalina.home}/logs/crdb.%i.log.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
</appender>
<property name="APP_VERSION" value="1.2.1"/>
<!-- max size of the log file, when the log file will be bigger, then will be split -->
<property name="LOG_FILE_MAX_SIZE" value="200MB"/>
<!-- max size of logs in ALL log files -->
<property name="LOG_TOTAL_SIZE_CAP" value="1GB"/>
<!-- count of days - files will be persisted for these days -->
<property name="LOG_FILE_MAX_HISTORY" value="3"/>
<!-- Splunk Monitoring Messages -->
<appender name="SPLUNK_MONITORING" class="ch.qos.logback.core.rolling.RollingFileAppender" additivity="false">
<file>${catalina.home}/logs/splunk_monitoring/crdb.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover inferred from the file name -->
<fileNamePattern>${catalina.home}/logs/splunk_monitoring/crdb.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE}</maxFileSize>
<maxHistory>${LOG_FILE_MAX_HISTORY}</maxHistory>
<totalSizeCap>${LOG_TOTAL_SIZE_CAP}</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="SPLUNK_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.home}/logs/splunk/crdb.log</file>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<version>${APP_VERSION}</version>
<timestampPattern>yyyy-MM-dd'T'HH:mm:ss.SSS</timestampPattern>
<customFields>{"app_name":"CRDB", "env": "${env.key}", "hostname": "${hostname}"}</customFields>
<fieldNames>
<timestamp>time</timestamp>
<version>app_version</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<exclude>net\.sf\.cglib\..*</exclude>
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover inferred from the file name -->
<fileNamePattern>${catalina.home}/logs/splunk/crdb.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE}</maxFileSize>
<maxHistory>${LOG_FILE_MAX_HISTORY}</maxHistory>
<totalSizeCap>${LOG_TOTAL_SIZE_CAP}</totalSizeCap>
</rollingPolicy>
</appender>
<logger name="splunk_monitoring" level="INFO" additivity="false">
<appender-ref ref="SPLUNK_MONITORING"/>
</logger>
<logger name="org.springframework.web.context.*" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
<appender-ref ref="SPLUNK_FILE"/>
</root>
</configuration>
When I try this configuration, It only write to console, that
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly. Thank you.
Tried this ?
<logger name="org.springframework.web">
<level value="info" />
<appender-ref ref="console" />
</logger>

Logback rolling logging not working

I am trying to create logger policy in a spring based project.
The issue I am facing is related to rolling policy. the logfile.log is created and is working fine but the rolling file rollingfile.log.%d{yyyy-MM-dd}.log is not created.
Given below is my logback.xml.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<property name="LOG_PATH" value="D:/coinLogs" />
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
<appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n</pattern>
</layout>
</appender>
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log.%d{yyyy-MM-dd}.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="coinPay.logbackxml" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
</logger>
<!-- To remove extra hibernate logs -->
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
</root>
</configuration>
any help will be appreciated. Thank you in advance :D
You need to specify ch.qos.logback.core.rolling.RollingFileAppender as class for your appender. A FileAppender can not roll.
I believe you're missing the %i parameter, which should be replaced by the rolling file index (i.e. 'mylog.2017-03-13.0.txt', 'mylog.2017-03-13.1.txt', etc.)
In addition, like Uwe Allner, I'm using RollingFileAppender instead of FileAppender.
(I also suggest adding a totalSizeCap to the configuration, in order to control to total size)
Here's my entire appender configuration:
<appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/mylog.txt</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level- %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<maxHistory>3</maxHistory>
<totalSizeCap>5GB</totalSizeCap>
<fileNamePattern>${LOG_HOME}/archived/mylog.%d{yyyy-MM-dd}.%i.txt.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>

Logback configuration into Java project

I'm using logback to save all my INFO into log file and every day I would like to save this file in a specific folder archived
This is how I have configured it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${CATALINA_HOME}/logs/DartFleetViewer.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">
<!-- rollover daily -->
<fileNamePattern>${CATALINA_HOME}/logs/archived/DartFleetViewer.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!-- <appender name="FILE-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/error.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">
rollover daily
<fileNamePattern>${DEV_HOME}/archived/error.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender> -->
<!-- Send logs to both console and file audit -->
<root level="info">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</root>
<!-- <root level="error">
<appender-ref ref="FILE-ERROR" />
</root> -->
</configuration>
The odd thing is that I find this in my folder
almost all of the files are empty or with few code lines, for example I don't find nothing about yesterday, In the last file I only find
2016-08-02 02:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-02 05:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-02 08:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
This is information about today and not yesterday.
Furthermore yesterday I had some errors, warnings and a lot of info log.
Do you see some error in my configuration file?
UPDATE:
Since I receive warning during deployment I have changed my logback configuration in order to avoid all warning and I use only time rollback. It seems to work:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${CATALINA_HOME}/logs/DartFleetViewer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${CATALINA_HOME}/logs/archived/DartFleetViewer.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Send logs to both console and file audit -->
<root level="info">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</root>
<!-- <root level="error">
<appender-ref ref="FILE-ERROR" />
</root> -->
</configuration>

Logback : different different files for different log levels

I know that this type of question have been answered, but in my case i have tried every config and still doesn't work. I need a fresh view to my config (I am sure i'm missing something). both of the appenders log all levels
I want to log info >= for all packages to the console, and the errors only the erros to log file. In my case, both of them log info
Here is config.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- CONSOLE -->
<property name="LOG_PATTERN" value="%d [%thread] %-5level %logger{36} - %msg%n" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- FILE FOR ERROR ONLY -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/Users/dev/Desktop/JAC/logs/frontend/errors.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/dev/Desktop/JAC/logs/frontend/errors_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Thanks in advance
May be, it's not the more beautiful solution. But, in this case, log level INFO is logged in the console, and error is logged in a file. The debug level will be not printed. And INFO and ERROR are in only one exit.
And my application package is fr.myuser.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="/home/myUser" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${DEV_HOME}/error.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">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/error.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- Send logs to console -->
<logger name="fr.myuser" level="INFO">
<appender-ref ref="STDOUT" />
</logger>
<root level="ERROR">
<appender-ref ref="FILE-ERROR" />
</root>
</configuration>

Logback creating logs at maven run but not when it deployed

Using log-back for logging.When building project with Maven its creating log files and writing logs but not same, when running Spring Runner test case or deployed on server.Its just creating empty log file.
Following is my Configuration,
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="1000000 seconds">
<appender name="consoleOutput"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{hh:mm:ss} %-5level %c:%M:%L %m%n- %msg%n</pattern>
</layout>
</appender>
<!-- Appender: for logging all the Logs into the Log file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ALL</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<customFilSystem>true</customFilSystem>
<!-- rollover daily -->
<fileNamePattern>log/LOGFILE-%d{yyyy-MM-dd}.%i.txt.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
<minIndex>1</minIndex>
<maxIndex>2</maxIndex>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{hh:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="ALL" additivity="false">
<appender-ref ref="FILE"/>
</root>
<root level="info">
<appender-ref ref="consoleOutput"/>
</root>
<!-- Specify the package or module and reference of respective Appender -->
<logger name="org.springframework" level="OFF" />
</configuration>

Categories