18 Jul 23:32
Re: Circular Dependency through Appender
From: Ivan Alencar <ialencar <at> gmail.com>
Subject: Re: Circular Dependency through Appender
Newsgroups: gmane.comp.jakarta.log4j.user
Date: 2008-07-18 21:32:27 GMT
Subject: Re: Circular Dependency through Appender
Newsgroups: gmane.comp.jakarta.log4j.user
Date: 2008-07-18 21:32:27 GMT
Thanks Michael,
As I've implemented lot of logging in this component, I didn't feel like
wiping out all the nice logging just because of that circular dependency...
So I decided to ignore all settings from the log4j configuration file and
create a very specific Logger instance, programmatically.
Created a factory method which gives me a Logger instance, independent of
the root Logger, and has just one appender -- the ConsoleAppender.
*Instead of using *
private static Logger log = Logger.getLogger(MyClass.class);
*I use...*
private static Logger log = LoggingRescue.loggerFactory(MyClass.class);
*Fragment of the factory method...*
public static Logger loggerFactory(Class clazz)
{
/** The new Logger */
Logger log = null;
// Gets the logger for the class
log = Logger.getLogger(clazz);
log.removeAllAppenders();
...
/*
* Creates the console appender
*/
Layout consoleLayout = new PatternLayout(CONSOLE_LAYOUT);
Appender dspConsoleAppender = new ConsoleAppender(consoleLayout);
dspConsoleAppender.setName(CONSOLE_APPENDER_NAME);
log.addAppender(dspConsoleAppender);
log.setAdditivity(false);
log.setLevel(level);
...
return log;
}
Thanks,
Ivan Z. Alencar.
On Thu, Jul 17, 2008 at 6:12 AM, Michael Erskine <michael.erskine <at> ketech.com>
wrote:
> Ivan Alencar [mailto:ialencar <at> gmail.com] wrote:
> > I've created a log4j appender (by extending AppenderSkeleton) which uses
> a
> > component that depends on log4j.
>
> If you can change the component then one technique I sometimes use is to
> pass in a Logger object which may be null. Or I suppose one could
> temporarily set the static logger for that class to null while I'm accessing
> it from an Appender. You can always programmatically disable the Logger for
> the component's class too with Logger.getLogger(whatever).setLevel()
>
> Typically I try to write components that don't rely on any particular
> logging framework at runtime - keep it optional.
>
> Regards,
> Michael Erskine.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe <at> logging.apache.org
> For additional commands, e-mail: log4j-user-help <at> logging.apache.org
>
>
--
--
-----------------------------------------------------------------------------------
Ivan Zilotti Alencar
347-920-1226 (mobile)
212-514-7000 x5213 (work)
ialencar <at> gmail.com
-----------------------------------------------------------------------------------
RSS Feed