在 Log4j 模式布局中右对齐 compound/composite 表达式?

Right-justify compound/composite expression in Log4j pattern layout?

我希望我的日志看起来像这样:

      ClassName.methodName() - just did something
ClassName.methodNameLonger() - just did something else

我知道你可以用 %-17M 右对齐方法名称,但我最终得到的是:

ClassName.methodName      () - just did something
ClassName.methodNameLonger() - just did something else

有没有一种方法可以将多个元素作为一个块来对齐,以便填充仅出现在最开始?

我不确定是否可以查看参考资料: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

更多详情

如果我尝试右对齐 class 名称,我得到:

ClassName      .      methodName() - just did something
ClassName      .methodNameLonger() - just did something else
ClassNameLonger.methodNameLonger() - just did something else

这并不可怕,但它仍然无法将多个字段(包括文字文本 ())视为一个连续的单元。

您是否尝试过将 class 名称右对齐? %-20C{1}.%17M

在另一个回答中提到,Log4j 不支持这个。


但是,logback 会。

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" scan="true">
    <encoder>
      <pattern>%-5level %20(%logger{0}.%method\(\)) - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>

</configuration>

这里有完整的文档:

http://logback.qos.ch/manual/layouts.html#Parentheses

括号比较特殊

在 logback 中,模式字符串中的括号被视为分组标记。因此,可以对子模式进行分组并在该子模式上应用格式化指令。从版本0.9.27开始,logback支持复合转换词,例如%replace可以转换子模式。

例如模式

%-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n

将对子模式 %d{HH:mm:ss.SSS} [%thread] 生成的输出进行分组,以便在少于 30 个字符时进行右填充。

如果没有分组输出是

13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Classload hashcode is 13995234
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Initializing for ServletContext
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Trying platform Mbean server
13:09:30 [pool-1-thread-1] INFO  ch.qos.logback.demo.LoggingTask - Howdydy-diddly-ho - 0
13:09:38 [btpool0-7] INFO c.q.l.demo.lottery.LotteryAction - Number: 50 was tried.
13:09:40 [btpool0-7] INFO c.q.l.d.prime.NumberCruncherImpl - Beginning to factor.
13:09:40 [btpool0-7] DEBUG c.q.l.d.prime.NumberCruncherImpl - Trying 2 as a factor.
13:09:40 [btpool0-7] INFO c.q.l.d.prime.NumberCruncherImpl - Found factor 2

"%-30()" 分组会是

13:09:30 [main]            DEBUG c.q.logback.demo.ContextListener - Classload hashcode is 13995234
13:09:30 [main]            DEBUG c.q.logback.demo.ContextListener - Initializing for ServletContext
13:09:30 [main]            DEBUG c.q.logback.demo.ContextListener - Trying platform Mbean server
13:09:30 [pool-1-thread-1] INFO  ch.qos.logback.demo.LoggingTask - Howdydy-diddly-ho - 0
13:09:38 [btpool0-7]       INFO  c.q.l.demo.lottery.LotteryAction - Number: 50 was tried.
13:09:40 [btpool0-7]       INFO  c.q.l.d.prime.NumberCruncherImpl - Beginning to factor.
13:09:40 [btpool0-7]       DEBUG c.q.l.d.prime.NumberCruncherImpl - Trying 2 as a factor.
13:09:40 [btpool0-7]       INFO  c.q.l.d.prime.NumberCruncherImpl - Found factor 2

后一种形式读起来更舒服。

如果您需要将括号字符视为文字,则需要通过在每个括号前加上反斜杠来对其进行转义。如 \(%d{HH:mm:ss.SSS} [%thread]\).

这绝对可以在 log4j 2 中完成

您可以将整个复合表达式放在 %replace 模式中(不带参数),然后证明 %replace

的结果

因此,如果我想证明复合表达式 [%c{1}:%method:%line]

我会简单地将表达式转储到一个空的替换中,并按如下方式正确证明

%-40.40replace{[%c{1}:%method:%line]}{}{}

结果是一个分组表达式 truncated/expanded 恰好 40 个字符且右对齐(或 %-40.-40replace 左对齐)