[slf4j-user] logging common data simply

Shanholtz, Jeff jeff.shanholtz at intel.com
Sat Feb 25 20:34:26 CET 2017


I have a scenario where I have 1, 2, or 3 pieces of data (guids) which I want to to log with all messages in order to track records as they traverse their way through a series of microservices.

Currently I've written my own wrapper class with my own debug, info, warn, error methods that take these 3 parameters, followed by the message field, followed by the optional parameters.

There must be a better way, plus this has the disadvantage of rendering the line number useless in log messages because the line numbers are the line numbers from my log wrapper class, not the actual code doing the logging.

What is the best way to handle this scenario? Googling has failed me, as has the FAQ, but it's probably because I don't know the right terms to search for. I'm using log4j2 as the underlying logger FWIW.

Here's a simplified version of my log wrapper (i.e. most methods stripped out because they are redundant for the purpose of this question):

public class Log {
    private Logger logger;

    public Log(Class clazz) {
        logger = LoggerFactory.getLogger(clazz);
    }

    public void info(Object xGuid, Object yGuid, String zGuid, String message, Object... args) {
        logger.info(guidifiedMessage(xGuid, yGuid, zGuid, message), args);
    }
}

The "guidifiedMessage" method just prefixes the message string with whatever guids are present.

So is there a better way? Or if not, is there at least a way to get the line number specified in my log4j properties to reflect the caller of this Log class rather than the line within the Log class?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/slf4j-user/attachments/20170225/de46f1fa/attachment.html>


More information about the slf4j-user mailing list