Skip to content

Instantly share code, notes, and snippets.

@woupiestek
Created February 25, 2016 09:36
Show Gist options
  • Save woupiestek/1ba69dbd86b2a414a4a0 to your computer and use it in GitHub Desktop.
Save woupiestek/1ba69dbd86b2a414a4a0 to your computer and use it in GitHub Desktop.
Configure logback with HOCON
app {
name: logstash-hocon-gist
}
<configuration>
<!--read properties from application.conf-->
<newRule pattern="*/load" actionClass="mypackage.LoadFromApplicationConf"/>
<load key="app.name" as="APP_NAME"/>
<appender name="application" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/${APP_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/${APP_NAME}.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{MM/dd HH:mm:ss} %-5level[%thread] %logger{1} - %msg%n</pattern>
</encoder>
</appender>
<root level="warn">
<appender-ref ref="application"/>
</root>
</configuration>
package mypackage
import ch.qos.logback.core.joran.action.Action
import ch.qos.logback.core.joran.spi.InterpretationContext
import com.typesafe.config.ConfigFactory
import org.xml.sax.Attributes
/**
* Make properties defined in application.conf available to logback
*/
class LoadFromApplicationConf extends Action {
val config = ConfigFactory.load
override def begin(ic: InterpretationContext, body: String, attributes: Attributes): Unit = {
ic.addSubstitutionProperty(attributes.getValue("as"), config.getString(attributes.getValue("key")))
}
override def end(ic: InterpretationContext, body: String): Unit = ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment