[slf4j-dev] [Bug 135] New: A default baseclass implementation of a delayed toString evaluator should be available in slf4j

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Mon Jun 8 14:02:37 CEST 2009


http://bugzilla.slf4j.org/show_bug.cgi?id=135

           Summary: A default baseclass implementation of a delayed toString
                    evaluator should be available in slf4j
           Product: SLF4J
           Version: 1.5.x
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: Core API
        AssignedTo: dev at slf4j.org
        ReportedBy: thorbjoern at gmail.com


I believe there is a need for a base class for wrapping any object in order to
be able to add a string renderer (replacing the toString()) which is evaluated
by the logging platform when it feels like it, instead of having to do it in
the log.whatever("foo bar {}"...) call.

In the email I quote below I named it ToString which I believe is inappropriate
as it refers to the implementation instead of the intent.

Perhaps a name of "Loggable" with an overridable method called "render(Object
o)" which is then called by the final toString() of Loggable.

The user could then extend loggable either as a class or directly with 

new Loggable(bigVariable) {
   public render(Object o) {
      return ">" + o.toString() + "<";
   }
}

I do not believe this should have any further notice in slf4j than providing
the baseclass with a default implementation of render(o) returning
"o.toString()".

this will out of the box allow

log.info("{}", new Loggable(foo));




===
Basically you need to delay the String rendering of the integer argument until
AFTER the decision to log the object has been made.   The simplest way to do so
is to create a wrapper class doing what you need to do in its toString()
method, and then wrap your object in an instance in your log statement.  The
wrappers toString method is then invoked by the logger framework, and there you
can put your string rendering.

E.g.

 log.debug("big={}", new ToString(bigObject));

where ToString looks like:


public class ToString {

   Object o;

   public ToString(Object o) {
       this.o = o;
   }
     public String toString() {
       return o.toString();
   }
  }
=====


-- 
Configure bugmail: http://bugzilla.slf4j.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.



More information about the slf4j-dev mailing list