java Log4j 显示包名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2594818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:58:03  来源:igfitidea点击:

Log4j show package name

javajakarta-eelog4j

提问by Stephane Grenier

Right now for my ConversionPattern I have:

现在对于我的 ConversionPattern 我有:

log4j.appender.A1.layout.ConversionPattern=%d{yyyy MMM dd HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n

What I'd like to do is also include the full package name with the class (%F:%L) but I can't find any config to do so in the docs. I do understand that this will be slower, but it's only for debugging and not when the system will be in production.

我想做的是在类中包含完整的包名 (%F:%L) 但我在 docs 中找不到任何配置。我确实理解这会更慢,但它仅用于调试,而不是系统投入生产时。

回答by Michael Konietzka

Maybe I just misunderstand you, but %C will output your class with package.

也许我只是误解了你,但是 %C 会用 package.json 输出你的类。

From your referenced docs:

从您引用的文档中:

%C

%C

Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.

If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form.

For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".

WARNING Generating the caller class information is slow. Thus, use should be avoided unless execution speed is not an issue.

用于输出发出日志请求的调用者的完全限定类名。此转换说明符可以选择后跟精度说明符,即括号中的十进制常量。

如果给出了精度说明符,则只会打印类名最右边部分的相应数量。默认情况下,类名以完全限定的形式输出。

例如,对于类名“org.apache.xyz.SomeClass”,模式 %C{1} 将输出“SomeClass”。

警告生成调用者类信息很慢。因此,除非执行速度不是问题,否则应避免使用。

Update:In many cases you can use %c also, which will print out the full class with package also, if your category is your class-name. For example when your doing stuff like this when initializing your Log:

更新:在许多情况下,您也可以使用 %c,如果您的类别是您的类名,它也会打印出带有包的完整类。例如,当您在初始化 Log 时做这样的事情时:

private static final Log LOG = LogFactory.getLog(MyClazz.class);

Using %c is not slow.

使用 %c 并不慢。

回答by Rakesh Dondlapally

Using C{1}is slow. Please see the details below:

使用C{1}很慢。请参阅以下详细信息:

As per the following link:

根据以下链接

Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form.

For example, for the class name org.apache.xyz.SomeClass, the pattern %C{1}will output SomeClass.

WARNINGGenerating the caller class information is slow. Thus, use should be avoided unless execution speed is not an issue.

用于输出发出日志请求的调用者的完全限定类名。此转换说明符可以选择后跟精度说明符,即括号中的十进制常量。如果给出了精度说明符,则只会打印类名最右边部分的相应数量。默认情况下,类名以完全限定的形式输出。

例如,对于类名org.apache.xyz.SomeClass,模式%C{1}将输出SomeClass.

警告生成调用者类信息很慢。因此,除非执行速度不是问题,否则应避免使用。