I have a Java Web application running on WLS 12 and using a logback RollingFileAppender to log data. Although it's inconsistent, I will occasionally see the current logging output being directed to one of the archive files created by the rollover process. For example, with the configuration below, I see current logging output being appended to debug.2019-01-17.0.txt instead of to main.log. Am I misunderstanding the expected behavior of the RollingFileAppender? If not, does anyone have an idea about what may be causing the behavior?
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG-PATH}/main.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%5p] [%t] \(%F:%M:%L\) %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG-PATH}/debug.%d.%i.txt</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>45</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
</appender>
I'm using the logback in my application and following rolling file appender.
<appender name="OVERALL_SYNCHRONOUS" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Append>true</Append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Daily rollover -->
<fileNamePattern>${logRoot}/${dirName}/application_overall.log.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yy HH:mm:ss.SSS} [%-5level] %65.65(%logger{0}.%aspectjFreeMethod\(%line\)): %X{MDC_CtrlName}%msg%n</pattern>
</encoder>
</appender>
This works fine most of the times but sometimes on the day change it zips the previous that is as expected but does not change the directory for the new log file. It creates the new file in the same folder of the previous day.
When day changes from 18.11.15 to 19.11.15 application zipped the application_overall.log as it should and create "application_overall.log.zip".
But application continues to log in the application_overall.log in the folder "2015-11-18".
No application_overall.log was created in the folder "2015-11-19" whereas all the other log files of the application were zipped in folder "2015-11-18" and a new log file is created in the current date folder i.e. "2015-11-19".
After restarting, a new log file of application_overall.log was created in the folder "2015-11-19".
EDIT:
logroot and dirName are defined as below :-
<insertFromJNDI env-entry-name="java:comp/env/log/logRoot" as="logRoot"
scope="context"/>
<insertFromJNDI env-entry-name="java:comp/env/log/dateDirNamePattern" as="datePattern" scope="context"/>
<property name="dirName" value="%d{${datePattern:-yyyy-MM-dd}}" scope="context"/>
What could be the possible reason for this behaviour and how can I solve this issue?
we are using below log file for logging data but we are not able to create file named as "mylogfile.log" which contains current date Logs .
however its creating Log file named as Logs "mylogfile2015.02.05.log for current day also as its not refering the tag defined.
Need help to create log file(mylogfile.log) without Date with Prudent mode is true to support multiple JVM's.
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${MY_LOG_LOCATION_PROP}/logs/mylogfile.log</file>
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>mylogfile-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
I guess you can't set file property of RollingFileAppender while using prudent mode.
Check this.
I'm using Logback and I want to log some strings to a log file and let it gzip the file when I'm done.
I was following the example of:
Programmatically configure LogBack appender
But haven't figured out how to, when I finish logging, tell it to gzip the file.
All the examples show to use fileNamePattern. The examples I've seen show to to define this in logback.xml, but I'm trying to do this by code.
Would appreciate some pointers / examples for this :)
<!-- Time and Size based: Roll every day and split big file in smaller peaces -->
<appender name="ROOT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/root.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/root-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %-5level [%thread] - %mdc{loginName} - [%logger]- %msg%n</pattern>
</encoder>
</appender>
Notice the ".gz": this indicates that the logfile will be compressed. Replace this with .zip to use a zip-file.
There are some limitations, but is basically the easiest flow. The docs state.
Just like FixedWindowRollingPolicy, TimeBasedRollingPolicy supports automatic file compression. This feature is enabled if the value of the fileNamePattern option ends with .gz or .zip.
Do any of the popular Java logging frameworks support a rolling file appender, that I can configure to rollover daily, and also delete any log file that is over some number of days old? I know I could use a rolling file appender and a cron, but was wondering if anyone knew of an appender that can do both.
Logback's classic RollingFileAppender provides this and more. An example configuration from the manual (http://logback.qos.ch/manual/appenders.html#onRollingPolicies)
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
This provides daily rollover and 30 days of history. Place this in a file called logback.xml, or logback-test.xml for test trees, and place it in the classpath.
After working a while on Log4j, I read that Logback was developed and designed to be the successor of Log4j, and I have to say, Logback is wide stepper!
For example, to configure, this rolling file Appender, and zip the older logs, and have a maximum of 30, on Log4J you have to perform some code changes (please review my previews answer for more details), but on logback, everything is done on the config file like this:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>log/logFile.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>[%-5level]: [%logger{35}] - %msg%n</pattern>
</encoder>
</appender>
<root level="trace">
<appender-ref ref="FILE" />
</root>
</configuration>
And that's it, you will have the current log unzipped, and the older days, zip, one zip file by each day.
Wide more easier than log4j!
If you want to play with more config options, just check this link.
Indeed, if you are using Log4J you can use this appender:
http://blog.kimb3r.com/2008/07/improving-log4j-dailyrollingfileappende.html
It's very, very, VERY easy to use, just download the whole class(yes, the code of the class in the above link), include it in your project (wherever you want, you can change the package to match inside your project), and then configure the log4j.properties file like this(this file has to be in your classpath, for example in the src/main/resources folder):
# Define the root logger with appender file
log = log
log4j.rootLogger = TRACE, FILE
# Define the logical path where you put the class you downloaded from "blog.kimb3r.com" link (above)
log4j.appender.FILE=com.yourapp.yourpackage.log.CustodianDailyRollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
log4j.appender.FILE.File=${log}/yourlogfile.log
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
# How many files you want to keep?, in this case I'm having just 15 days of files, one file per day:
log4j.appender.FILE.MaxNumberOfDays=15
# If True, the older files will be compressed into a zip file (*which will save you a lot of space on the server*)
log4j.appender.FILE.CompressBackups=true
and besides this just add the dependency to log4j into your pom like this:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
And that's it!
Why not look at this might help?
You can probably set maximum nuber of days to retain the logs.
log4j.rootLogger=INFO, FILE
log4j.appender.FILE=ca.justtechnologies.utils.logging.CustodianDailyRollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{MMM dd yyyy
HH:mm:ss,SSS} [%t] %-5p %l - %m%n
log4j.appender.FILE.File=/var/log/web-apps/Dashboard.log
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.MaxNumberOfDays=14
log4j.appender.FILE.CompressBackups=true