Logback date-based file rolling doesn't seem to work - java

I use Logback 1.0.10 and I'm trying to get log file rolling to work, but no matter what I do, whenever a new day comes, it just truncates the log file.
Any ideas? I use Java 6 and this is for a webapp running in Tomcat 6 on a Windows 2008 server.
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="10 seconds">
<!--Daily rolling file appender -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/${log.name}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.name}.%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root>
<level value="${log.level}" />
<appender-ref ref="FILE" />
</root>
</configuration>

I realize now that I forgot ${log.dir} in FileNamePattern and ta-da, the old log file was in the parent directory.

Related

Change HikariCP Logging file

I am currently using Sl4j 1.7.25 with logback-classic-1.2.3
The logback.xml put inside the class path of the tomcat i.e WEB-INF/classes is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
This works fine and everything gets logged inside the foodini.log file under tomcat logs directory.
Now unfortunately, HikariCp also logs to the same file and constantly at an interval of around 30 seconds. It basically writes pool events and leaks and all. I need this to go in a separate file say hikari.log rather than inside foodini.log which is meant to be the logs of just my webapp to avoid clutter.
I tried :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
It does create a new file called hikari.log but all things still get logged inside foodini.log
For any one servlet i use :
private static final Logger LOG = LoggerFactory.getLogger(ServeletName.class);
to get the logger and it works fine.
EDIT: added a logger configuration to the logback file and now logs go to both the files foodini.log and hikari.log... Now i need the hikari logs from going in foodini.log file at all
For anyone interested, the configuration logback.xml i created is nearly correct, only one thing was missing and that was the additivity property on the logger element. Without that set to false, the hikari logs would go to both files instead of exclusively going to hikari.log files, heres the updated and working configuration example to get two different log file, one fore the tomcat webapp and one for hikari config,pool and error logs
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari" level="ALL" additivity="false">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>

Spring boot - number of backup log files restricted to 7

In our spring-boot project we are using slf4j for logging purpose. Below are configuration which we have added in application.properties file
logging.file=/opt/logs/my_log.log
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.level.nl.yestelecom.boss=DEBUG
logging.level.com.github.isrsal.logging.LoggingFilter=DEBUG
It generates only 7 backup files (my_log.log.1, my_log.log.2 ..., my_log.log.7) with each file of size 10.5MB and after that logging is not happening at all.
Is there any way to change this behavior?
We looked into available properties of spring-boot but, didn't find anything. Any suggestion is appreciated.
Spring-Boot only allows limited properties to be configured in its application.properties. See the list here.
The default (out-of-the-box) configuration that Spring-boot uses is defined in base.xml. See base.xml config here which includes this File appender
There are 2 ways to add extra configuration
Add logback-spring.xml
If there is a logback configuration XML with name logback-spring.xml in project's classpath, it is picked up by Spring-Boot on initialization.
Point to config file from application.properties
Within application.properties use following to point to your custom logback XML
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
Once you add the extra config using any of the above 2 steps, the rollover strategy can be mentioned within that custom XML like this
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE"/>
</root>
</configuration>
SFL4J is just wrapper. You need to add extra configuration for logback library:
<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 capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
source
In this case we have logs from last 30 days but not bigger than 3GB.
for spring-boot 2.0.0:
logging.file.max-history
logging.file.max-size
...and others
Either look at org.springframework.boot.logging.LoggingSystemProperties or
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-file-output

Configure logback through Eclipse for Tomcat logging

I configured my logback.xml to create a ATSLog.log file and save every day a log file with the date.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>.%d{yyyy-MM-dd}.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n
</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
</appender>
<appender name="dailyRollingFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${CATALINA_HOME}/logs/ATSLog.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<FileNamePattern>${CATALINA_HOME}/logs/ATSLog_%d{yyyy-MM-dd}.%i.log
</FileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n
</Pattern>
</encoder>
</appender>
<logger name="org.hibernate.type" level="ERROR" />
<logger name="org.hibernate" level="ERROR" />
<root>
<level value="INFO" />
<appender-ref ref="dailyRollingFileAppender" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
But in my /opt/apache-tomcat-8.0.26/logs I find only one old ATSLog.log and no other log saved. Do you see an error in my configuration?
Thanks
Make sure you have added CATALINA_BASE in you environment variables. In your config file you have specified the log file path relative to CATALINA_BASE using the expression ${catalina.base}. If its not in environment variables, try adding it. If not set, you can probably expect the logs in /log/ATSLogs.log.
Try this project: https://github.com/grgrzybek/tomcat-slf4j-logback
This project provides the necessary JAR files and configuration files to integrate you instance of Tomcat with SLF4J + Logback, replacing the logging code embedded within the default Tomcat distribution. Refer to the Quick Start section of that project's README for instructions and download links.
I've used it on production and Eclipse environments with much success. Note the extra steps at the bottom of the project's README file for Eclipse environments.

How to configure rollingPolicy while using receiver component?

I would like to have a log file that can be rolled at the beginning of the next day or if it's reached to specified file size. But my log file receives logging events from a remote appender.
My log.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property name="LOG_PORT" value="6000" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/logname.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/logname-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date{MMM dd yyyy HH:mm:ss.SSS}| [%thread] [%-5level] %logger{35}- %msg%n</pattern>
</encoder>
</appender>
<receiver class="ch.qos.logback.classic.net.server.ServerSocketReceiver">
<port>${myport}</port>
</receiver>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
But with above configuration the log file is not rolled if the log file's size reached to 10 MB. So I wonder if Rolling policy still running for ServerSocketReceiver component.
Please help to advise me.

Redirecting logs for JUL to logback using SLF4J LevelChangePropagator fails

I'm redirecting all logs meant for JUL to logback using jul-to-slf4j. But it works if I use the SLF4JBridgeHandler approach but I cannot see the logs getting written when I use more performant LevelChangePropagator approach by adding following lines to config files(logback.xml & logback-test.xml):
<configuration debug="true">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
.......
</configuration>
No logs are getting written.
Edit:
Here is what my full config file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -> %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>c:/dev/logs_test/log_01.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -> %msg%n</pattern>
</encoder>
</appender>
<logger name="px10" level="TRACE"/>
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Tried with JSF(Myfaces 2.1.8) App on Glassfish 3.1.1
Have your tried setting the level of JSF (MyFaces) loggers in your logback.xml config?
<configuration debug="false">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
<logger name="org.apache.myfaces" level="DEBUG"/>
</configuration>
LevelChangePropagator does not do anything unless there level changes to propagate. The fact that logging in other parts of your application work as you expect, only means that those parts are configured correctly for logback, but not necessarily j.u.l. For MyFaces you want to configure j.u.l. by propagating your logback configuration via LevelChangePropagator.

Categories