Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure system properties or logback configuration variables from typesafe config?

I have variables with defaults in my logback.xml configuration file, and I would like to be able to optionally set these variables from my typesafe config application.conf file.

I am deploying the application using one-jar, and the application.conf file packaged up in the deployable jar contains defaults. I pass -Dconfig.file=foo.conf on execution to provide the path to a server-specific config file.

Right now I can also pass -Dlog.level and other variables to override my defaults in logback.xml, and I also have to pass -Dfile.encoding=UTF-8 on the command line. I'm looking for a way to be able to specify these properties in the typesafe config instead of on the command line. It feels like there should be a way to do it, but I can't find the magic words.

logback.xml:

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path:-logs/}/${log.file:-myLog.log}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

            <!-- keep 15 days' worth of history -->
            <maxHistory>${log.history.days:-15}</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="${log.level:-INFO}">
        <appender-ref ref="FILE" />
    </root>
</configuration>

application.conf (bundled):

akka {
    log-config-on-start = false
    event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
}

sample server-specific app.conf:

include "/application.conf"

akka.log-config-on-start = true

log.level = WARN // this is what I'd LIKE to be able to do

How I'm currently running the app:

java -Dfile.encoding=UTF-8 -Dconfig.file=myApp.conf -Dlog.level=WARN -jar myApp_2.10-0.1-one-jar.jar 
like image 753
Ian McMahon Avatar asked Feb 26 '13 19:02

Ian McMahon


People also ask

How do I use application properties in Logback xml?

properties file in your logback configuration. The tag works in a similar way to Logback's standard tag, but rather than specifying a direct value you specify the source of the property (from the Environment). You can use the scope attribute if you need to store the property somewhere other than in local scope.

Which of the following is the configuration file for Logback?

The Logback Configuration File. For Logback configuration through XML, Logback expects a Logback. xml or Logback-test. xml file in the classpath.


1 Answers

I chose to programmatically configure logback having typesafe config. It turned out to be easy.

def enableRemoteLogging(config: Config) = {
    val ctx = LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]

    val gelf = new GelfAppender
    gelf.setGraylog2ServerHost(config.getString("logging.remote.server"))
    gelf.setUseLoggerName(true)
    gelf.setUseThreadName(true)
    gelf.setUseMarker(true)
    gelf.setIncludeFullMDC(true)
    gelf.setContext(ctx)
    gelf.start()

    LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)
      .asInstanceOf[ch.qos.logback.classic.Logger]
      .addAppender(gelf)
  }
like image 85
Stanislav Savulchik Avatar answered Oct 13 '22 01:10

Stanislav Savulchik