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

Configure Logback using HOCON format (Typesafe's Config)

    XMLWordPrintable

Details

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • None
    • logback-classic

    Description

      The idea is to add a new way of configuring Logback using HOCON format (Typesafe's Config), in addition to XML and Groovy. The motivation is that for projects that use HOCON for their main configuration (e.g. application.conf) it would be very convenient just to add an additional "logback" section instead of providing a separate configuration file. Moreover, Typesafe's Config provides many features that would be beneficial also for configuring Logback (e.g. reference configuration merging).

      Below is an example of the proposed configuration syntax based on a sample XML config:

      <configuration debug="true" scan="true" scanPeriod="30 seconds">
        <contextName>myAppName</contextName>
        <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
          <resetJUL>true</resetJUL>
        </contextListener>
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
          <file>myApp.log</file>
          <append>true</append>
          <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
          </encoder>
        </appender>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
          <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
          </encoder>
        </appender>
        <logger name="chapters.configuration" level="INFO">
        <root level="DEBUG">
          <appender-ref ref="FILE" />
          <appender-ref ref="STDOUT" />
        </root>
      </configuration>
      

      HOCON equivalent (e.g application.conf):

      logback {
        debug = true
        scan = true
        scan-period = 30 seconds
        context-name = "myAppName"
        context-listener {
          class = "ch.qos.logback.classic.jul.LevelChangePropagator"
          reset-jul = true
        }
        appenders {
          "FILE" {
            class = "ch.qos.logback.core.FileAppender"
            file = "myApp.log"
            append = true
            encoder {
              pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
            }  
          }
          "STDOUT" {
            class = "ch.qos.logback.core.ConsoleAppender"
            encoder {
              pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
            }  
          }
        }
        loggers {
          "chapters.configuration" {
            level = INFO
          }  
        }
        root {
          level = DEBUG
          appender-refs = ["FILE", "STDOUT"]  
        }
      }
      

      Standard XML config's features not directly supported:

      • Conditional processing of a configuration file.
      • Properties and variable substitution (but supported to some extend by HOCON itself).
      • File inclusion (but supported by HOCON itself).

      I have a PoC version of the above that I would like to share. Please let me know what do you think.

      Attachments

        Activity

          People

            logback-dev Logback dev list
            mwegrz Michał Węgrzyn
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: