Java自定义记录器:记录标准或/和最佳实践 [英] Java custom logger: logging standards or/and best practices

查看:57
本文介绍了Java自定义记录器:记录标准或/和最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个框架,我希望jar尽可能轻巧并且尽可能独立.

I am developing a framework and I want the jar to be light weight and independent as much as it can be.

所以我写了一个日志类:

So I wrote a logging class:

import java.util.Date;
import java.util.Properties;

public class Logger {

    private static final Logger me = new Logger();
    private static boolean info = false;
    private static boolean debug = false;
    private static boolean error = false;
    private static String className = null;

    public static Logger getInstance(Class<?> clazz) {  
        className = clazz.getCanonicalName();
        try {
            Properties props = new CustProps().load(clazz);
            if(props.get(CustProps.NAME_LOG_MODE) != null) {
                String devMode = props.getProperty(CustProps.NAME_LOG_MODE)
                                   .toLowerCase();
                if("info".equals(devMode)) {
                    info = true;
                    debug = true;
                } else if("debug".equals(devMode)) {
                    debug = true;
                } 
            }
        } catch (Exception e) {
            // debug is error by default
        }

        error = true;
        return me;
    }

    public void logError(Object msg) {
        if(isError()) {
            System.out.println(new Date().toString()
              + " ERROR ["+Logger.className+"] - " + msg);
        }
    }

    public void logDebug(Object msg) {
        if(isDebug()) {
            System.out.println(new Date().toString()
              + " DEBUG ["+Logger.className+"] - " + msg);
        }
    }

    public void logInfo(Object msg) {
        if(isInfo()) {
            System.out.println(new Date().toString()
              + " INFO ["+Logger.className+"] - " + msg);
        }
    }

    public boolean isInfo() { return Logger.info; }

    public boolean isDebug() { return Logger.debug; }

    public boolean isError() { return Logger.error; }

}

  • 什么是使日志记录更好的最佳实践?
  • 让自己的记录器值得吗?
  • 使用此记录器会使我的框架比选择现有的东西(例如log4j)更糟吗?
    • What are the best practices to make this logging better?
    • Is making your own logger even worth it?
    • Will the use of this logger make my framework worse than choosing something existing (like log4j)?
    • 推荐答案

      您不喜欢java.util.logging.Logger吗?它包含在JDK中,您不需要其他任何东西.

      Don't you like java.util.logging.Logger? It is included into JDK, you don't need anything else.

      这篇关于Java自定义记录器:记录标准或/和最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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