I’m using log4j for record my program’s log. The code is like this:
static Logger logger = Logger.getLogger(MyClassName.class);
logger.error("This is error message");
However, instead of using error(), I want to implement my own error method, adding some additional info, e.g.
logger.searcherror("This is error message")
void searcherror(String mes){
logger.error("[search] "+mes)
}
I implemented this by using proxy pattern:
class Mylogger{
private Logger logger = null;
public MyLogger (Logger logger){
this.logger = logger;
}
public void searcherror(String msg){
logger.error("[search]"+msg);
}
}
There is a bug when using MyLogger when I print the log by setting appender layout: %l, which tells me the line num of this log. This will print the line number of the searcherror() method in MyLogger, not the actual line when I call Mylogger.serach("xxxxx"), I don’t how to fix it. Any suggestions?
You should not add functionality to logger itself. Instead implement your custom (or even configure one existing layout).
Take a look on this document: http://logging.apache.org/log4j/1.2/manual.html