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 Ids Achterhof <Id...@cmg.nl> on 2001/12/29 22:35:51 UTC

impliciet log4j usage.

I am stuck creating a subclass of httpServlet that will use log4j as it's
standard logging facility. ( Instead of the default log calls ).

There are two problems.

public class Log4JHttpServlet extends HttpServlet implements nl.cmg.log.Log
{

**1) The first is i want to use the subclass's classname as the classes
**category name. But how can i access a subclasses classname form a super
**class.

    /* Log4J logging variable */
    private static Category cat =
Category.getInstance(LogLevelHttpServlet.class.getName());
    private static String classname = Log4JHttpServlet.class.getName();
    /** Initializes the servlet.

2)
**I added all of the logging level functions like cat.debug until
**cat.fatal to my log4jHttpServlet superclass, but this f*cks up the line
**numbers when logging.

    public void debug(Object Message, Throwable t) {
        cat.debug(Message,  t );        

**The reason 4 doing so is that i what to be compatible with are standard
**way of logging so that i only have to change the class that my new
**servlet extends to change the logging implementation currently used.



example using normal classes.

1public class a
2{
3private static Category cat = Category.getInstance(<<RUNTIMECLASS NAME OF
B>>);
4
5
6    public void debug(Object Message, Throwable t) { 
7cat.debug(Message,  t ); }       
8
9    
10}

1public class b extends a
2{
3   public static void main ( String[] args )
4   {
5     this.debug("damn error",someException);
6   }
7}

OUT>dam error in class a at line 7,

and i want it to print

OUT>damn error in class b at line 5 

ids,
Thankx




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: impliciet log4j usage.

Posted by Steve Ebersole <st...@austin.rr.com>.
As for #1, you cannot reference anything relating to a subclass from a
static block in the superclass.  This just makes no sense.  The only way to
do what you are trying to do is to make cat a non-static variable (i.e., an
instance variable).  Then, you can simply use the "this" variable to refer
to the actual run-time type of the object:

public class Log4JHttpServlet
extends HttpServlet
implements nl.cmg.log.Log
{
    protected Category cat = Category.getInstance( this.getClass() );   //
this category now refers to the sub-class, or Log4JHttpServlet directly
instantiated
    ...

As for #2, well yeah:  the line numbers indicate from where the log4j method
was called and in your case, this is now the superclass not the actual
subclass.  Instead, make cat directly availableto the subclasses (either as
it is above by nature of the protected access or by providing an accessor
method) and directly call the logging methods of cat in the subclasses.

****************************************************************************
*********************
As a real simple scaled-down example:
public class Log4JHttpServlet
extends HttpServlet
implements nl.cmg.log.Log
{
    private Category cat = Category.getInstance( this.getClass() );
    protected Category getLogger() { return cat; }
    ...
}

public class MyServlet
extends Log4JHttpServlet
{
    public void doGet( HttpServletRequest req, HttpServletResponse res )
    {
        getLogger().debug( "Processing new request!" );
        ...
    }
}
*********************************************** OR
*******************************************
public class Log4JHttpServlet
extends HttpServlet
implements nl.cmg.log.Log
{
    protected Category cat = Category.getInstance( this.getClass() );
    ...
}

public class MyServlet
extends Log4JHttpServlet
{
    public void doGet( HttpServletRequest req, HttpServletResponse res )
    {
        cat.debug( "Processing new request!" );
        ...
    }
}


And your "example using normal classes" will not work for the same reasons
stated above.




----- Original Message -----
From: "Ids Achterhof" <Id...@cmg.nl>
To: <lo...@jakarta.apache.org>
Sent: Saturday, December 29, 2001 3:35 PM
Subject: impliciet log4j usage.


> I am stuck creating a subclass of httpServlet that will use log4j as it's
> standard logging facility. ( Instead of the default log calls ).
>
> There are two problems.
>
> public class Log4JHttpServlet extends HttpServlet implements
nl.cmg.log.Log
> {
>
> **1) The first is i want to use the subclass's classname as the classes
> **category name. But how can i access a subclasses classname form a super
> **class.
>
>     /* Log4J logging variable */
>     private static Category cat =
> Category.getInstance(LogLevelHttpServlet.class.getName());
>     private static String classname = Log4JHttpServlet.class.getName();
>     /** Initializes the servlet.
>
> 2)
> **I added all of the logging level functions like cat.debug until
> **cat.fatal to my log4jHttpServlet superclass, but this f*cks up the line
> **numbers when logging.
>
>     public void debug(Object Message, Throwable t) {
>         cat.debug(Message,  t );
>
> **The reason 4 doing so is that i what to be compatible with are standard
> **way of logging so that i only have to change the class that my new
> **servlet extends to change the logging implementation currently used.
>
>
>
> example using normal classes.
>
> 1public class a
> 2{
> 3private static Category cat = Category.getInstance(<<RUNTIMECLASS NAME OF
> B>>);
> 4
> 5
> 6    public void debug(Object Message, Throwable t)

> 7cat.debug(Message,  t ); }
> 8
> 9
> 10}
>
> 1public class b extends a
> 2{
> 3   public static void main ( String[] args )
> 4   {
> 5     this.debug("damn error",someException);
> 6   }
> 7}
>
> OUT>dam error in class a at line 7,
>
> and i want it to print
>
> OUT>damn error in class b at line 5
>
> ids,
> Thankx
>
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>