[slf4j-user] separation of format and data
Jiri Pejchal
jiri.pejchal at gmail.com
Mon Aug 30 22:56:21 CEST 2010
"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
More information about the slf4j-user
mailing list