amal
amal

Reputation: 3582

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

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

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

I use org.slf4j.Logger and Logback logging

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 

Upvotes: 8

Views: 22171

Answers (2)

Juha Hanka
Juha Hanka

Reputation: 659

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

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

logback patternlayout docs

Upvotes: 1

amal
amal

Reputation: 3582

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

Upvotes: 16

Related Questions