简体   繁体   中英

Extend Log4JLogger and invoke subclass during runtime

I currently am using the SocketAppender in Log4J and have run into some issues with passing data over to a remote host, specifically around losing LocationInfo (class name, line number, etc). What I have done is the following:

import org.apache.commons.logging.impl.Log4JLogger;

public class MyLogger extends Log4JLogger  
{  
    ...
       public void debug(Object message)  
       {   
           String extra = "Extra!";  
           super.debug(message + extra);
       }   
    ...
}  

At runtime how can I change the following code to grab MyLogger instead of Log4JLogger ?

private static Log logger = LogFactory.getLog(Test.class);

You don't have to extends Logger but implement your own Appender or extend the base Log4J Appender you need. For example you can extend AppenderSkeleton .

I cannot imagine exactly why do you need it. Another possibility could be use Log4J Mapped Diagnostic Context .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM