More than 1 log file was rolling at the same time using logback - logback

I am using logback for rolling and below is my rolling configuration
<appender name="rtc_file_appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DEST}</file>
<Append>true</Append>
<encoder>
<Pattern>%date | [%.-1level] | %logger | %X{threadId} | %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>5</maxIndex>
<FileNamePattern>rtc.log.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>5MB</MaxFileSize>
</triggeringPolicy>
</appender>
when application is in use, first rtc.log file is created and then rtc.log.1 and then tc.log.2 etc..
But, this this configuration after rolling one log file (eg: rtc.log.1), it started rolling the logs in rtc.log.2 and at the same time rtc.log.1 file was opened again and start rolling.
Means, two log files are in open mode and rolling the log.
Please advice , how this can be fixed.

Related

logback DailyRolling seems does not work for me

I am using a logback dailyroller, the application is up and running always, but it seems it does not create log file every day as I expected. below is my setting,
My question is if my application is running but if there is no output to the log file, will the daily log file still be created every day?
<property name="LOGS" value="./log" />
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/out.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/%d{yyyy-MM-dd}.gz
</fileNamePattern>
<maxHistory>30</maxHistory>
<!--timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>40MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicyi-->
</rollingPolicy>
</appender>
If you're not able to find log file, change your property tag from <property name="LOGS" value="./log" /> to <property name="LOGS" value="/log" /> and also add the root tag.
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="/log" />
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/out.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/%d{yyyy-MM-dd}.gz
</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="debug">
<appender-ref ref="RollingFile" />
</root>
</configuration>
Also, If there is no logging event then they'll be no rollover. So if your log back configuration policy is set to daily rollover and your application does not produce a single logging event for the entire day then there'll be no log file created for that day.

Java web app catalina logs not written to log file when running in Eclipse

I have a Java web app that I run in Eclipse for my development environment. I use logback for logging to a custom file. The problem is certain logging statements, specifically those that traditionally go to catalina.out, do not end up in my log file. They do show up in my Eclipse console, but not in my custom logback log file.
When I run the same app in tomcat outside of Eclipse (via startup.bat), those catalina logging statements do get captured in a catalina.out file. But when running in Eclipse no catalina.out is created, so those logs don't persist.
Here's my logback-test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- RollingFileAppender that rolls based on size and time -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/mylog.log</file>
<encoder>
<pattern>%date [%thread] %-5level %logger{10} [%file:%line] - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${catalina.base}/logs/mylog.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each file at most 20MB; keep 10 files worth of history, max total 20GB -->
<maxFileSize>20MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>logback-test %date [%thread] %highlight(%-5level) %logger{10} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>logback-test %date [%thread] %highlight(%-5level) %logger{10} - %msg%n</pattern>
</encoder>
</appender>
<!-- set logging levels for specific packages -->
<logger name="org.apache.catalina.startup" level="INFO"/>
<!-- set level of the root logger and associate it with both appenders -->
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="STDERR"/>
</root>
</configuration>
This answer resolved my question: https://stackoverflow.com/a/5045247/3368818
Apparently, if, while running an app on tomcat via Eclipse, you want to capture the catalina logs in a file, you need to specify that via the Eclipse tomcat launch configuration settings.
I guess in a way this makes sense in that the catalina logs are perhaps not application specific, so maybe outside the scope of logback appenders. But on the other hand, shouldn't they get written to a log file by default anyway, just as they do if you launch your tomcat server outside of Eclipse?

slf4j, logback, the target compressed file named exist already

logback 1.1.5 + slf4j 1.7.4.
Rollover at midnight
00:00:00,163 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [/opt/wls/appl/log/out.log] to [/opt/wls/appl/log/out.log1294410077875834.tmp]
00:00:00,164 |-INFO in ch.qos.logback.core.rolling.helper.Compressor - ZIP compressing [/opt/wls/appl/log/out.log1294410077875834.tmp] as [/opt/wls/appl/log/archive/out/out-2018-09-04.10.zip]
But after some seconds rollover starts again
00:01:52,551 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [/opt/wls/appl/log/out.log] to [/opt/wls/appl/log/out.log1294522459498363.tmp]
00:01:52,551 |-WARN in ch.qos.logback.core.rolling.helper.Compressor - The target compressed file named [/opt/wls/appl/log/archive/out/out-2018-09-04.0.zip] exist already.
There are 2 applications on 2 virtual servers of WLS. Both of applications use separate home folder with your own logback.xml.
logback.xml is
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${PRG_HOME}/logs/out/out-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%ex</pattern>
</encoder>
</appender>
<appender name="ASYNC_COMMON" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>32000</queueSize>
<appender-ref ref="ROLLING" />
</appender>
What can I do?
You can try to add <prudent>true</prudent> to your RollingFileAppender. This should allow for multiple JVMs writing safely to the same file.
There are some restrictions that apply to prudent mode, though. Log-file compression is not allowed, for example.

Logback empty log file is getting created

In my application is using RolingFileAppender with size based triggering policy main classes initializes log file using logback configuration file. When main class is invoked using a shell script, I see log messages are correctly going to log file that conf define but in addition it also created empty logfile with scriptname
say ABC.sh calls MainClass which is supposed to write to logback123.log now all messages are going to logback123.log fine but there is extra empty ABC.log file is getting created.
Any thoughts on this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILEOUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/opt/agents/upgradeSC91Temp/mbs/logs/watchdog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/opt/agents/upgradeSC91Temp/mbs/logs/watchdog_%i.log</fileNamePattern>
<minIndex>1</minIndex> <maxIndex>10</maxIndex> </rollingPolicy>
<triggeringPolicy class="com.aptare.dc.util.LogbackSizeBasedTriggeringPolicy">
<maxFileSize>20MB</maxFileSize> </triggeringPolicy>
<encoder> <pattern>%d{dd MMM yyyy HH:mm:ss:SSS} %-5p %C{0}.%-5M - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd MMM yyyy HH:mm:ss:SSS} %-5p %C{0}.%-5M - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.aptare.dc.util.LogWriterInitializer">
<level value="INFO"/>
<appender-ref ref="FILEOUT"/>
</logger>
</configuration>

OverlappingFileLockException in logback

I'm hitting the following OverlappingFileLockException in LogBack. Is there any know issues with prudent mode in this version of Logback?
logback-classic 1.0.7
logback-core 1.0.7
slf4j-api 1.7.2
slf4j-ext 1.7.2
11:16:59,252 |-ERROR in
ch.qos.logback.core.rolling.RollingFileAppender[FILE_LOG] - Appender
[FILE_LOG] failed to append.
java.nio.channels.OverlappingFileLockException
at java.nio.channels.OverlappingFileLockException
at at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:972)
at at java.nio.channels.FileChannel.lock(FileChannel.java:1052)
at at ch.qos.logback.core.FileAppender.safeWrite(FileAppender.java:187)
at at ch.qos.logback.core.FileAppender.writeOut(FileAppender.java:204)
at at ch.qos.logback.core.OutputStreamAppender.subAppend(OutputStreamAppender.java:212)
at at ch.qos.logback.core.rolling.RollingFileAppender.subAppend(RollingFileAppender.java:148)
at at ch.qos.logback.core.OutputStreamAppender.append(OutputStreamAppender.java:103)
at at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:88)
at at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48)
at at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:280)
at at ch.qos.logback.classic.Logger.callAppenders(Logger.java:267)
at at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:449)
at at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:403)
at at ch.qos.logback.classic.Logger.log(Logger.java:803)
at at org.slf4j.ext.LoggerWrapper.info(LoggerWrapper.java:490)
<appender name="FILE_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C:/MyLog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%-20.20(%thread)] %-6level %-30.30logger{0} - [%-12.12(S:%mdc{pucid}]) %msg%n</pattern>
</encoder>
</appender>
On http://jira.qos.ch/browse/LOGBACK-864 Mahesh Chimanpure says: "timeBasedFileNamingAndTriggeringPolicy can not be used in prudent mode."