[slf4j-user] logging common data simply

Shanholtz, Jeff jeff.shanholtz at intel.com
Tue Feb 28 21:42:53 CET 2017


Not a bad suggestion. I'll give that some thought but I think I like it! Thank you!

-----Original Message-----
From: slf4j-user [mailto:slf4j-user-bounces at qos.ch] On Behalf Of Joachim Durchholz
Sent: Tuesday, February 28, 2017 12:22 PM
To: slf4j-user at qos.ch
Subject: Re: [slf4j-user] logging common data simply

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.


_______________________________________________
slf4j-user mailing list
slf4j-user at qos.ch
http://mailman.qos.ch/mailman/listinfo/slf4j-user


More information about the slf4j-user mailing list