Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-873

Construct message in LoggingEvent lazily

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • 1.1.0
    • 1.0.13
    • logback-classic
    • None

    Description

      Profiling our application with following logback configuration showed that message in LoggingEvent is constructed even if it will not be logged. Is it possible to make it computed lazily?

      Excerpt from out logback.xml:

      <appender name="DEBUG"
      	class="ch.qos.logback.core.rolling.RollingFileAppender">
      	<File>debug.log</File>
      	<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      		<FileNamePattern><![CDATA[debug.%i.log]]></FileNamePattern>
      		<minIndex>1</minIndex>
      		<maxIndex>5</maxIndex>
      	</rollingPolicy>
      
      	<triggeringPolicy
      		class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      		<maxFileSize>30MB</maxFileSize>
      	</triggeringPolicy>
      
      	<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      		<level>DEBUG</level>
      	</filter>
      
      	<encoder>
      		<Pattern><![CDATA[%date{YYYYMMdd HH:mm:ss.SSS} [%thread][%-5level][%X{tenantId}] %-50logger{50}:%line - %message%n]]></Pattern>
      		<charset>UTF-8</charset>
      		<immediateFlush>false</immediateFlush>
      	</encoder>
      </appender>
      
      <appender name="TRACE"
      	class="ch.qos.logback.core.rolling.RollingFileAppender">
      	<File>trace.log</File>
      	<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      		<FileNamePattern><![CDATA[trace.%i.log]]></FileNamePattern>
      		<minIndex>1</minIndex>
      		<maxIndex>5</maxIndex>
      	</rollingPolicy>
      
      	<triggeringPolicy
      		class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      		<maxFileSize>30MB</maxFileSize>
      	</triggeringPolicy>
      
      	<encoder>
      		<Pattern><![CDATA[%date{YYYYMMdd HH:mm:ss.SSS} [%thread][%-5level][%X{tenantId}] %-50logger{50}:%line - %message%n]]></Pattern>
      		<charset>UTF-8</charset>
      		<immediateFlush>false</immediateFlush>
      	</encoder>
      </appender>
      
      <logger name="com.our_package">
      	<!-- TRACE does not mean that it will be printed.
      	we just not filter it on logger level,
      	but messages will filtered later on appenders level -->
      	<level value="trace" /> 
      </logger>
      
      <root>
      	<level value="info" />
      	<appender-ref ref="MAIN" />
      	<appender-ref ref="ERROR" />
      <!-- debug log not enabled by default. Uncomment if needed. Use it wisely! -->
      	<!--appender-ref ref="DEBUG" /-->
      <!-- trace log not enabled by default. Uncomment if needed. Use it wisely! -->
      	<!--appender-ref ref="TRACE" /-->
      </root>
      

      What is happening: Logger is set to trace so it passes everything, event is formatted, event is discarded by ThresholdFilter.

      The idea for this configuration is to have several log files with different detalization. I think this is a valid usecase.

      Attachments

        Activity

          People

            logback-dev Logback dev list
            ash2k Mikhail Mazursky
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: