
fi.evolver.utils.attribute.Slf4jMdcAttribute Maven / Gradle / Ivy
package fi.evolver.utils.attribute;
import fi.evolver.utils.ContextUtils;
import org.slf4j.MDC;
import java.util.Optional;
/**
* A helper class for setting and accessing Slf4j MDC attributes.
*
* Note! the attribute life cycle and thread migration are handled by the current context.
*/
public class Slf4jMdcAttribute implements ContextRegistrableAttribute {
private final String name;
public Slf4jMdcAttribute(String name) {
this.name = name;
}
@Override
public String name() {
return name;
}
@Override
public Optional get() {
return Optional.ofNullable(MDC.get(name()));
}
@Override
public void set(String value) {
if (value != null) {
MDC.put(name(), value);
ContextUtils.register(this);
}
else {
MDC.remove(name());
}
}
@Override
public String toString() {
return name;
}
@Override
public Class type() {
return String.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy