log4j2如何使用%c{precision}或logger{precision}来控制日志记录器名称。

huangapple go评论94阅读模式
英文:

log4j2 How to control logger name using %c{precision} OR logger{precision}

问题

project.class 的日志名称遵循约定:project.class

我希望确保在日志中,除了最后两个前缀之外的所有前缀都缩写为一个字母,最后两个前缀以完整形式打印出来。所以我有我的日志记录器的全名以及其他日志记录器的缩写名称,例如:

  • project.class 打印为 project.class
  • org.apache.spark.util.Utils 打印为 o.a.s.util.Utils

根据文档(按照我的理解),%c{1.2.*} 是适合我的模式。

但是当我将模式设置为:
%c %c{1} %c{2} %c{1.2.*} %c{1.3.*} %c{1.*.*} %c{2.*.*}%n

>>> l = spark.sparkContext._jvm.org.apache.log4j.LogManager.getLogger('project.class')
>>> o = spark.sparkContext._jvm.org.apache.log4j.LogManager.getLogger('org.apache.spark.util.Utils')
>>> l.info(''); o.info('')
project.class    class    project.class    p.class    p.class    p.class    pr.class
org.apache.spark.util.Utils    Utils    util.Utils    o.ap.spark.util.Utils    o.apa.spark.util.Utils    o.apache.spark.util.Utils    or.apache.spark.util.Utils
>>>

我想要的是它打印出:project.classo.a.s.util.Utils

官方文档中的描述似乎非常令人困惑:

c{precision}

logger{precision}

输出发布日志事件的记录器的名称。记录器转换说明符可以选择后跟精度说明符,该说明符由十进制整数或以十进制整数开头的模式组成。

当精度说明符是一个整数值时,它会减小记录器名称的大小。如果数字为正数,则布局将打印右侧记录器名称组件的相应数量。如果为负数,则布局将删除左侧记录器名称组件的相应数量。如果精度包含句点,则第一个句点之前的数字标识要从模式其余部分的令牌之前的项目中打印的长度。如果第一个句点后的数字后跟一个星号,它表示将以完整形式打印最右侧的令牌中的多少个。请参阅下表以获取缩写示例。

如果精度包含任何非整数字符,则布局会根据模式对名称进行缩写。如果精度整数小于1,则布局仍会以完整形式打印最右侧的令牌。默认情况下,布局以完整形式打印记录器名称。

转换模式 记录器名称 结果
%c{1} org.apache.commons.Foo Foo
%c{2} org.apache.commons.Foo commons.Foo
%c{10} org.apache.commons.Foo org.apache.commons.Foo
%c{-1} org.apache.commons.Foo apache.commons.Foo
%c{-2} org.apache.commons.Foo commons.Foo
%c{-10} org.apache.commons.Foo org.apache.commons.Foo
%c{1.} org.apache.commons.Foo o.a.c.Foo
%c{1.1.~.~} org.apache.commons.test.Foo o.a.~.~.Foo
%c{.} org.apache.commons.test.Foo ....Foo
%c{1.1.1.*} org.apache.commons.test.Foo o.a.c.test.Foo
%c{1.2.*} org.apache.commons.test.Foo o.a.c.test.Foo
%c{1.3.*} org.apache.commons.test.Foo o.a.commons.test.Foo
%c{1.8.*} org.apache.commons.test.Foo org.apache.commons.test.Foo
英文:

My logger names follow the convention: project.class

I want to ensure that, in logs, all prefixes except the last 2 are abbreviated to one letter each and the last 2 are printed in full. So I have full names for my loggers AND abbreviated names for other loggers E.g.:

  • project.class is printed as project.class
  • org.apache.spark.util.Utils is printed as o.a.s.util.Utils

According to documentation (as I interpret it) %c{1.2.*} is the right pattern for me.

But when I set pattern to:
%c %c{1} %c{2} %c{1.2.*} %c{1.3.*} %c{1.*.*} %c{2.*.*}%n

>>> l = spark.sparkContext._jvm.org.apache.log4j.LogManager.getLogger('project.class')
>>> o = spark.sparkContext._jvm.org.apache.log4j.LogManager.getLogger('org.apache.spark.util.Utils')
>>> l.info(''); o.info('')
project.class    class    project.class    p.class    p.class    p.class    pr.class
org.apache.spark.util.Utils    Utils    util.Utils    o.ap.spark.util.Utils    o.apa.spark.util.Utils    o.apache.spark.util.Utils    or.apache.spark.util.Utils
>>>

What I want is for it to print: project.class and o.a.s.util.Utils instead.


The description in official documentation seems VERY confusing:

> c{precision}
>
> logger{precision}
>
> Outputs the name of the logger that published the logging event. The logger conversion specifier can be optionally followed by precision specifier, which consists of a decimal integer, or a pattern starting with a decimal integer.
>
> When the precision specifier is an integer value, it reduces the size of the logger name. If the number is positive, the layout prints the corresponding number of rightmost logger name components. If negative, the layout removes the corresponding number of leftmost logger name components. If the precision contains periods then the number before the first period identifies the length to be printed from items that precede tokens in the rest of the pattern. If the number after the first period is followed by an asterisk it indicates how many of the rightmost tokens will be printed in full. See the table below for abbreviation examples.
>
> If the precision contains any non-integer characters, then the layout abbreviates the name based on the pattern. If the precision integer is less than one, the layout still prints the right-most token in full. By default, the layout prints the logger name in full.

Conversion Pattern Logger Name Result
%c{1} org.apache.commons.Foo Foo
%c{2} org.apache.commons.Foo commons.Foo
%c{10} org.apache.commons.Foo org.apache.commons.Foo
%c{-1} org.apache.commons.Foo apache.commons.Foo
%c{-2} org.apache.commons.Foo commons.Foo
%c{-10} org.apache.commons.Foo org.apache.commons.Foo
%c{1.} org.apache.commons.Foo o.a.c.Foo
%c{1.1.~.~} org.apache.commons.test.Foo o.a.~.~.Foo
%c{.} org.apache.commons.test.Foo ....Foo
%c{1.1.1.*} org.apache.commons.test.Foo o.a.c.test.Foo
%c{1.2.*} org.apache.commons.test.Foo o.a.c.test.Foo
%c{1.3.*} org.apache.commons.test.Foo o.a.commons.test.Foo
%c{1.8.*} org.apache.commons.test.Foo org.apache.commons.test.Foo

答案1

得分: 2

你要查找的模式是 2.20.0 版本引入的 %c{1.2*}(参见 LOG4J2-2785)。

现在有四种 %c 模式:

  • 如果参数是正整数 n(例如 %c{7}),只会打印日志记录器名称的最右边的 n组件
  • 如果参数是负整数 -n(例如 %c{-7}),则会丢弃最左边的 n组件
  • 如果参数形如 1.n*,则最右边的 n组件 会完整输出,而其余的会缩写为一个字母。
  • 对于任何其他模式,数字表示从每个组件中打印多少个 字母:例如 1.4~.7 表示从第一个组件中取1个字母,然后是一个点 .,然后是第二个组件中的4个字母(如果名称被缩写则会加上 ~),然后是一个点 .。其余组件会缩写为7个字母,带有点 .。最后一个组件会完整输出。
英文:

The pattern you are looking for is %c{1.2*} introduced in version 2.20.0 (cf. LOG4J2-2785).

There are now four kinds of %c patterns:

  • if the parameter is a positive integer n (e.g. %c{7}), only n rightmost components of the logger name are printed,
  • if the parameter is a negative integer -n (e.g. %c{-7}), then the leftmost n components are dropped,
  • if the parameter is of the form 1.n*, the n rightmost components are written in full, while the remaining ones are abbreviated to one letter,
  • for any other pattern the numbers indicate how many letters to print from each component: e.g. 1.4~.7 means 1 letter from the first component followed by a dot . followed by 4 letters from the second component (followed by ~ if the name was abbreviated) and a dot .. The remaining components are abbreviated to 7 letters with a dot .. The last component is written down entirely.

huangapple
  • 本文由 发表于 2023年6月6日 01:11:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定