Logback AyncAppender not printing File and Line number

﹥>﹥吖頭↗ 提交于 2019-12-18 11:54:08

问题


I have the following configuration file that is very similar to the standard example in the Logback manual. The only difference is the addition of [%F:%L]. while everything works, %F and %L do not work. If I remove the async appender and log directly using the file appender, everything works just great.

can somebody explain what is going on? And how to print the file name and line number as these two parameters are supposed to?

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>myapp.log</file>
    <encoder><pattern>%logger{35} - [%F:%L] - %msg%n</pattern></encoder>
    </appender>
    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
         <appender-ref ref="FILE" />
    </appender>
   <root level="DEBUG"><appender-ref ref="ASYNC" /></root>
 </configuration>

回答1:


You need to set AsyncAppender's includeCallerData property to true. Here is the modified config file:

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
      <file>myapp.log</file>
      <encoder><pattern>%logger{35} - [%F:%L] - %msg%n</pattern></encoder>
    </appender>

    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
      <appender-ref ref="FILE" />
      <!-- add the following line -->
      <includeCallerData>true</includeCallerData>
    </appender>

    <root level="DEBUG"><appender-ref ref="ASYNC" /></root>
 </configuration>



回答2:


I post same answer in groovy format for someone who want groovy style like me.

appender('FILE', ch.qos.logback.core.FileAppender) {
    file = 'myapp.log'
    encoder(PatternLayoutEncoder) {
        pattern = '%logger{35} - [%F:%L] - %msg%n'
    }
}
appender('ASYNC', ch.qos.logback.classic.AsyncAppender) {
    appenderRef('FILE')
    //add the following line
    includeCallerData = true
}

root(DEBUG, ['ASYNC'])


来源:https://stackoverflow.com/questions/13944641/logback-ayncappender-not-printing-file-and-line-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!