使用Logger的信息打印类名,方法名和行号()

时间:2015-06-01 18:43:02

标签: java logging logback

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

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

我想在输入和现有方法时打印日志消息。我怎么能使用info()方法在类中做到这一点。我使用下面的代码。但它没有打印出我想要的东西。

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

LOG.info("Entering " + );

这就是我得到的:

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

我想打印这样的东西:

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

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:0)

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

上面的模式输出结果如下:

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

如果您想打印出一些精美的版本,这是另一个选择。

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

输出看起来像这样:

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$$3f00c757] 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

  

请记住,“ ...生成呼叫者类信息是   不是特别快。 ...”

     

logback patternlayout docs