Logback-spring.xml creating multiple log files when configured with springProfile - java

I am getting an issue while adding <springProfile> in my logback-spring.xml. In my case, I need to get the file name as well as path from the application.properties file and provide a date based rolling policy. So I can't specify the file name and path in logback-spring.xml.
So whenever I run the project specific to the profile from my IDE or by executable jar file, I am getting two log files created, one at the current working directory and the other at the specified file path location mentioned in the application.properties file.
I have three profiles based on the three different environments i.e. Local, Dev and Prod.
for local I am using spring OOTB application.properties file.
spring.profiles.active=local
server.port=8080
#logging
logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml
and for dev and prod i am using application-dev.properties and application-prod.properties respectively.
application-dev.properties
spring.profiles.active=dev
server.port=9090
#logging
logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs/dev
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml
Here is my logback-spring.xml.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include
resource="org/springframework/boot/logging/logback/base.xml" />
<springProfile name="prod">
<property resource="application-prod.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %X{clientapp} %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
<springProfile name="uat">
<property resource="application-uat.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
<springProfile name="local">
<property resource="application.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
</configuration>

So after a long time I got to know the extra log file was created because of the property inside application.properties files.
The below property is honored by spring to create a file also I was mentioning the file name as well as the file path in the logback-spring.xml.
logging.file.name=app-Log
So whenever I run the application spring see this above property and creates the file in the current working directory and one other file in the specified location in the logback-spring.xml file.
${logging.file.path}/${logging.file.name}.log
So if you are mentioning the file name and path do not use the reserved words. I know its a very silly mistake but if it helps anyone then this will not be in vain.

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 application properties for logging

I've seen many examples of configuring logging in Spring Boot, but what I look for is the simplest way to get the job done modifying this pattern in my application.properties:
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
logging.file=/Users/alessandroargentieri/Desktop/try/application.log
This works pretty fine but what I would like to get is set a MB limit for the single file and set that every day I want to have a different log file (with the date in the filename).
Is possible to get this adding some lines in the application.properties without using XML or JSON files?
I think you need to override the default logging config with your own custom configuration. Spring boot provide logging support with minimum configuration and it's limited. It uses internally base.xml file which includes the below file-appender.xml:
<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>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
As you can see only few properties can be injected.
You can go through the below link to setup your custom configuration:
https://dzone.com/articles/configuring-logback-with-spring-boot
https://springframework.guru/using-logback-spring-boot/
No. The easiest way to do that is with an extra xml file.
I'm using logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<property name="DEV_HOME" value="c:/logs" />
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>
${DEV_HOME}/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="ar.com" level="debug" additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="error">
<appender-ref ref="FILE-AUDIT" />
</root>

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

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.

Categories