You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Harp, George" <GH...@GAINSystems.com> on 2006/07/21 19:17:16 UTC

Declaration of loggers

Hello,
   I am used to declaring Loggers in my code the following way
 
private final static Logger logger_m = Logger.getLogger( MyClass.class
);
 
A co worker wants do declare loggers from a base class so that each sub
class has a seperate logger
 
public class BaseClass
{
    private Logger logger_m = Logger.getLogger( MyClass.class );
 
    public Logger createLogger( Object baseObject )
    {
        logger_m = Logger.getLogger(  baseObject.getClass( ) );
    }
    public Logger getLogger( )
    {
        return( logger_m );
    }
}
 
I feel the second approach is to much effort when the first and "normal"
approach if a 1 liner, but i need to know if there are any ramifications
of the second approach?

Re: Declaration of loggers

Posted by Mi...@anixter.com.
Here's a thought

class ExampleA {
  private static final Logger logger_m = Logger.getLogger(ExampleA.class);

  void hello() {
    getLogger().info("Hello!");
  }
  public Logger getLogger() {
    return logger_m;
  }

}

class ExampleB extends ExampleA {
  private static final Logger logger_m = Logger.getLogger(ExampleB.class);

  void hi() {
    getLogger().info("This is a greeting:");
    hello();
    getLogger().info("How are you?");
  }
  public Logger getLogger() {
    return logger_m;
  }
}




"Javier Gonzalez" <ja...@gmail.com> 
07/21/2006 01:47 PM
Please respond to
"Log4J Users List" <lo...@logging.apache.org>


To
"Log4J Users List" <lo...@logging.apache.org>
cc

Subject
Re: Declaration of loggers






If you use a lot of inheritance, declaring your loggers in the private
static final way will make calls to logging that are in inherited code
to appear as coming from the superclass.

For example:

class ExampleA {
  private static final Logger logger_m = Logger.getLogger(ExampleA.class);

  void hello() {
    logger_m.info("Hello!");
  }

}

class ExampleB extends ExampleA {
  private static final Logger logger_m = Logger.getLogger(ExampleB.class);

  void hi() {
    logger_m.info("This is a greeting:");
    hello();
    logger_m.info("How are you?");
  }
}

The output from hi() would be like:
[INFO] (ExampleB): This is a greeting:
[INFO] (ExampleA): Hello!
[INFO] (ExampleB): How are you?

If you have multiple classes sublclassing A, it could get confusing.

A way to avoid this is to declare the logger as:

protected Logger m_log = Logger.getLogger(this.getClass());

That way the logger gets named as the instance class and not the base
class. (You still would have to make some kind of workaround for
static methods)

Depending on what effect you want to see (i.e. see the logging calls
as coming from superclass implementation or the subclass
implementation) you can use either approach.

Hope that helps,

-- 
Javier González Nicolini

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




Re: Declaration of loggers

Posted by Javier Gonzalez <ja...@gmail.com>.
If you use a lot of inheritance, declaring your loggers in the private
static final way will make calls to logging that are in inherited code
to appear as coming from the superclass.

For example:

class ExampleA {
  private static final Logger logger_m = Logger.getLogger(ExampleA.class);

  void hello() {
    logger_m.info("Hello!");
  }

}

class ExampleB extends ExampleA {
  private static final Logger logger_m = Logger.getLogger(ExampleB.class);

  void hi() {
    logger_m.info("This is a greeting:");
    hello();
    logger_m.info("How are you?");
  }
}

The output from hi() would be like:
[INFO] (ExampleB): This is a greeting:
[INFO] (ExampleA): Hello!
[INFO] (ExampleB): How are you?

If you have multiple classes sublclassing A, it could get confusing.

A way to avoid this is to declare the logger as:

protected Logger m_log = Logger.getLogger(this.getClass());

That way the logger gets named as the instance class and not the base
class. (You still would have to make some kind of workaround for
static methods)

Depending on what effect you want to see (i.e. see the logging calls
as coming from superclass implementation or the subclass
implementation) you can use either approach.

Hope that helps,

-- 
Javier González Nicolini

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


Re: Declaration of loggers

Posted by James Stauffer <st...@gmail.com>.
How about this.  Then each sub-class has its own logger and it isn't much work.

protected Logger logger_m = Logger.getLogger(getClass());

On 7/21/06, Harp, George <GH...@gainsystems.com> wrote:
> Hello,
>    I am used to declaring Loggers in my code the following way
>
> private final static Logger logger_m = Logger.getLogger( MyClass.class
> );
>
> A co worker wants do declare loggers from a base class so that each sub
> class has a seperate logger
>
> public class BaseClass
> {
>     private Logger logger_m = Logger.getLogger( MyClass.class );
>
>     public Logger createLogger( Object baseObject )
>     {
>         logger_m = Logger.getLogger(  baseObject.getClass( ) );
>     }
>     public Logger getLogger( )
>     {
>         return( logger_m );
>     }
> }
>
> I feel the second approach is to much effort when the first and "normal"
> approach if a 1 liner, but i need to know if there are any ramifications
> of the second approach?
>
>


-- 
James Stauffer
Are you good? Take the test at http://www.livingwaters.com/good/

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