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

fi.evolver.basics.spring.log.LogUtils Maven / Gradle / Ivy

There is a newer version: 6.5.1
Show newest version
package fi.evolver.basics.spring.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public class LogUtils {
	private static final Logger LOG = LoggerFactory.getLogger(LogUtils.class);

	private static final Specifier NOP_SPECIFIER = new Specifier() { @Override public void close() { /* NOP */ } };

	public static final String SPECIFIER = "Specifier";


	private LogUtils() { }


	public static Specifier startSpecifier(String specifier) {
		if (getSpecifier() != null)
			return NOP_SPECIFIER;

		LOG.trace("{}: START Specifier", Thread.currentThread().getId());
		setSpecifier(specifier);
		return new Specifier();
	}


	public static String getSpecifier() {
		return MDC.get(SPECIFIER);
	}


	private static void setSpecifier(String specifier) {
		if (LOG.isTraceEnabled())
			LOG.trace("{}: Specifier {} => {}", Thread.currentThread().getId(), getSpecifier(), specifier);

		if (specifier == null)
			MDC.remove(SPECIFIER);
		else
			MDC.put(SPECIFIER, specifier);
	}


	public static class Specifier implements AutoCloseable {

		@Override
		public void close() {
			setSpecifier(null);
			LOG.trace("{}: CLOSE Specifier", Thread.currentThread().getId());
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy