I once pursued similar goals for a java based trace/log mechanism - i.e. to look clean
and also to be efficient and also allow the possibility to 'compile it out' of the
code completely (rather like one could do with C/C++ macro based mechanisms).
The best I could come up with was to make the trace/logger calls return a boolean so
that I could write them as part of an '&&' expression, then I had a dummy static
method that took a boolean type as an argument so I could write lines like:
TRACE.MACRO(DTR && log.debug(...)); // diagnostic trace point that will be
compiled out of
// production code
or
TRACE.MACRO(log.isDebugEnabled() && log.debug(...)); // low cost production
trace point
where TRACE.MACRO was the dummy static method that took a boolean as an argument and
DTR was a globally defined boolean constant defined in an interface that everything
implemented. During development DTR==true so the diagnostic trace point was always
called. For a production build the source code that initialized DTR was mechanically
altered to be false and as TRACE.MACRO was an empty function the combined effect of
compiler optimization is to remove all 'trace' of the code in the class file.
To get the same effect with log4j one would have to create a wrapper for the logger
that changed the return types of the logger methods, but many would say that my
concerns with run-time effeciency and object file memory occupancy simply give away my
age... 8^).
> -----Original Message-----
> From: Ashish Jain [mailto:[EMAIL PROTECTED]]
> Sent: 11 February 2003 18:46
> To: Log4J Users List; [EMAIL PROTECTED]
> Subject: Re: IsDebugEnabled
>
>
>
> Fergus,
> Help me understand more of what you are doing ? How/Where do
> you get the handler to the logger (Logger.getLogger(....))
> to be able to control the level at run time. Do you call your
> own debug method from inside your code ?
> Thks,
> - Ashish.
> Fergus Gallagher <[EMAIL PROTECTED]> wrote:Yes,
> this a hard one.
>
> One option we've used is to wrap MessageFormat
>
> debug("Hello {1}", obj)
>
> with cases for 1..n args and a catch-all
>
> debug("Hello {1}.....", new Object[] {a,b,.....});
>
> with
>
> void debug(String fmt, Object arg) {
> if (LOG.isDebugEnabled()) {
> LOG.debug(MessageFormat.format(fmt, new Object[] {arg});
> }
> }
>
> etc.
>
> However, this doesn't cope with "expensive" method calls:
>
> debug("", obj.someSlowMethod());
>
> In this case it's probably best to wrap with isDebugEnabled()
> yourself. A
> (messy) alternative is to use introspection:
>
> debug(fmt, obj, "someSlowMethod");
>
> so that obj.someSlowMethod is only evaluated if really required. Yuck.
>
>
>
> On Tue, Feb 11, 2003 at 12:33:07PM -0500, John Guthrie wrote:
> > i think the rule is not to check *every time*, but only if
> the statement you
> > are sending to the debugger is expensive to create. our
> rule of thumb is to
> > check if the message requires more than one string
> concatenation. so for
> > this:
> > logger.debug("look out!");
> > or this:
> > logger.debug("look "+"out!");
> > there is no need to check. but beyond that, you should,
> because otherwise
> > you could be wasting time creating messages that don't get logged.
> >
> > john
> >
> > ----- Original Message -----
> > From: "Ashish Jain"
> > To:
> > Sent: Tuesday, February 11, 2003 12:26 PM
> > Subject: IsDebugEnabled
> >
> >
> > >
> > > All,
> > >
> > > I am looking at the log4j documentation and it states the
> following
> > >
> > > "To avoid the parameter construction cost write:
> > >
> > > if(logger.isDebugEnabled() {
> > > logger.debug("Entry number: " + i + " is " +
> > > String.valueOf(entry[i]));
> > > }"
> > >
> > > I do want to avoid the parameter construction cost, but I
> don't want to
> > > check for IsXXXEnabled everytime I use a log statement
> because it makes
> > > my code clumsy. Is there a better way of doing this ? Is
> there a config
> > > flag somewhere ? or should I extend the logger classes
> and check for
> > > IsXXXenabled before calling the Log4j classes. And then
> my code can call
> > > my log4jextended classes. Again, I don't want to check
> for IsXXXEnabled
> > > every time I use a log statement.
> > >
> > > Any help is appreciated.
> > >
> > > Thks,
> > >
> > > - Ashish.
> > >
> > >
> > >
> > > ---------------------------------
> > > Do you Yahoo!?
> > > Yahoo! Shopping - Send Flowers for Valentine's Day
> > >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
> --
> Fergus Gallagher Tel: +44 (20) 8742 1600
> Orbis Fax: +44 (20) 8742 2649
> 414 Chiswick High Street email: [EMAIL PROTECTED]
> London W4 5TL Web: http://www.orbisuk.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Shopping - Send Flowers for Valentine's Day
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]