简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM