patternjavaMinor
Any recommendations to improve this logging framework? Or is it overkill?
Viewed 0 times
thislogginganyoverkillimproveframeworkrecommendations
Problem
So I've been kind of irritated with my logging recently as I find myself copying and pasting the same kinds of generic messages around to lots of different methods or retyping them ever so slightly differently (when they ought to be identical).
Hence, I created this kind of framework:
It's then invoked like the following:
```
private static final String FILE_PATH = "resources/something.txt";
//...
LOGGER.error(Log.Mes
Hence, I created this kind of framework:
public class Log {
// other logging utility methods truncated
public enum Message {
//Main
PROGRAM_EXIT ("Exiting program, return code = {0}"),
THREAD_INTERRUPTED ("Thread interrupted while {0}"),
FATAL_TERMINATING_ERROR ("Fatal exception thrown through entire stack: {0}"),
SHUTDOWN_HOOK_EXCEPTION ("Exception while attempting to gracefully exit: {0}"),
IO_READ_ATTEMPT ("Attempting to read file: {0}"),
IO_READ_FAILURE ("Unable to read from file: {0}"),
IO_WRITE_ATTEMPT ("Attempting to write to file: {0}"),
IO_WRITE_FAILURE ("Unable to write to file: {0}"),
IOEXCEPTION ("IOException while {0} to {1} : {2}"),
private final String text;
private Message(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
public String format(Object ... args) {
String returnValue = toString();
for(int i = 0; i < args.length; i++) {
String placeholder = "{" + i + "}";
if(!returnValue.contains(placeholder)) {
LOGGER.error("Too many arguments supplied to generateMessage() from " + getCallingMethodName());
LOGGER.error(" -- " + name());
break;
}
returnValue = returnValue.replace("{" + i + "}", args[i] == null ? "null" : args[i].toString());
}
return returnValue;
}
}
}It's then invoked like the following:
```
private static final String FILE_PATH = "resources/something.txt";
//...
LOGGER.error(Log.Mes
Solution
You don't need to re-implement
You don't show us the class of
If you are using a logging library, you are abstracting to much away from the library. All of the frameworks I've used support message formating and detailed exception logging (with stack traces).
The problem you are really trying to solve here is to allow yourself to reuse a number of common log message formats. This can be done with a simple set of Strings set to
MessageFormat. It is much more powerful than what your implementation provides.You don't show us the class of
LOGGER, but based on the fact that you have a Log class, I suspect you are not using a logging library. I highly recommend you look into using one instead of implementing your on custom implementation. If you are using a logging library, you are abstracting to much away from the library. All of the frameworks I've used support message formating and detailed exception logging (with stack traces).
The problem you are really trying to solve here is to allow yourself to reuse a number of common log message formats. This can be done with a simple set of Strings set to
public static final.Context
StackExchange Code Review Q#40738, answer score: 3
Revisions (0)
No revisions yet.