Logback daily rolling incorrect - logback

We are using quite a basic daily rolling configuration with logback, and it seems not to function properly. Application creates rolled (previous day) file usually in first minute after midnight. The yyyyMMdd fraction of file name contans previous day, that's correct. But messages in log are not from previous but from new day, only that 30-40 seconds !
For example, bim.2013-08-21.log file contains records with timestamps from 2013.08.22 00:00:00 to 2013.08.22 00:00:42. And all messages from previous day gets lost.
Also, there was some "lucky" exceptions when rolling started in the mid of the day , e.g. 16:00 , so we got more records rolled - from current day.
Logback version is 1.0.13
logback-test.xml :
<configuration scan="true" scanPeriod="10 minutes">
<contextName>bim</contextName>
<property name="LOG_DIR" value="/usr/share/apache-tomcat-7.0.41/logs" />
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %m \(%logger{36}:%L\)%n</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/bim.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_DIR}/bim.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>5</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %m \(%logger{36}:%L\)%n</pattern>
</encoder>
</appender>
<logger name="com.bim" level="DEBUG" />
<logger name="org.springframework.web.servlet.mvc.support" level="WARN" />
<logger name="org.springframework.web.servlet.mvc.annotation" level="WARN" />
<root level="INFO">
<!--appender-ref ref="console" /-->
<appender-ref ref="file" />
</root>
I have not found any traces of similar problems by search, so your help will be greatly appreciated.

I can't see what part are you missing over here. Even I have done the same and its working fine. Please check below link and try it in the same fashion :
https://github.com/abdulwaheed18/Slf4jTutorial/blob/master/sample7.xml

Related

Logback json configuration return no logs

I'm trying to get json logs from Nexus 3. Since it use logback, I tried ton change the logback.xml file but, for some reason, it result to no logs at all... Does someone know what I did wrong?
The only things I changed is to add those lines to the Nexus's default logback.xml file, to add json logs:
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
<filter class="org.sonatype.nexus.pax.logging.NexusLogFilter" />
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
<timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
</layout>
</appender>
and change these for logback to actually use it:
<root level="${root.level:-INFO}">
<appender-ref ref="osgi"/>
<appender-ref ref="console"/>
<appender-ref ref="json"/>
<appender-ref ref="logfile"/>
<appender-ref ref="clusterlogfile"/>
<appender-ref ref="tasklogfile"/>
<appender-ref ref="metrics"/>
</root>

Conditionally format logs in logback

I'm currently formatting my logs in logback like below. However, when I run my app locally I don't want all this metadata gunking up my logs, I just wanna see the message. How can I say "If an environment variable = 'local' then format this way, else format as below"?
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<Pattern>
date=%d{yyyy-MM-dd HH:mm:ss} | rte=${RTEID} | runId=%X{RunId} | interface=%X{Interface} | class=%class{0}.%method | level=%-5level | message=%msg %replace(%xException){'\n','\u2028'}%nopex%n
</Pattern>
</encoder>
</appender>
bit late. But if you are using spring boot you can use logback-spring.xml
and inside you can do something like
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<springProfile name="dev-local">
<pattern>FIST PATTERN</pattern>
</springProfile>
<springProfile name="!dev-local">
<pattern>SECOND PATTERN</pattern>
</springProfile>
</encoder>
</appender>

How to define multiple rolling policies for a single log file in logback?

I am facing same issue as mentioned in Is there a way to define multiple rolling policies for a single log file in logback?
can any one know how to write multiple rolling policies which creates log like
test.log,
test.1.log,
test.2.log.gz,
test.3.log.gz and so on... Here it first create normal log file and 2nd onward it create archive of same.
Later on this all log will be read by filebeat.
Also tried with two appender like
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>test.log</file>
enter code here
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>archives/tests.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>1</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILETEST" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>archives/tests.%i.log.gz</fileNamePattern>
<minIndex>2</minIndex>
<maxIndex>7</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="test-logger" level="DEBUG" additivity="false">
<appender-ref ref="FILE"/>
<appender-ref ref="FILETEST"/>
</logger>
This leads to generate the test.log, test.1.log, test.2.log.gz which I want but here it causes with it writes in both file test.log and in test.1.log at initial level, this again leads to data loss in rotation.

logback's RollingFileAppender is not rolling file with EvaluatorFilter

Logback framework's ch.qos.logback.core.rolling.RollingFileAppender is not rolling files when EvaluatorFilter is used as following:
<appender name="APPLICATION-INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.root}/${CONTEXT_NAME}-INFO.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.root}/${CONTEXT_NAME}-INFO-%d{yyyyMMdd}.log
</fileNamePattern>
</rollingPolicy>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>return (level == INFO || level == WARN);</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.xxx.yyyy.zzz.logging.json.JsonLayout">
<timestampFormat>${timestampFormat}</timestampFormat>
<includeMDC>false</includeMDC>
</layout>
<immediateFlush>true</immediateFlush>
</encoder>
</appender>
But the the following configuration rolls the log file.
<appender name="APPLICATION-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.root}/${CONTEXT_NAME}-ERROR.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.root}/${CONTEXT_NAME}-ERROR-%d{yyyyMMdd}.log
</fileNamePattern>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.xxx.yyy.zzz.logging.json.JsonLayout">
<timestampFormat>${timestampFormat}</timestampFormat>
<includeMDC>false</includeMDC>
</layout>
<immediateFlush>true</immediateFlush>
</encoder>
</appender>
I'm a bit lazy to test that locally but I have a clue what might be wrong :-) In the docs of logback it is written:
For various technical reasons, rollovers are not clock-driven but
depend on the arrival of logging events. For example, on 8th of March
2002, assuming the fileNamePattern is set to yyyy-MM-dd (daily
rollover), the arrival of the first event after midnight will trigger
a rollover. If there are no logging events during, say 23 minutes and
47 seconds after midnight, then rollover will actually occur at
00:23'47 AM on March 9th and not at 0:00 AM.
Based on this a possible guess is that there was no proper logging event accepted by the default JaninoEventEvaluator that you configured.
Another speculation is that you do not refer the appender properly but there are not enough details about this in the question.

Logback fire mail for warnings

I want to configure Logback to fire an email whenever any warnings are found on server.
Right now it is already configure for error level we are getting mails when any error occurred. However same code with changing root level to warn doesn't work.
All sender, receiver, host etc. details are same for which use in error SMTP appender so it's not a server issue. Below is my code.
<appender name="EMAILWARN" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>XXXX</smtpHost>
<to>XXXX</to>
<!-- additional destinations are possible -->
<from>XXXX</from>
<!--
<discriminator class="ch.qos.logback.classic.sift.MDCBasedDiscriminator">
<key>req.remoteHost</key> visit http://logback.qos.ch/manual/mdc.html#mis for different key values
<defaultValue>default</defaultValue>
</discriminator>
-->
<!-- <subject>RCM-Error: ${HOSTNAME} %X{req.remoteHost} %logger - %m</subject> -->
<subject>Error: ${HOSTNAME} - %m %logger</subject>
<!-- <layout class="ch.qos.logback.classic.PatternLayout"> -->
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%date%X{REMOTE_ADDR}%X{USER_NAME}%level%logger%message</pattern>
</layout>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTrackerImpl">
<bufferSize>20</bufferSize> <!-- set number of log entry to send per email -->
</cyclicBufferTracker>
</appender>
<root level="warning">
<appender-ref ref="EMAILWARN" />
</root>
ch.qos.logback.classic.net.SMTPAppender use an evaluator to trigger the email send procedure. The default one triggers at ERROR level.
So what you need is an evaluator that tiggers at WARN level (and above)
<appender ... class="ch.qos.logback.classic.net.SMTPAppender">
...
<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
<expression>return level >= WARN;</expression>
</evaluator>
...
</appender>
I'm pretty sure that warning is not a valid logback level.
Try changing your root logger level to warn:
<root level="warn">
<appender-ref ref="EMAILWARN" />
</root>
You can use filters as below, which will select log levels error and warn
<appender name="EMAILWARN" class="ch.qos.logback.classic.net.SMTPAppender">
<!-- rest of your configurations -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
</appender>