[logback-user] Multiple MDC variable substitution

cdub23 chriswong20 at gmail.com
Thu Dec 30 22:41:24 CET 2010


I have been trying to pass multiple values in the MDC.  I want to use the
values to create unique paths and log file names.  I'm unable to use more
than one value that is passed to the MDC.  How would I go about doing this?  

I have also tried using SiftingAppender but I was not able figure out how to
use more than one discriminator.

Is there a way to store the value from the pattern %X{VAR}?


MDC.put("jobName", jobName);
MDC.put("station", station);

//logback.groovy file
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.PatternLayout
import ch.qos.logback.classic.sift.GSiftingAppender
import ch.qos.logback.classic.sift.MDCBasedDiscriminator

import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.FileAppender
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy
import ch.qos.logback.core.spi.LifeCycle

import static ch.qos.logback.classic.Level.DEBUG
import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.classic.Level.OFF

appender("FILE", FileAppender) {

  file = "logs/${station}/${jobName}.log
  append = true
  encoder( PatternLayoutEncoder ) {
        pattern = "[%date] %-5level: %msg [%file:%line] %n"

//Only 1 VAR will contain a value.
appender("SIFT", GSiftingAppender) {
  discriminator(MDCBasedDiscriminator) {
    key = "jobName"
    defaultValue = "unknown"
 discriminator(MDCBasedDiscriminator) {
    key = "station"
    defaultValue = "unknown"
  sift {
    appender("FILE-${jobName}", FileAppender) {
      file = "logs/${station}/${jobName}.log"
      append = false
      layout(PatternLayout) {
        pattern = "%d [%thread] %level %mdc %logger{35} - %msg%n"

View this message in context: http://old.nabble.com/Multiple-MDC-variable-substitution-tp30560208p30560208.html
Sent from the Logback User mailing list archive at Nabble.com.

More information about the Logback-user mailing list