All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.groupbyinc.common.util.logging.LoggingContext Maven / Gradle / Ivy

There is a newer version: 198
Show newest version
package com.groupbyinc.common.util.logging;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/**
 * Logging context
 *
 * @author Ben Teichman
 */
public class LoggingContext {

  private static final transient Logger LOG = LoggerFactory.getLogger(LoggingContext.class);
  private static final String UNKNOWN = "";

  public enum Key {
    CustomerId("customerId");

    String value;

    Key(String value) {
      this.value = value;
    }

    public String getValue() {
      return value;
    }
  }

  public static void init() {
    // set all values to unknown
    LOG.trace("initializing all values as '{}'", UNKNOWN);
    for (Key key : Key.values()) {
      MDC.put(key.getValue(), UNKNOWN);
    }
  }

  public static void set(Key key, String value) {
    String keyValue = key.getValue();
    if (StringUtils.isNotBlank(value)) {
      LOG.trace("setting '{}' to '{}'", keyValue, value);
      MDC.put(keyValue, value);
    } else {
      LOG.trace("'{}' must not be blank", keyValue);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy