Logback : TimeBasedRollingPolicy does not change the directory sometimes - java

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?

Related

How to set the server-log location in Tomcat

I recently started working in a Spring boot application. The application uses spring-logback for logging implementation.
The application is bundled as a war in different environments. However, the location of writing logs is different in each environment and is mentioned in a logging.properties file.
Therefore, I currently have to change the location before preparing the war every time.
How can I set the path of the logs in the Tomcat configuration so that the path is auto-picked for an environment.
Currently mu logback appender looks like this:
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${logging.file}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.file.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${log.file.history}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${logging.pattern.file}</pattern>
</encoder>
</appender>
All the properties here are picked from logging.properties file inside the src/main/resources
spring-boot provides several logging.* settings that can be applied in application.properties, like:
logging.level.=DEBUG
logging.file=myfile.log
logging.path=d:/logs/
If you set logging.path, Spring Boot writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.

config play framework log file location in production

I'm using play framework in a project. In order to config the log file related information, I use logback to config it. Here is the sample configuration:
<appender name="DEBUGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/usr/frank/logs/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover with compression -->
<fileNamePattern>debug-log-%d{yyyy-MM-dd}.gz</fileNamePattern>
<!-- keep 1 week worth of history -->
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date - [%level] - from %logger in %thread %message%n</pattern>
</encoder>
</appender>
When I run the test like ./activator test or run the application in dev mode ./activator run, I can find the log file get created at the place specified at the file section. But when I run the application in production mode, like ./activator start, the file will not get generated.
Can anyone tell me why?
In Production mode you have to pass the parameter logger.file as shown below.
-Dlogger.file="/usr/frank/logs/debug.log"

Rolling policy in logback is don't like what I want

I have a problem when using logback to write log file daily (when a new day has been started, logback compress the file to zip file).
When I set the time is end of day, and run logback, it still run normally.
But when the day has been changed, and logback was running...the old day log file still exist, and the zip file of old date and the new date log file have been created.
I want when the new day come, the old log file compress to zip file and new day log file is created.
Show me your ideas, please!
thanks for your help.
Here is my logback.xml config file:
<appender name="RollByTimeComp"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>c:/day-%d{yyyyMMdd}.log.zip
</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] %m \(%F:%L\) %C:%M\(\)%n
</pattern>
</encoder>
</appender>

How to configure logback to create log file name having server name

My java EAR application runs on 2 application servers. Each write the logs locally with log file names. I want to know how to add the unique app server name to the log file name that is being written in each server. This would help me identify the files from each server when I am looking into the log files after downloading them from server and while sending to coworkers for debugging. Currently we have to put them into separate folders as both folders have files with the same names. Thank you.
Following is the snippet from logback.xml that names the file
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logfile-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 10MB -->
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
If you use groovy configuration for logback, you can try
import java.net.InetAddress
println InetAddress.getLocalHost()
This will give you the local server name and IP address.

logback create log files inside folder having name as current date

In my current project i want to create log files date wise i.e. log files should reside inside
folder having name as date.
Also archiving should happen at that particular folder.
Current appender that i am using looks like this (it does archiving of log file based on size).
<appender name="AUDITFILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${PROJECT_HOME}\\projectname\\audits\\myproject.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${PROJECT_HOME}\\projectname\\audits\\myproject_%d{yyyy-MM-dd}.%i.zip
</fileNamePattern>
<maxHistory>10</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date %msg%n
</pattern>
</encoder>
</appender>
As mentioned in the documentation for fileNamePattern, you can specify multiple %d tokens so as put the date in the folder name of the archive filename:
<fileNamePattern>${PROJECT_HOME}\\projectname\\audits\\%d{yyyy-MM, aux}\\myproject_%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
Note that only one %d token can be primary, all other tokens must be marked as auxiliary by passing the 'aux' parameter.
But if you also want to put it in the file name of the non-archive filename, then you have two options:
use a <timestamp /> element to set a variable which you use in the path. But this timestamp will only be set once at startup, so it's good for batch runs but not for services.
Do like (1) above, but wrap the <appender/> and the <timestamp /> with a SiftingAppender, which will enable the timestamp to be re-evaluated, if using version of logback >=1.0.12. Not sure exactly how you'd want to configure the SiftingAppender. But hopefully that will put you on the right track.

Categories

Resources