[slf4j-user] separation of format and data

Jiri Pejchal jiri.pejchal at gmail.com
Tue Aug 31 00:46:28 CEST 2010


"Ed Bras" <zooi at debrasjes.com> writes:

>> userService.create(user);
>> logger.info("Created {}", user);
>
> Thanks, and yes that is what I also do, but I want more control.
> For a single message I would do something like this:
>    logger.info("Created", "User", user);
> or, to use the fluent api like in your example:
>   logger.info("Created").add("User", user);
> BTW: this latter case might not such a good idea as I think it's not good
> for performance, as in case it concerns a debug call with debug disabled,
> all the add() calls always will be executed...
>
> Anyway, your example contains both data and the output format which is
> created through the {} combination...
> I want to extract the "format information" into the "log configuration"
> (e.g.: logback.xml).
> Such that in my configuration I can specify that the output has to look like
> for example:
> ---
> Message: Created
> Data:
> User = [User at dadfafd, .... etc..]
> ---
>
> Or you just want a single line logging:
> ---
> Created, User = [User at dadfafd, .... etc..]
> ---
>
> The latter case is nice for simple logging, but in case of logging complex
> Value objects, this is too simple, as the lines become too large and
> unreadable...
> Conclusion: more control.
>
> Besides the simple example you point out, I am more thinking of examples
> that contain more data, something like this:
>    logger.debug("Created.\nUser:{}\nAddress:{}\nHistory:{}", user,
> currenAddress, addressHistory);
> Note how the format is included in the message and doesn't look so nice...
> It would look better if you would have something like:
>    logger.debug("Created., "User", user, "Address", currentAddress,
> "History:", addressHistory);
> Or:
>    logger.debug("Created., user, currentAddress, addressHistory);
> Basically the call would be something like this:
> 	debug(String message, String description1, Object obj1, String
> description2, Object obj2, String description3, Object obj3)
> or
> 	debug(String message, Object obj1, Object obj2, Object obj3)
>
> I hope I made it a bit more clear through the above examples?
> Of course, in most cases "simple logging" is sufficient, however, in the
> complex projects I am in the last few year, I really need this
> functionality.
> Please some advice how to realize this ?
> I was thinking about extending the current Logger and returning it from the
> Factory, but haven't looked in to it if this is a good idea... just a
> thought....
>
> Ed

Hm,

I don't know how to do it with logback. Personally I would use something
similar to:

public static String toLogString(Object... arguments){
        StringBuilder str = new StringBuilder();
        for (Object object : arguments) {
            str.append(object.toString()).append('\n');
        }
        return str.toString();
}
    
Or apache.commons.lang has
ToStringBuilder(Object object, ToStringStyle style)
where you can specify your own ToStringStyle
http://people.apache.org/~bayard/commons-lang-3.0-snapshot-api/org/apache/commons/lang/builder/ToStringStyle.html

Jiri Pejchal



More information about the slf4j-user mailing list