Search code examples
javalogback

logback how to log only the classname not also the package path?


I have an appender in my logback xml config:

    <encoder>
        <pattern>%d{"yyyy-MM-dd HH:mm:ss"} [%thread] %-5level %logger{5} - %msg %n</pattern>
    </encoder>

but it prints out the abbreviated package along with the class, even if I set it to %logger{1} like:

2019-12-19 10:26:16 [main] INFO o.f.d.d.u.Myclass - my message

I want it to just log the class MyClass like log4j does. How?


Solution

  • looks like the number you specify for %logger is some kind of special case and doesnt' exactly dictate size. Except it does dictate size, but never less than a minimum size which is the entire package path with the full class name. Unless you set it to "0" which is a special case to mean "don't include the package".

    So if I set it to %logger{0} then I get just the class name, see http://logback.qos.ch/manual/layouts.html#conversionWord

    2019-12-19 10:28:45 [main] INFO MyClass - my message

    In log4j the equivalent would have been %c{1} or %logger{1} so I guess it's different.