[slf4j-user] separation of format and data

Ed Bras zooi at debrasjes.com
Thu Sep 2 21:18:21 CEST 2010


Hi Ceki,

Could you please give me some advice on the topic below?

Ed

> -----Original Message-----
> From: slf4j-user-bounces at qos.ch [mailto:slf4j-user-bounces at qos.ch] On
> Behalf Of Ed Bras
> Sent: woensdag 1 september 2010 11:29
> To: 'User list for the slf4j project'
> Subject: Re: [slf4j-user] separation of format and data
> 
> Anybody any advice on the topic below?...
> 
> I did some further investigation and came up with the following idea to
> be
> able to create my own logger and as such be able to add extra
> functionality
> like this separation of format and data:
> 1) In my class I put:
> 	private static final MyLogger LOG =
> MyLoggerFactory.getLogger(InternetAccountDefault.class);
> 
> The difference:
> MyLogger: my logger containing specialized functionality. This class
> must
> extends the original Logger class.
> MyLoggerFactory: my logger factory that must extends the original
> LoggerFactory class.
> 
> 2)The original LoggerFactory contains generics and the actual creation
> of
> the Logger class is done through subclasses like the MyLoggerFactory.
> 
> Impact: the current LoggerFactory should contain generics and must be
> abstract like this:
>   public abstract class <T extends Logger> LoggerFactory<T> {
> 
> All occurrences of the class Logger in LoggerFactory should be changed
> by T.
> It must contain the following abstract method to create the Logger:
>   protected abstract T createLogger();
> 
> I think that's about it...
> So my above classes would be:
>  public final class MyLogger extends Logger {
> 
> And:
>   public final class MyLoggerFactory extends LoggerFactory<MyLogger> {
>     @Override
>     protected T createLogger() {
>       ....
>     }
>  }
> 
> Note: It probably has some more impact to make sure it still deals
> correctly
> with Logback, log4j, etc...
> 
> Please feedback on this, before I start playing with it?
> 
> Ed
> 
> 
> 
> 
> > -----Original Message-----
> > From: slf4j-user-bounces at qos.ch [mailto:slf4j-user-bounces at qos.ch] On
> > Behalf Of Ed Bras
> > Sent: dinsdag 31 augustus 2010 9:35
> > To: 'User list for the slf4j project'
> > Subject: Re: [slf4j-user] separation of format and data
> >
> > > 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();
> > > }
> >
> > Thanks, I am familiar with these methods/constructions, used them
> > myself
> > till I made my own (such that I can change it during compile time
> with
> > GWT).
> > Buttttt anyway: this isn't really what I want.
> > It would be nice if I could extend the Logger class in someway such
> > that I
> > can add my own facade methods like the ones explained below.
> > Ed
> >
> >
> > > -----Original Message-----
> > > From: slf4j-user-bounces at qos.ch [mailto:slf4j-user-bounces at qos.ch]
> On
> > > Behalf Of Jiri Pejchal
> > > Sent: dinsdag 31 augustus 2010 0:46
> > > To: slf4j-user at qos.ch
> > > Subject: Re: [slf4j-user] separation of format and data
> > >
> > > "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
> > >
> > > _______________________________________________
> > > slf4j-user mailing list
> > > slf4j-user at qos.ch
> > > http://qos.ch/mailman/listinfo/slf4j-user
> >
> > _______________________________________________
> > slf4j-user mailing list
> > slf4j-user at qos.ch
> > http://qos.ch/mailman/listinfo/slf4j-user
> 
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://qos.ch/mailman/listinfo/slf4j-user



More information about the slf4j-user mailing list