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 "G.L. Grobe" <ga...@grobe.net> on 2001/09/05 01:48:55 UTC

making a wrapper for log4j

I'm trying to put a wrapper around the Logging so that I won't have to put log4j imports and declarations all over my code. I've heard of other doing this but I'm not exactly sure how so I was hoping I could get some pointers on what I've done so far.

Any help much appreciated.

----
package com.neuroquest.cais.log;

import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;

public class Logger extends Category {
   static Category cat;

   public Category getCat() {
      return cat;
   }  
         
   public Logger(String name) {
      try {
         PropertyConfigurator.configure("rtlog.properties");
      }
      catch (Exception e) {
         System.err.println("Could not open rtlog.properties");
      }
      if (cat == null)
         cat = Category.getInstance(name);
   }
}
   

Re: making a wrapper for log4j

Posted by Naresh Sharma <na...@yahoo.com>.
Hello ,

Well i guess in this implementation code which will use your logging
code has to make call like this in order to log the messages:

Logger logger = new Logger("xyz");
Category cat = logger.getCat();
cat.debug("Hi, this is Debug");
...

I think better way to implement could be like this

Create a interface, which will contain method for logging
Now create a class which will implement above mentioned interface, this
class will act as Wrapper over the Log4J API and the code which is using
this logging module have to just get the reference of this class and
call the method for logging. So any time you can change the
implementation if need to do so.

Does this makes sense ????

Anyway i have written the code in crude form below, please comment what
do you think.

Creating Interface
public interface Logger {

    public void debug (Object message);

     public void debug (Object message, Throwable exception);

     public void info (Object message);

     public void info (Object message, Throwable exception);

     public void warn (Object message);

     public void warn (Object message, Throwable exception);

     public void error (Object message);

     public void error (Object message, Throwable exception);

     public void fatal (Object message);

     public void fatal (Object message, Throwable exception);
}


Creating Wrapper class

public class Log4JLogger implements Logger { // May be we can have
Singleton class.

public Log4JLogger() {
    initi();
}

private void init() {
    // Load Log4J config file here.

}

}


--
Naresh


"G.L. Grobe" wrote:

> I'm trying to put a wrapper around the Logging so that I won't have to
> put log4j imports and declarations all over my code. I've heard of
> other doing this but I'm not exactly sure how so I was hoping I could
> get some pointers on what I've done so far. Any help much appreciated.
> ----package com.neuroquest.cais.log; import org.apache.log4j.Category;
>
> import org.apache.log4j.PropertyConfigurator;public class Logger
> extends Category {   static Category cat;    public Category getCat()
> {
>       return cat;
>    }
>
>    public Logger(String name) {      try {
>          PropertyConfigurator.configure("rtlog.properties");
>       }
>       catch (Exception e) {
>          System.err.println("Could not open rtlog.properties");
>       }      if (cat == null)
>          cat = Category.getInstance(name);   }
> }
>

Re: making a wrapper for log4j

Posted by Don Taylor <do...@yahoo.com>.
Check out following link from the mail archives:

http://marc.theaimsgroup.com/?l=log4j-user&m=99858973708304&w=2


--- "G.L. Grobe" <ga...@grobe.net> wrote:
> I'm trying to put a wrapper around the Logging so that I won't have
> to put log4j imports and declarations all over my code. I've heard of
> other doing this but I'm not exactly sure how so I was hoping I could
> get some pointers on what I've done so far.
> 
> Any help much appreciated.
> 
> ----
> package com.neuroquest.cais.log;
> 
> import org.apache.log4j.Category;
> import org.apache.log4j.PropertyConfigurator;
> 
> public class Logger extends Category {
>    static Category cat;
> 
>    public Category getCat() {
>       return cat;
>    }  
>          
>    public Logger(String name) {
>       try {
>          PropertyConfigurator.configure("rtlog.properties");
>       }
>       catch (Exception e) {
>          System.err.println("Could not open rtlog.properties");
>       }
>       if (cat == null)
>          cat = Category.getInstance(name);
>    }
> }
>    
> 


=====
Don

__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com

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