英文:
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:
<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 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"(大写)。
- "c"将记录为日志记录器名称,
- "C"将记录为创建日志记录器的类(在其中执行了%M的类)。
包含第二种情况的可能模式如下所示:
<Property name="LOG_PATTERN">[%-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).
- "c" will be log as the logger name and
- "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:
<Property name="LOG_PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C][%M] - %msg%n</Property>
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的文档或阅读关于如何使用它的文章。特别是你应该查看:
-
Log4j 2 配置 - 特别是关于日志记录器的部分
-
Pattern Layout - 查看类、文件、方法、行和位置转换模式。
英文:
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:
- Log4j 2 Architecture
- Log4j 2 Configuration - in particular the section on Loggers
- Pattern Layout - See the class, file, method, line, and location conversion patterns.
- Articles
答案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"。
英文:
<Property name="LOG_PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C][%M] - %msg%n</Property>
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".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论