Created
February 25, 2016 09:36
-
-
Save woupiestek/1ba69dbd86b2a414a4a0 to your computer and use it in GitHub Desktop.
Configure logback with HOCON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app { | |
name: logstash-hocon-gist | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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