Logback leaving .tmp files with multiple instances in Jboss with springBoot - java

We are deploying the application in multiple instances of JBoss. We log back with version 1.1.11 and spring-boot 1.5.10. When log back is trying to append the log file and rolling back as a .zip file it's leaving .tmp files. Below is the scenario
When the application is running in three instances, it's appending only one instance logs into the zip leaving other two instances as .tmp files, which actually should be appended under the same zip file. This is a blocker in our production, with this behavior we are unable to trace out the issue.
<configuration>
<property resource="bootstrap.yml" />
<springProperty name="appName" source="spring.application.name"/>
<springProperty name="loglevel" source="logging.level.com.some.package"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d {ISO8601}%-5p [%c{3}] [%t] %m%n</pattern>
</encoder>
</appender>
<appender name ="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/appllogs/spp/$
{appName}.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/appllogs/backup/${appName}/%d{yyyy-MM-dd}
%i.log.zip</fileNamePattern>
<maxHistory>7</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d %-5p %m%n</pattern>
</encoder>
</appender>
<logger name="com.some.package" level="$
{loglevel}" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="APPLICATION"/>
</logger>
<root level="${loglevel}
" >
<appender-ref ref="CONSOLE" />
<appender-ref ref="APPLICATION"/>
</root>
</configuration>

Related

Logback overrides dependencies logback.xml

I'm developing an application using another of my project as a maven dependency.
Expectation
I want my dependency using its own logback.xml to log in its own file.
And I want the application using its own logback.xml file to log in the console and a separate file than the dependency. And I want both files in a log folder near the application jar.
What it does now
But for the moment both the application and the dependency use the application's logback.xml and everything is logged in the console and in the same file.
How can I solve this problem?
Details about the projects
Both use logback as a logger. The dependency is a protocol implementation that logs communication information in a file that must be in a separate file than application logs.
Both the application and the dependency have a classic maven structure with the logback.xml file inside the resource folder.
The dependency logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{HH:mm:ss}|%msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>./log/communications.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE"/>
</root>
</configuration>
The application logback.xml
<?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="FILEAPPLI" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{HH:mm:ss}|%msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>./log/debugfileappli.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILEAPPLI"/>
</root>
</configuration>
I found a solution. In my dependency code I call a specific logger name.
private static final Logger logger = LoggerFactory.getLogger("dependencyLogger");
And I declare this logger in my application logback.xml
<logger name="dependencyLogger" level="debug">
<additivity="false">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</logger>
In this way I can handle as I want all the log from the dependency in a single logback.xml

Time and size based logger backup policy

I am trying to create logger policy in a spring based project.
In below logger I wish to created a rolling policy which creates a new file when rollingfile.log exceds more than 10 Mb. So I can have backup of logs without having one single huge logger file. Is there any way to do that?
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>
</appender>
<appender name="RollingFile-Appender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/rollingfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="Async-Appender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="RollingFile-Appender" />
</appender>
<logger name="coinPay.logbackxml" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-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" />
<appender-ref ref="Async-Appender" />
</root>
</configuration>
The total size cap is not doing what you want.
<totalSizeCap>10MB</totalSizeCap>
It caps the total size of all your archived log files. You could have a look at TimeBasedArchiveRemover::capTotalSize to understand what it is doing.
What you need to do is change TimeBasedRollingPolicy to SizeAndTimeBasedRollingPolicy and set <maxFileSize>10MB</maxFileSize>
So your configuration should include:
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxFileSize>10MB<maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>

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>

How to make log file as a zip file?

I have an application, in which I use logback. My logback.xml file is like so :
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>myApp.log</file>
<encoder>
<Pattern>%date{"yyyy-MM-dd'T'HH:mm:ss", UTC} [%thread] %-5level %logger{36} %L - %msg%n</Pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%date{"yyyy-MM-dd'T'HH:mm:ss", UTC} [%thread] %-5level %logger{36} %L - %msg%n</Pattern>
</encoder>
</appender>
<logger name="source.main.FileProcess" level="INFO" />
<logger name="source.main.FileReadWrite" level="INFO" />
<logger name="source.main.OperatorLoader" level="DEBUG" />
<logger name="source.exception.ValidationException" level="INFO" />
<logger name="source.validation.Validation" level="INFO" />
<logger name="source.main.OperatorLoader" level="DEBUG" />
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Now I want to say if my myApp.log file's size become 100 KB then convert it to a zip the myApp.log .
Can anyone help me to do this?
You can use something like below, this is what I use in our project. It creates folder for the month, and keeps daily file in it in gz format
<timestamp key="date" datePattern="yyyy-MM-dd"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>myApp.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>archived_logs_%d{yyyy-MM,aux}/myApp_%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<maxHistory>180</maxHistory>
<encoder>
<pattern>[%d{yyyy.MM.dd HH:mm:ss.SSS}] [%t] [%p] [%logger{5}] [%X] %msg%n</pattern>
</encoder>
</appender>
if you read here, there is a section stating that FixedWindowRollingPolicy, TimeBasedRollingPolicy appenders will compress the log file if the name specified ends with .zip or .gz
Just like FixedWindowRollingPolicy, TimeBasedRollingPolicy supports
automatic file compression. This feature is enabled if the value of
the fileNamePattern option ends with .gz or .zip.
I guess it will something like
<file>test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>

How to use file appender with jar file?

I use logback in my main program .I implement the logback.xml file like so :
<configuration>
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>E:\mylog.txt</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<Pattern>%date{"yyyy-MM-dd'T'HH:mm:ss", UTC} [%thread] %-5level %logger{36} %L - %msg%n</Pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%date{"yyyy-MM-dd'T'HH:mm:ss", UTC} [%thread] %-5level %logger{36} %L - %msg%n</Pattern>
</encoder>
</appender>
<logger name="source.main.FileProcess" level="INFO" />
<logger name="source.main.FileReadWrite" level="INFO" />
<logger name="source.main.OperatorLoader" level="DEBUG" />
<logger name="source.exception.ValidationException" level="INFO" />
<logger name="source.validation.Validation" level="INFO" />
<root level="debug">
<appender-ref ref="ROLLING" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
My application work fine .but when I convert it to a jar file File appender dose not work and logs only shows in console .I use this statement for make a jar file :
jar -cvfm app.jar manifest.txt source/main/MyApp.class
And content of manifest.txt file is like so :
Manifest-Version: 1.0
Main-Class: source.main.FileProcess
Class-Path: slf4j-api-1.7.7.jar logback-core-1.1.3.jar logback-classic-1.1.3.jar OperatorInterface.jar
Do anyone know where is the problem?
Try setting the path to your logback configuration when you execute your jar:
java -Dlogback.configurationFile=PATH_TO_FILE -jar app.jar

Categories