[slf4j-user] separation of format and data

Ed Bras zooi at debrasjes.com
Mon Aug 30 23:46:21 CEST 2010


> 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






> -----Original Message-----
> From: slf4j-user-bounces at qos.ch [mailto:slf4j-user-bounces at qos.ch] On
> Behalf Of Jiri Pejchal
> Sent: maandag 30 augustus 2010 22:56
> To: slf4j-user at qos.ch
> Subject: Re: [slf4j-user] separation of format and data
> 
> "Ed Bras" <zooi at debrasjes.com> writes:
> 
> > I like to have more control over the output format and data input by
> > separating these things.
> > How can I do this?
> >
> > I would like to do something like this:
> > Logger.debug("some debug data", "Member", this.member, "Address",
> > this.address);
> >
> > Then the formatter would output something like this:
> > Some debug data.
> > Member= [Member at dfsdf,  id=2434, ....]
> > Address= [Adress at dafa, street=bla, postalCode=34324234, ...]
> >
> > The text between [] is the toString output.
> > I use this in other parts, like in my asserts and toString logging
> since a
> > few years now, and like it: development friendly, that is: easy usage
> in
> > code and readably output (especially with complex data structures
> this is a
> > requirement).
> >
> > I could do the above through a static Utils method, something like
> this:
> > UtilsLogger.debug(logger, "some debug data", "Member", this.member,
> > "Address", this.address);
> > And then the Utils method will construct the complete log statement
> with the
> > {} in it.
> > But might it not an idea to make this part of the Logger Facade?.. Or
> is
> > there a way to extend SLF4j to add this myself?
> >
> > Please some advice?
> > Ed
> 
> I don't know if I understand you correctly, by I usually use something
> like:
> 
> import static com.google.common.base.Objects.toStringHelper;
> 
> public class User {
>     private Long id;
>     private String firstName;
>     private String lastName;
> 
>     @Override
>     public String toString() {
>         return toStringHelper(this).
>                 add("id", id).
>                 add("firstName", firstName).
>                 add("lastName", lastName).
>                 toString();
>     }
> }
> 
> 
> 
> 
> userService.create(user);
> logger.info("Created {}", user);
> 
> And the result is:
> 22:50:51.116 [http-8084-1] INFO  cz.asterix.beans.DataInitBean
> (DataInitBean.java:71) Created User{id=1, firstName=Jan,
> lastName=Novak}
> 
> 
> 
> Jiri Pejchal
> 
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://qos.ch/mailman/listinfo/slf4j-user



More information about the slf4j-user mailing list