使用 Logger 的 info() 打印 Class 名称、方法名称和行号
Print Class Name, Method name and line number using Logger's info()
我已经配置了 logback 文件以获取 class名称、方法名称和行号。
<pattern> %d{HH:mm:ss.SSS} [%thread] %-5level %class{36}.%M %L - %msg%n </pattern>
我想在输入现有方法时打印日志消息。我怎样才能在 class 中使用 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
<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>
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$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
Just keep in mind that "...Generating the caller class information is
not particularly fast. ..."
我已经配置了 logback 文件以获取 class名称、方法名称和行号。
<pattern> %d{HH:mm:ss.SSS} [%thread] %-5level %class{36}.%M %L - %msg%n </pattern>
我想在输入现有方法时打印日志消息。我怎样才能在 class 中使用 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
<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>
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$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
Just keep in mind that "...Generating the caller class information is not particularly fast. ..."