[slf4j-user] logging common data simply
Joachim Durchholz
jo at durchholz.org
Tue Feb 28 21:21:40 CET 2017
On 27.02.2017 17:31, Shanholtz, Jeff wrote:
> You ask why is a wrapper better than just plain old logging? It is *marginally* better in that it forces a consistent log message format (important for importing into our ELK stack) and also serves as a reminder to the programmer to include the guids whenever possible.
In that case, you can use a wrapper class like this:
public class GuidForLog {
private GUID guid1;
private GUID guid2;
private GUID guid3;
public GuidForLog(GUID guid1, GUID guid2, GUID guid3) {
this.guid1 = guid1;
this.guid2 = guid2;
this.guid3 = guid3;
}
public GuidForLog(GUID guid1, GUID guid2) {
this.guid1 = guid1;
this.guid2 = guid2;
}
public GuidForLog(GUID guid1) {
this.guid1 = guid1;
}
public String toString() {
StringBuilder sb := new StringBuilder();
sb.append("[");
sb.append(guid1);
if (guid2 != null) {
sb.append(",");
sb.append(guid2);
}
if (guid3 != null) {
sb.append(",");
sb.append(guid3);
}
sb.append("]");
}
}
Programmers can then use it like this:
logger.info("GUID {}: foo blah blarb", new GuidForLog(guid1, guid2));
It does not make programmers think about adding the GUIDs, but it does
make sure there's a common format.
If GUID is something other than String, then this also avoids
unnecessary toString calls.
Option B:
If the number of GUIDs is known in advance for each call site, then
logger.info("GUID [{},{}]: foo blah blarb", guid1, guid2);
is still the easiest option.
More information about the slf4j-user
mailing list