planetiler/planetiler-core/src/main/java/com/onthegomap/planetiler/util/LogUtil.java

39 wiersze
1.0 KiB
Java
Czysty Zwykły widok Historia

package com.onthegomap.planetiler.util;
2021-08-10 10:55:30 +00:00
import java.util.regex.Pattern;
import org.slf4j.MDC;
2021-09-10 00:46:20 +00:00
/**
* Wrapper for SLF4j {@link MDC} log utility to prepend {@code [stage]} to log output.
*/
2021-08-10 10:55:30 +00:00
public class LogUtil {
private LogUtil() {}
2021-09-10 00:46:20 +00:00
private static final String STAGE_KEY = "stage";
/** Prepends {@code [stage]} to all subsequent logs from this thread. */
2021-08-10 10:55:30 +00:00
public static void setStage(String stage) {
2021-09-10 00:46:20 +00:00
MDC.put(STAGE_KEY, stage);
2021-08-10 10:55:30 +00:00
}
2021-09-10 00:46:20 +00:00
/** Removes {@code [stage]} from subsequent logs from this thread. */
2021-08-10 10:55:30 +00:00
public static void clearStage() {
2021-09-10 00:46:20 +00:00
MDC.remove(STAGE_KEY);
2021-08-10 10:55:30 +00:00
}
2021-09-10 00:46:20 +00:00
/** Returns the current {@code [stage]} value prepended to log for this thread. */
2021-08-10 10:55:30 +00:00
public static String getStage() {
2021-09-10 00:46:20 +00:00
return MDC.get(STAGE_KEY);
2021-08-10 10:55:30 +00:00
}
2021-09-10 00:46:20 +00:00
/** Prepends {@code [parent:child]} to all subsequent logs from this thread. */
public static void setStage(String parent, String child) {
2021-08-10 10:55:30 +00:00
if (parent == null) {
2021-09-10 00:46:20 +00:00
setStage(child);
2021-08-10 10:55:30 +00:00
} else {
2021-09-10 00:46:20 +00:00
setStage(parent + ":" + child.replaceFirst("^" + Pattern.quote(parent) + "_?", ""));
2021-08-10 10:55:30 +00:00
}
}
}