Logback not working with Prudent Mode is true - java

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.

Related

Logback messages being appended to archive file

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>

Logback log to main file and clients in a separate directory

I have a Java server application for which I log all the general data to server.log and each individual client to its own hostname.log file. I want to put the hostname.log files in a different directory that server.log ideally in some organized fashion since there are thousands of hostname.log files.
This is the config I'm using now:
<appender name="SiftAppender" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>descriminatorid</key>
<defaultValue>server</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${descriminatorid}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${cmb.log.dir}/${descriminatorid}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${cmb.log.dir}/archive/${descriminatorid}.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!-- keep ${maxbackupindex} days' worth of history capped at ${maxfilesize} total size -->
<maxHistory>${cmb.log.maxbackupindex}</maxHistory>
<totalSizeCap>${cmb.log.maxfilesize}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %msg%n</pattern>
</encoder>
</appender>
</sift>
</appender>
This config logs everything to $cmb.log.dir. What I'd like is something like this:
Server logs: $cmb.log.dir/server.log
Client logs: $cmb.log.dir/client/${descriminatorid}.log
Or even better would be:
Client logs: $cmb.log.dir/client/${firstLetter}/${descriminatorid}.log
where ${firstLetter} is the first letter of $descriminitorid. That way the logs would be distributed in a more scalable hierarchy.
The solution I found was very simple. Since I am generating ${descriminatorid} in my java code I simple included the directory structure i wanted in that value. So instead of "descriminatorid" being "hostname" it's now "client/firstletter/hostname".
I was definitely trying to overthink this one.

Depending on springProfiles[dev, stg, prod] in logback-spring.xml , how to activate corresponding spring profile passed in command line

In my application-properteis, i have set spring.profiles.active=dev, but when i pass from command line via gradle clean -Dspring.profiles.active=stg build, it is taking by default dev profile only. how ot overwrite the default value set in application.properteis.
You can use below configuration using profile. Active profiles can be set using command line argument -Dspring.profiles.active="abc". For multiple profiles, you can include profile names comma separated.
<?xml version="1.0" encoding="UTF-8"?>
<appender name="rootappender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<springProfile name="abc">
<file>${LOG_ROOT}/abc.log</file>
</springProfile>
<springProfile name="xyz">
<file>${LOG_ROOT}/xyz.log</file>
</springProfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover. Make sure the path matches the one in the file element
or else the rollover logs are placed in the working directory. -->
<fileNamePattern>${LOG_ROOT}/system_%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

Logback RollingFileAppender not working in concurrent writing to same log?

We have a desktop application and recently introduce logback to the system.All the logs are going to the same place.And it's rolling nicely for single client.But if we run two client of the same application log file not rolling even after it's exceed its limit.If one client close rolling happens nicely.
<appender name="LOG-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\log\log.txt</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>\log\old\log.%d{yyyyMMdd}_%i.txt</fileNamePattern>
<maxHistory>2</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy.MM.dd HH:mm:ss} > [%thread] %-5level - %msg%n</Pattern>
</layout>
By default writing from multiple applications to the same file is supported (it might work for you, but you can see unexpected behavior when they both try to write at the same time).
You can solve this by enable prudent mode for your appender. This is done by setting prudent="true" as an attribute of your <appender> tag.
Note that for RollingFileAppender extra restrictions apply. For you this means you can not specify the 'file' property. Also you can not use the SizeAndTimeBasedFNATP but you have to change to a TimeBasedRollingPolicy.

Logback programmatically set to log to a gzip file

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.

Categories

Resources