logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Watts <...@cliftonfarm.org>
Subject Re: adding lower level debug statements for context-information
Date Wed, 25 Aug 2010 19:03:55 GMT
The NDC class might give you what you want -- or close to it. You could
(ab)use it to accumulate context data that gets dumped when a msg is
logged. Could get unwieldy fast however. And confusing if you don't
manage it properly.

Your "just in time" logging idea would likely have a significant
performance cost associated with it. Sounds like you want a logger that,
below the effective level, will do everything except actually write the
message. That could be a good chunk of the cost. Since the formatting is
done in the appenders it seems to me you'd want an appender (or a
decorator on the appender) that would write to a fixed queue which could
then be flushed on demand to the actual stream.

I would not want to use this on a production site unless I could
dynamically enable/disable it (i.e. without having to restart the app).

In my experience, having good context data in the exceptions (all nested
exceptions) and analyzing the stack trace almost always is enough to
isolate the problem. Unless there's threading issues. Defensive coding
helps a lot too: e.g. checking for null/invalid parameters and throwing
an exception with *helpful* context.


On Wed, 2010-08-25 at 14:00 +0000, Carlos Freund wrote:
> 
> I agree the example doesn't really bring the point across. In the real
> world there would be much more debug statements and the LOG.error call
> would be much higher in the stack than the place where the error
> occurs. 
> 
> Are more typical example might simply be a NullPointerException that
> is logged in some outer try-catch-block. 
> You know the line, where it happened, but you don't know how the
> variable got to be null in the first place. 
> 
> > Maybe your example is too simplistic
> > but,  I do not see why you
> > could not do this for your error log statement:
> > 
> >     LOG.error("Could not calculate average age
> > for persons: " +persons);
> > 
> > -Ryan
> > 
> > >
> > > On 25 August 2010 14:08, Carlos Freund <karlsfriend@yahoo.de>
> > wrote:
> > >
> > > > Hello,
> > > >
> > > > I've been using log4j for some time now and it's
> > working really fine.
> > > > However i have trouble with setting the
> > log-levels right.
> > > >
> > > > I am working on a server that gets quite some
> > load.
> > > > At first I set the log-level to debug. This was
> > very convenient for, well,
> > > > debugging. When the server got heavier load this
> > got impractical because the
> > > > log files would inflate and IO caused by logging
> > became noticeable.
> > > >
> > > > So i set the log level to warn. That solved those
> > problems. But now when
> > > > the occasional error occurs it has become much
> > harder to figure out why.
> > > > Before I had the debug statements from which I
> > could tell, what was wrong.
> > > > Now i just have a Exception and a stacktrace,
> > which sometimes just isn't
> > > > enough information.
> > > >
> > > >
> > > > So what I'd really like would be a compromise of
> > these two options. I was
> > > > thinking about a possibility to configure a
> > logger so it would add the last
> > > > X log
> > > > messages of lower levels whenever an log of the
> > "regular" level would be
> > > > written.
> > > >
> > > > A Simple example:
> > > >
> > > > public double getAvgAge(List<Person>
> > persons){
> > > >  LOG.debug("Calulating average age for
> > persons: "+persons);
> > > >  try{
> > > >    int total_age =
> > getTotalAge(persons);
> > > >    double avg_age = total_age /
> > persons.size();
> > > >    return avg_age;
> > > >  } catch (Exception e ){
> > > >    LOG.error("Could not calculate
> > average age");
> > > >    throw e;
> > > >  }
> > > > }
> > > >
> > > >
> > > >  I'd like to be able to set up log4j so that
> > it doesn't log anyting if the
> > > > code works(calls no LOG.error). But when it does
> > call LOG.error i'd like to
> > > > also see the previous debug statements in my log
> > so I can figure out why my
> > > > code failed.
> > > >
> > > > So i would read something like this in my log:
> > > > DEBUG AverageAgeCalculator: Calulating average
> > age for persons: [ ]
> > > > ERROR AverageAgeCalculator: DivideByZeroError
> > > > ....stacktrace....
> > > >
> > > > But no unnecessary statements like:
> > > > DEBUG AverageAgeCalculator: Calulating average
> > age for persons: [Mary,
> > > > Peter ]
> > > > DEBUG AverageAgeCalculator: Calulating average
> > age for persons: [Carlos,
> > > > Hans, Klaus]
> > > >
> > > >
> > > > I happen to have some free time on my hands so
> > i'd like to try to code this
> > > > myself.
> > > > But before i start thinking about implementation,
> > I would like to know some
> > > > things.
> > > >
> > > > Does something like this already exist?
> > > > Do you think this would be useful?
> > > >
> > > > Greetings, Carlos Freund
> > > >
> > > >
> > > >
> > > >
> > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > > > For additional commands, e-mail: log4j-user-help@logging.apache.org
> > > >
> > > >
> > >
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > For additional commands, e-mail: log4j-user-help@logging.apache.org
> > 
> > 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message