java 使用 Logger 的 info() 打印类名、方法名和行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30580859/
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-11-02 17:20:41  来源:igfitidea点击:

Print Class Name, Method name and line number using Logger's info()

javalogginglogback

提问by amal

I already configured logback file to get classname, method name and line number.

我已经配置了 logback 文件来获取类名、方法名和行号。

 <pattern> %d{HH:mm:ss.SSS} [%thread] %-5level %class{36}.%M %L - %msg%n </pattern>

I want to print log message when entering and existing method. How can i do that within the class using info() method. I used the code below. But it didnt print what i wanted.

我想在输入和现有方法时打印日志消息。我如何使用 info() 方法在课堂上做到这一点。我使用了下面的代码。但它没有打印我想要的。

I use org.slf4j.Logger and Logback logging

我使用 org.slf4j.Logger 和 Logback 日志记录

LOG.info("Entering " + );

This is what I got :

这就是我得到的:

14:41:48.097 [main] INFO  c.a.j.orgchart.CsvPersonReader - Entering 

I want to print something like this:

我想打印这样的东西:

14:41:48.097 [main] INFO  c.a.j.orgchart.CsvPersonReader.[MethodName] [Linenumber] - Entering 

回答by amal

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
     <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level  %class{36}.%M %L  - %msg%n</pattern>
     </encoder>
</appender>

回答by Juha Hanka

logging.pattern.console=%14date{dd.MM.yyyy kk:mm:ss.SSS} [%1level] %30.30logger{1}\.%replace(%replace(%replace(%caller{3..4}){'[\n\r]',''}){'\(.+\)$','()'}){'^.+\.',''} : %msg%n

Above pattern outputs result like below:

以上模式输出结果如下:

10.05.2019 08:35:05.337 [INFO] epositoryConfigurationDelegate.forEach() : Bootstrapping Spring Data repositories in DEFAULT mode.
...

Here's another option if you want to printout little a bit polished version of it.

如果您想打印出稍微改进的版本,这是另一种选择。

logging.pattern.console=%14date{dd.MM.yyyy kk:mm:ss.SSS} [%1level] %40.40logger{0}\.%-35.35replace(%replace(%replace(%caller{3..4}){'[\n\r]',''}){'\(.+\)$','()'}){'^.+\.',''} : %msg%n

Output looks like this:

输出如下所示:

10.05.2019 08:45:57.228 [DEBUG]                 RepositoryConfigurationDelegate.forEach()                           : Bootstrapping Spring Data repositories in DEFAULT mode.
10.05.2019 08:45:59.651 [INFO]          RepositoryConfigurationDelegate.forEach()                           : Finished Spring Data repository scanning in 384ms. Found 3 repository interfaces.
10.05.2019 08:46:00.524 [INFO] trationDelegate$BeanPostProcessorChecker.doCreateBean()                      : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$f00c757] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
10.05.2019 08:46:03.828 [INFO]                          TomcatWebServer.getWebServer()                      : Tomcat initialized with port(s): 8080 (http)
10.05.2019 08:46:03.896 [INFO]                          StandardService.start()                             : Starting service [Tomcat]
10.05.2019 08:46:03.897 [INFO]                           StandardEngine.start()                             : Starting Servlet engine: [Apache Tomcat/9.0.14]

1

1

Just keep in mind that "...Generating the caller class information is not particularly fast. ..."

logback patternlayout docs

请记住“...生成调用者类信息不是特别快。...”

logback 模式布局文档