Logback overrides dependencies logback.xml - java

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

Related

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

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.

Logback leaving .tmp files with multiple instances in Jboss with springBoot

We are deploying the application in multiple instances of JBoss. We log back with version 1.1.11 and spring-boot 1.5.10. When log back is trying to append the log file and rolling back as a .zip file it's leaving .tmp files. Below is the scenario
When the application is running in three instances, it's appending only one instance logs into the zip leaving other two instances as .tmp files, which actually should be appended under the same zip file. This is a blocker in our production, with this behavior we are unable to trace out the issue.
<configuration>
<property resource="bootstrap.yml" />
<springProperty name="appName" source="spring.application.name"/>
<springProperty name="loglevel" source="logging.level.com.some.package"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d {ISO8601}%-5p [%c{3}] [%t] %m%n</pattern>
</encoder>
</appender>
<appender name ="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/appllogs/spp/$
{appName}.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/appllogs/backup/${appName}/%d{yyyy-MM-dd}
%i.log.zip</fileNamePattern>
<maxHistory>7</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d %-5p %m%n</pattern>
</encoder>
</appender>
<logger name="com.some.package" level="$
{loglevel}" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="APPLICATION"/>
</logger>
<root level="${loglevel}
" >
<appender-ref ref="CONSOLE" />
<appender-ref ref="APPLICATION"/>
</root>
</configuration>

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>

Logback creating logs at maven run but not when it deployed

Using log-back for logging.When building project with Maven its creating log files and writing logs but not same, when running Spring Runner test case or deployed on server.Its just creating empty log file.
Following is my Configuration,
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="1000000 seconds">
<appender name="consoleOutput"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{hh:mm:ss} %-5level %c:%M:%L %m%n- %msg%n</pattern>
</layout>
</appender>
<!-- Appender: for logging all the Logs into the Log file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ALL</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<customFilSystem>true</customFilSystem>
<!-- rollover daily -->
<fileNamePattern>log/LOGFILE-%d{yyyy-MM-dd}.%i.txt.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
<minIndex>1</minIndex>
<maxIndex>2</maxIndex>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{hh:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="ALL" additivity="false">
<appender-ref ref="FILE"/>
</root>
<root level="info">
<appender-ref ref="consoleOutput"/>
</root>
<!-- Specify the package or module and reference of respective Appender -->
<logger name="org.springframework" level="OFF" />
</configuration>

Rolling logback logs on filesize and time

I've been trying to set up a simple logback project to roll my log files by date and by filesize, and so far I have been unable to get my appender to roll over to another file. Instead, it writes to the log specified by the <file/> tag.
Here is my logback.xml configuration file:
<?xml version="1.0"?>
<configuration scan="true" scanPeriod="10 seconds">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</layout>
</appender>
<appender name="milliroller" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/output.log</file>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="stdout"/>
<appender-ref ref="milliroller"/>
</root>
<logger name="com.tkassembled.logback" level="DEBUG"/>
</configuration>
At first glance, it looks like it should work, right? Is there something I'm doing wrong? My entire, buildable project is available in a zip here: http://www.mediafire.com/file/2bxokkdyz2i/logback.zip
Although this is an old question, I felt that a working answer is appropriate to help anyone who requires this kind of implementation.
I use the following logback configuration to provide an HTML log, rolled over by date and filesize, as well as logging to console for debugging output.
Logfiles are stored in a logs directory with a name of logFile.html while its active, and logFile.2013-mm-dd.i.html when it rolls over, where i is the number of 50MB log files. For instance logFile.2013-01-07.0.html.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{35}) - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs\logFile.html</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs\logFile.%d{yyyy-MM-dd}.%i.html</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 50MB -->
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
</layout>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Since logback 1.1.7 (released March 2016) a new policy called SizeAndTimeBasedRollingPolicy is available that dramatically simplifies what you need to do:
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>app-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
See here for further info.

Categories