⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loggerconfig.java.svn-base

📁 日志组件,添加到应用中,可在数据库中查询日志
💻 SVN-BASE
字号:
package com.gisinfo.common.log;

import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.apache.commons.collections.map.HashedMap;

import java.util.*;
import java.text.MessageFormat;

import com.gisinfo.common.util.CommonUtil;

/**
 * User: Ken
 * Date: 2008-5-21
 * Time: 9:00:54
 */
public class LoggerConfig {
    private static Properties prop = new Properties();
    private static boolean showLogInfo = true;
    public static boolean loadConfigSuccess = true;

    public  static final String configFileName = "gisLog.properties";
    private static final String log_prefix = "gislog.logger.";
    private static final String custom_log_prefix = "gislog.customlogger.";
    private static final String level_key = "level";
    private static final String writer_key = "writer";
    private static final String log_connection_key = ".";
    private static final String defaultKey = "default";
    private static final String time_Key = "logTimeInterview";
    private static final String task_delay_Key = "logTaskDelay";
    private static final String show_info_key = "showLogInfo";
    private static final String format_key = "format";
    private static final String task_enable_key = "enableLogTask";
    private static final String custom_loggers_key = "gisLog.customloggers";
    private static final String splite_Key = ",";
    private static String format = null;
    private static Map configMap = null;
    private static Map systemLevelMap = new CaseInsensitiveMap();
    private static Map systemWriterMap = new CaseInsensitiveMap();


    private static Map customLevelMap = new CaseInsensitiveMap();
    private static Map customWriterMap = new CaseInsensitiveMap();
    static List customLoggers = new ArrayList();


    private static Map<String, WriterBean> writerMapBean = new CaseInsensitiveMap();
    private static long task_delay_time = 5000;          //延迟启动日志任务时间
    private static long task_interview_time = 120000;   //默认间隔 2分钟


    public static boolean showLogInfo() {
        return showLogInfo;
    }

    static {
        init();
        if (loadConfigSuccess && enableLogTask()) {
            writeInfo("启动日志任务!");
            beginTask();
        }
        writeInfo("日志初始化完成!");
    }

    private static void beginTask() {
        if (!loadConfigSuccess) return;
        Timer timer = new Timer();
        timer.schedule(new LogTask(), getTaskDelayTime(), getLongTimeInterview());
    }

    private static boolean enableLogTask() {
        return !"false".equalsIgnoreCase((String) configMap.get(log_prefix + task_enable_key));
    }

    private static long getTaskDelayTime() {
        return CommonUtil.getLong(configMap.get(log_prefix + task_delay_Key), task_delay_time);
    }

    //gisLog.logger.com.gisinfo.common.writer


    public static long getLongTimeInterview() {
        return CommonUtil.getLong(configMap.get(log_prefix + time_Key), task_interview_time);
    }

    public static Level getDefaultLevel() {
        return getLevel(defaultKey);
    }

    public static List<String> getDefaultWriter() {
        return getWriterByStr(defaultKey);
    }


    public static Map getConfig() {
        return new CaseInsensitiveMap(prop);
    }

    public static String getConfigValue(String key) {
        return CommonUtil.trimToEmpty(configMap.get(key));
    }

    public static Level getLevel(String str) {
        return getLevel(str, false);
    }

    public static Level getLevel(String str, boolean customLogger) {
        if (customLogger) {
            Object temp = customLevelMap.get(str);
            if (CommonUtil.isNotNullOrEmpty(temp)) return Level.getLevel((String) temp);
        }
        if (CommonUtil.isNotNullOrEmpty(str)) {
            List keyList = new ArrayList(systemLevelMap.keySet());
            sortStringListByLengthDesc(keyList);
            for (Object key : keyList) {
                String temp = CommonUtil.trimToEmpty(key);
                Object value = systemLevelMap.get(key);
                if (str.startsWith(temp) && value != null) return Level.getLevel((String) value);
            }
        }
        Object o = systemLevelMap.get(defaultKey);
        if (o == null) {
            writeInfo("找不到默认的级别设置!采用INFO级别!");
            return Level.getDefaultLevel();
        }
        return Level.getLevel((String) o);
    }

    private static void sortStringListByLengthDesc(List keyList) {
        Collections.sort(keyList, new Comparator() {
            public int compare(Object o1, Object o2) {
                return CommonUtil.trimToEmpty(o1).length() - CommonUtil.trimToEmpty(o2).length();
            }
        });
    }

    public static List<String> getWriterByStr(String str) {
        return getWriterByStr(str, false);
    }

    //gisLog.logger.default.writer = consoleWriter,fileWriter
    public static List<String> getWriterByStr(String str, boolean customLogger) {
        Object o = null;
        if (customLogger) o = customWriterMap.get(str);
        else {
            o = systemWriterMap.get(str);
            List keyList = new ArrayList(systemWriterMap.keySet());
            sortStringListByLengthDesc(keyList);
            if (CommonUtil.isNotNullOrEmpty(str)) {
                for (Object key : keyList) {
                    String temp = CommonUtil.trimToEmpty(key);
                    Object value = systemWriterMap.get(key);
                    if (str.startsWith(temp) && value != null) {
                        o = value;
                        break;
                    }
                }
            }
        }


        if (o == null) o = systemWriterMap.get(defaultKey);
        if (o == null) return null;
        String writers[] = o.toString().trim().split(splite_Key);
        List result = new ArrayList();
        for (String str_temp : writers) result.add(str_temp.trim());
        return result;
    }


    //consoleWriter
    public static WriterBean getWriterBeanByName(String name) {
        return writerMapBean.get(name);
    }


    public static void writeInfo(String info) {
        if (CommonUtil.isNotNullOrEmpty(format)) System.out.println(MessageFormat.format(format, info));
        else System.out.println("=========Log:" + info);
    }

    public static void writeInfo(Exception e) {
        writeInfo(e.getMessage());
    }


    static void init() {
        try {
            prop.load(LoggerConfig.class.getResourceAsStream("/" + configFileName));
            initParameters();
            initLevelMap();
            initWriterMap();
            initCustomLoggers();
            initWriterMapBean();


        } catch (Exception e) {
            writeInfo("配置文件读取失败!请将文件" + configFileName + "放到classpath下!");
            loadConfigSuccess = false;
            //writeInfo(e);
        }
    }

    /*
    gisLog.customLoggers=a,b,c
    gisLog.customLogger.a.level=
    gisLog.customLogger.a.writer=


    private static Map customLevelMap = new CaseInsensitiveMap();
    private static Map customWriterMap = new CaseInsensitiveMap();
    private static List customLoggers = new ArrayList();
     */
    private static void initCustomLoggers() {
        String customLoggerStr = getConfigValue(custom_loggers_key);
        String[] loggers = customLoggerStr.split(splite_Key);
        if (loggers.length == 0) return;
        customLoggers = CommonUtil.arrayToList(loggers);
        //========init customLogger level===============
        //========init customLogger writer===============
        for (Object temp : configMap.keySet()) {
            String key = (String) temp;
            if (key == null) continue;
            String value = getConfigValue(key);
            if (key.startsWith(custom_log_prefix) && key.endsWith(log_connection_key + level_key)) {
                customLevelMap.put(key.substring(custom_log_prefix.length(), key.length() - (log_connection_key + level_key).length()), value);
            } else if (key.startsWith(custom_log_prefix) && key.endsWith(log_connection_key + writer_key)) {
                customWriterMap.put(key.substring(custom_log_prefix.length(), key.length() - (log_connection_key + writer_key).length()), value);
            }

        }


    }

    private static void initParameters() {
        configMap = new CaseInsensitiveMap(prop);
        showLogInfo = !"false".equalsIgnoreCase((String) configMap.get(log_prefix + show_info_key));
        format = (String) configMap.get(log_prefix + format_key);
        //To change body of created methods use File | Settings | File Templates.
    }

    private static void initWriterMapBean() {
        Set<String> writerName = new HashSet();
        Map allWriterMap = new HashMap();
        allWriterMap.putAll(systemWriterMap);
        allWriterMap.putAll(customWriterMap);

        for (Object str : allWriterMap.values()) {
            if (CommonUtil.isNullOrEmpty(str)) continue;
            String writers[] = str.toString().trim().split(splite_Key);
            for (String str_temp : writers) writerName.add(str_temp.trim());
        }
        //fileWriter.class
        for (String name : writerName) {
            WriterBean bean = new WriterBean();
            bean.setKey(name);
            for (Object o : configMap.keySet()) {
                String key = CommonUtil.trimToEmpty(o);
                String value = CommonUtil.trimToEmpty(configMap.get(key));
                if (!key.toLowerCase().startsWith((name + log_connection_key).toLowerCase()) || CommonUtil.isNullOrEmpty(value))
                    continue;
                bean.addProperty(key.substring((name + log_connection_key).length()), value);
            }
            writerMapBean.put(name, bean);
        }

    }

    //gisLog.logger.com.gisinfo.common.writer = dbWriter
    private static void initWriterMap() {
        for (Object o_temp : configMap.keySet()) {
            String key = (String) o_temp;
            String value = (String) configMap.get(key);
            if (!isValidateWriter(key, value)) continue;
            putInWriterMap(key, value);
        }
    }

    //gisLog.logger.com.gisinfo.common.writer = dbWriter
    private static void putInWriterMap(String key, String value) {
        String realPrefix = key.trim().substring(log_prefix.length(), key.length() - (log_connection_key + writer_key).length());
        // com.gisinfo.common        dbWriter
        systemWriterMap.put(realPrefix, value.trim());
    }

    //gisLog.logger.com.gisinfo.common.writer = dbWriter
    private static boolean isValidateWriter(String key, String value) {
        if (CommonUtil.isNullOrEmpty(key) || CommonUtil.isNullOrEmpty(value)) return false;

        if (!key.trim().toLowerCase().startsWith(log_prefix)) return false;
        if (!key.trim().toLowerCase().endsWith(log_connection_key + writer_key)) return false;
        if (!(key.trim().length() > (log_prefix + writer_key).length())) return false;
        return true;
    }

    //gisLog.logger.com.gisinfo.common.level=debug
    private static void initLevelMap() {
        for (Object o_temp : configMap.keySet()) {
            String key = (String) o_temp;
            String value = (String) configMap.get(key);
            if (!isValidateLevel(key, value)) continue;
            putInLevelMap(key, value);
        }
    }

    //gisLog.logger.com.gisinfo.common.level=debug
    private static void putInLevelMap(String key, String value) {
        String realPrefix = key.trim().substring(log_prefix.length(), key.length() - (log_connection_key + level_key).length());
        // com.gisinfo.common        debug
        systemLevelMap.put(realPrefix, value.trim());
    }

    //gisLog.logger.com.gisinfo.common.level=debug
    private static boolean isValidateLevel(String key, String value) {
        if (CommonUtil.isNullOrEmpty(key) || CommonUtil.isNullOrEmpty(value)) return false;

        if (!key.trim().toLowerCase().startsWith(log_prefix)) return false;
        if (!key.trim().toLowerCase().endsWith(log_connection_key + level_key)) return false;
        if (!(key.trim().length() > (log_prefix + level_key).length())) return false;
        return true;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -