java Logback - 打印出调用日志方法的方法名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31914337/
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
Logback - Print out method name that called the log method
提问by RAnders00
In my example, I have the following code:
在我的示例中,我有以下代码:
package com.example.somepackage;
public class Example {
public static void main(String[] args) {
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Example.class);
logger.info("Example message.");
}
}
I want it to output to the console like this minimal example:
我希望它像这个最小的例子一样输出到控制台:
com.example.somepackage.Example.main Example message.
The only thing I need is either:
我唯一需要的是:
- a pattern to put into the
logback.xml
file or - instructions on further code I need to add to my example code scenario
- 要放入
logback.xml
文件的模式或 - 关于我需要添加到示例代码场景的更多代码的说明
If extra code is used instead of a XML configuration pattern, please also provide information on how to pair these two, so one can still use patterns for time, log level, etc. in addition to the method name being printed out.
如果使用额外的代码而不是 XML 配置模式,还请提供有关如何配对这两者的信息,这样除了打印出方法名称之外,仍然可以使用时间、日志级别等模式。
回答by mthmulders
I think you're looking for the %M
pattern.
See the Logback docsfor all patterns that you could use.
Note, however, that these docs also say:
我认为你正在寻找%M
模式。有关
您可以使用的所有模式,请参阅Logback 文档。但是请注意,这些文档还说:
Generating the method name is not particularly fast. Thus, its use should be avoided unless execution speed is not an issue.
生成方法名称并不是特别快。因此,除非执行速度不是问题,否则应避免使用它。