Logback-打印出名为log方法的方法名称 [英] Logback - Print out method name that called the log method

查看:1601
本文介绍了Logback-打印出名为log方法的方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的示例中,我有以下代码:

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.

我唯一需要的是:

  • 要放入logback.xml文件或
  • 的模式
  • 我需要添加到示例代码场景中的其他代码的说明
  • a pattern to put into the logback.xml file or
  • instructions on further code I need to add to my example code scenario

如果使用额外的代码代替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.

推荐答案

我认为您正在寻找%M模式. 请参见登录文档 适用于您可以使用的所有模式. 但是请注意,这些文档还说:

I think you're looking for the %M pattern. See the Logback docs for all patterns that you could use. Note, however, that these docs also say:

生成方法名并不是特别快.因此,除非执行速度不是问题,否则应避免使用它.

Generating the method name is not particularly fast. Thus, its use should be avoided unless execution speed is not an issue.

这篇关于Logback-打印出名为log方法的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆