log4j2在使用多个除了root之外的记录器时的类名

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

log4j2 class name when using multiple loggers besides root

问题

I'm using log4j2.
I have defined multiple loggers, so that, my log4j2.xml looks like:

<Property name="LOG_PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</Property>
...
...
<Loggers>
    <Logger name="trace" level="INFO" additivity="false">
        <AppenderRef ref="console" />
    </Logger>
    <Logger name="error" level="ERROR" additivity="false">
        <AppenderRef ref="errorLog" />
    </Logger>
    <Logger name="warn" level="WARN" additivity="false">
        <AppenderRef ref="warnLog" />
    </Logger>
</Loggers>

Then I invoke them from code. Snippet:

Class myclass{
...

    Logger LOGGER1 = LogManager.getLogger("trace");
    Logger LOGGER2 = LogManager.getLogger("error");
...
    LOGGER1.trace("whatever message");

My problem is that, when invoking the logger from code, as far as I know, I get to specify the logger from log4j2.xml. For example, if I want to use "trace logger" I get to code LogManager.getLogger("trace")

Then, when "printing the log", %c{1} is not any longer the classname, it is the logger name...
In this example, log4j2 will print as %c "trace" and not "myclass" (that is what I want).

I want to know, is it possible:

  • option 1. call a concrete logger, while printing the "real" classname
    (not the classname passed as a parameter in the getLogger method),
  • option 2. using the classname in the getLogger Method and them
    mapping this logger from code to a logger from the log4j2.xml

thanks in advance for your clues.

英文:

I'm using log4j2.
I have defined multiple loggers, so that, my log4j2.xml looks like:

   &lt;Property name=&quot;LOG_PATTERN&quot;&gt;[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n&lt;/Property&gt;
   ...
   ...
   &lt;Loggers&gt;
        &lt;Logger name=&quot;trace&quot; level=&quot;INFO&quot; additivity=&quot;false&quot;&gt;
            &lt;AppenderRef ref=&quot;console&quot; /&gt;
        &lt;/Logger&gt;
        &lt;Logger name=&quot;error&quot; level=&quot;ERROR&quot; additivity=&quot;false&quot;&gt;
            &lt;AppenderRef ref=&quot;errorLog&quot; /&gt;
        &lt;/Logger&gt;
        &lt;Logger name=&quot;warn&quot; level=&quot;WARN&quot; additivity=&quot;false&quot;&gt;
            &lt;AppenderRef ref=&quot;warnLog&quot; /&gt;
        &lt;/Logger&gt;
   &lt;/Loggers&gt;

Then I invoke them from code. Snippet:

Class myclass{
...

    Logger LOGGER1 = LogManager.getLogger(&quot;trace&quot;);
    Logger LOGGER2 = LogManager.getLogger(&quot;error&quot;);
...
     LOGGER1.trace(&quot;whatever message&quot;);

My problem is that, when invoking the logger from code, as far as I know, I get to specify the logger from log4j2.xml. For example, if I want to use "trace logger" I get to code LogManager.getLogger("trace")

Then, when "printing the log", %c{1} is not any longer the classname, it is the logger name...
In this example, log4j2 will print as %c "trace" and not "myclass" (that is what I want).

I want to know, is it possible:

  • option 1. call a concrete logger, while printing the "real" classname
    (not the classname passed as parameter in the getLogger method),
  • option 2. using the classname in the gerLogger Method and them
    mapping this logger from code to a logger from the log4j2.xml

thanks in advance for your clues.

答案1

得分: 8

在log4j2.xml文件中,在定义日志模式时,您可以使用两种不同的东西:"c"(小写)和"C"(大写)。

  1. "c"将记录为日志记录器名称,
  2. "C"将记录为创建日志记录器的类(在其中执行了%M的类)。

包含第二种情况的可能模式如下所示:

<Property name=&quot;LOG_PATTERN&quot;>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C][%M] - %msg%n</Property>

更多信息请参见:http://logging.apache.org/log4j/2.x/manual/usage.html#LoggerVsClass

无论如何,之前由https://stackoverflow.com/users/1709216/rgoers提出的文章都是宝藏。

英文:

In the log4j2.xml file, when defining the log pattern you can use two DIFFERENT things: "c" (lower case) and "C" (upper case).

  1. "c" will be log as the logger name and
  2. "C" will be log as the class where the logger was created (class where %M is been executed).

A possible pattern including the second would look something like:

&lt;Property name=&quot;LOG_PATTERN&quot;&gt;[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C][%M] - %msg%n&lt;/Property&gt;

more info here: http://logging.apache.org/log4j/2.x/manual/usage.html#LoggerVsClass

Anyway, the articles proposed by https://stackoverflow.com/users/1709216/rgoers previously, are a gold mine.

答案2

得分: 0

你的问题让人觉得你还没有阅读log4j的文档或阅读关于如何使用它的文章。特别是你应该查看:

英文:

Your questions make it sound like you have not read the log4j documentation or read any of the articles on how to use it. In particular you should review:

答案3

得分: 0

<Property name="LOG_PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C][%M] - %msg%n</Property>

c - 用于输出日志事件的类别。例如,对于类别名称 "a.b.c",模式 %c{2} 将输出 "b.c"。

C - 用于输出发出日志请求的调用方的完全限定类名。例如,对于类名 "org.apache.xyz.SomeClass",模式 %C{1} 将输出 "SomeClass"。

英文:
&lt;Property name=&quot;LOG_PATTERN&quot;&gt;[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C][%M] - %msg%n&lt;/Property&gt;

c - Used to output the category of the logging event. For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".

C - Used to output the fully qualified class name of the caller issuing the logging request. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".

huangapple
  • 本文由 发表于 2020年8月10日 18:06:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63338125.html
匿名

发表评论

匿名网友

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

确定