Package io.bdeploy.common.util
Class MdcLogger
java.lang.Object
io.bdeploy.common.util.MdcLogger
Utility class that simplifies writing log statements with MDC data.
The logger is typically initialized with a fixed MDC value that is attached to all log calls.
If required additional data can be passed to each log call.
public class MyClass {
private final MdcLogger logger = new MdcLogger(MyClass.class);
public MyClass() {
logger.setMdc("green");
}
public void foo() {
logger.log((l) -> l.info("Doing some work."));
logger.log((l) -> l.info("Doing more work."),"red");
}
}
The following output will be printed when a pattern layout is used:
%-12.12MDC{BDEPLOY} | %-5level | %-20msg - (%F)%n
green | INFO | Doing some work. - (MyClass.java)
green / red | INFO | Doing more work. - (MyClass.java)
The actual log call is delegated to a functional interface so that the line numbers and the class is preserved in the log statements. As consequence it is also up the the caller to check if the desired log level is enabled.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidWrites the desired log statement.voidWrites the desired log statement.voidsetMdcValue(Object... value) Sets the MDC value that will be associated with all subsequent log calls.
-
Field Details
-
MDC_NAME
The key to which all MDC data is associated with.- See Also:
-
-
Constructor Details
-
MdcLogger
Creates a new instance using the given logger.- Parameters:
clazz- the class used by the logger
-
-
Method Details
-
setMdcValue
Sets the MDC value that will be associated with all subsequent log calls.- Parameters:
value- the value to associate with the MDC key.
-
log
Writes the desired log statement. The previously configured MDC value will be set during the logger is called.- Parameters:
writer- the logger to be used to write the log statement
-
log
Writes the desired log statement. The given MDC data is added to the already defined MDC data. Typically used when there is a base MDC value that should be present in all statements but in some additional values are required.- Parameters:
writer- logger to be used to write the log statementmdcData- data to associate with the MDC key
-