Log4j show package name

牧云@^-^@ 提交于 2019-12-21 03:48:25

问题


Right now for my ConversionPattern I have:

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.


回答1:


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

From your referenced docs:

%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.

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:

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

Using %c is not slow.




回答2:


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

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.

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



来源:https://stackoverflow.com/questions/2594818/log4j-show-package-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!