Log4j单例包装的好处? [英] Benefits of Log4j singleton wrapper?

查看:201
本文介绍了Log4j单例包装的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近继承了一些Java代码,需要将其集成到我正在从事的项目中.我的项目是一个服务代理,用于处理和转换XML消息.浏览新代码时,我发现了以下日志记录类:

I have recently inherited some Java code and need to integrate it into a project that I am working on. My project is a service agent that processes and transforms XML messages. While looking through the new code, I discovered the following logging class:

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class MyLogger {

    private static MyLogger instance = null;
    protected final static Logger log = Logger.getLogger(MyLogger.class);

    private MyLogger() {
        super();
    }

    public static MyLogger getInstance(){
        if(instance  == null){
            instance  = new MyLogger();
            BasicConfigurator.configure();
            log.setLevel(Level.ALL);
        }
        return instance;
    }

    public void info(String myclass, String msg) {
        log.info("[" + myclass + "] " + msg);

    }

    public void error(String myclass, String msg, Exception ce) {               
        log.error("[" + myclass + "] " + msg, ce);      
    }

    public void warning(String myclass, String msg) {
        log.warn("[" + myclass + "] " + msg);
    }    
}

此类基本上用(另一个)单例包装log4j.我需要集成的所有类的日志记录如下所示:

This class basically wraps log4j with (another) singleton. All of the logging in the classes that I need to integrate look something like this:

public class MyClass {
   private final static MyLogger log = MyLogger.getInstance();
   private final static String myclass = MyClass.class.getName();

   ...

   log.info(myclass, "Information message...");   
}

我看不到使用额外的类进行日志记录有任何明显的好处,因此,我正在考虑重构此代码以删除MyLogger类并以以下方式记录日志:

I do not see any obvious benefit to using an extra class for logging, thus I am considering refactoring this code to remove the MyLogger class and log in the following fashion:

import org.apache.log4j.Logger;

public class MyClass {
   private static Logger log = Logger.getLogger(MyClass.class);

   ...

   log.info("Information Message...");     
}

这将使日志记录机制在整个项目中保持一致.在执行此操作之前,我想知道用我可能会缺少的单例类包装Log4j是否有任何好处.谢谢!

This would make the logging mechanism consistent across the project. Before I do this, I would like to know if there are any benefits to wrapping Log4j with a singleton class that I may be missing. Thanks!

编辑:谢谢大家的有用回答-我从中汲取了一些新见解.纳森·休斯(Nathan Hughes)的回答是通过保持完好无损的类来指出失去的功能-我一直认为,让单身人士离开的最大弊端就是代码膨胀.我会废课.

Thanks everyone for the helpful answers - I pickup up several new insights from each. Accepted Nathan Hughes' answer for pointing out lost functionality by leaving the class intact - I had been assuming that the biggest downside to leaving the singleton alone was simply code bloat. I will trash the class.

推荐答案

摆脱它.使用这种怪异意味着通过它进行的所有日志记录都将使用相同的记录器(MyLogger)和方法列出(这就是为什么其方法的参数包括要记录的事物的类的原因).这意味着您不仅必须向每个记录器调用中添加任何类,方法和行号信息,而且还不能像使用典型的log4j方法将类作为记录器那样对不同包进行日志级别的任何过滤.

Get rid of it. Using this monstrosity means all logging that goes through it will be listed with the same logger (MyLogger) and method (which is why the arguments to its methods include the class of the thing being logged). That means not only do you have to add any class, method, and line number information to each logger call, but you can't do any filtering on log levels for different packages the way you could using the typical log4j approach with classes as loggers.

这东西是一块垃圾,没有它,你会更好.

This thing is a piece of junk and you are better off without it.

这篇关于Log4j单例包装的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆