Log4j - Force log message parameters
I am trying to f开发者_运维问答orce the detail within log4j messages as opposed to just a string. Ultimately I would like to have something like:
logger.info("component", "error code", " error message");
I may even use an enum for the component.
I would then expect the message to appear in the log as "time - component - code - message"
Any ideas?
A very trivial solution (for this special case):
logger.info(
String.format("%s - %s - %s", "component", "error code", "error message"));
or in an improved version:
logger.info(createMessage("component", "error code", "error message"));
with
public static String createMessage(
String component, String errorCode, String errorMessage) {
return String.format("%s - %s - %s",
"component", "error code", "error message");
}
For a more general solution, consider wrapping the original Logger class into a custom logger class and implement an info
method that takes the three arguments and delegates to internalLogger.info(String message)
with the new message based on the input.
精彩评论