简体   繁体   中英

Logback printing %n instead of new line

I am using Logback through Slf4j. I have an appender configured as follows:

    <appender name="PLAN_STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- On Windows, this will also require org.fusesource.jansi:jansi:1.17
             (or higher) on the classpath.  On a Mac, it works without it -->
        <withJansi>true</withJansi>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %blue(%msg) %n</pattern>
        </encoder>
    </appender>

When I send messages to that logger, they appear on the console (as expected) with the text in blue (as expected). However, after the text, it prints %n rather than a new line, so the output looks like

17:36:50.184 This text is in blue %n17:36:50.200 This text is one the same line

I am not doing anything complicated elsewhere. I get the logger by

private final static Logger planLogger = LoggerFactory.getLogger("PLAN")

and use it like

logger.info("=== Summary ===");
for (var d: data) {
   logger.info("  Data: {}", d);
}

I tried to make a simple example to reproduce it with just slf4j-api, Logback-classic and Logback-core as dependencies, and it worked as expected. I suspect that the problem is the full system depends on something else which is interfering, but I don't know what.

In the full system, these are the log-related dependencies that I have.

$ mvn dependency:tree | egrep 'log|slf4j'
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.29:runtime
[INFO] +- ch.qos.logback:logback-classic:jar:1.2.6:runtime
[INFO] |  \- ch.qos.logback:logback-core:jar:1.2.6:runtime
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.17.1:compile
[INFO] \- org.apache.logging.log4j:log4j-core:jar:2.17.1:compile

(I know, depending on log4j and logback is bad. But I am not in charge of enforcing such things.)

I tried updating my pom to include all of those dependencies, but the simple test still works.

I am on a Mac if that matters.

Has any one experienced anything like this or have an idea what could be causing it?

It looks like this is a known issue in logback: %n after color grouping does not work

A comment by David Eckel on that issue shows a workaround:

Workaround: add an empty options list:

 <pattern>%magenta(%message){}%n</pattern>

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