2

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?

Troy Daniels
  • 3,270
  • 2
  • 25
  • 57
  • No idea, but have you tried without jansi? – tgdavies Jan 26 '22 at 23:53
  • You can try: 1. Take your simple example and add dependencies until it stops working. Or 2. Step into the log statement with the debugger until you get to the interpretation of the pattern. Do this in your application and your working example and see what the difference is. – tgdavies Jan 26 '22 at 23:55

1 Answers1

0

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>
Tim Moore
  • 8,958
  • 2
  • 23
  • 34