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

📄 generallogeventdao.java.svn-base

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

import com.gisinfo.common.log.LogWriter;
import com.gisinfo.common.log.LogEvent;
import com.gisinfo.common.log.LogException;
import com.gisinfo.common.log.Level;
import com.gisinfo.common.util.CommonUtil;
import com.gisinfo.common.util.IDCreator;
import com.gisinfo.common.util.DBUtil;
import com.gisinfo.sql.delegater.Connection;
import com.gisinfo.sql.delegater.Statement;
import com.gisinfo.sql.delegater.ResultSet;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.text.ParseException;

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

/**
 * User: Ken
 * Date: 2008-6-18
 * Time: 17:09:32
 */
public class GeneralLogEventDao {
    private static String defaultTableName = "T_COMMON_LOG";

    /**
     * @param events
     * @param tableName  日志表名,如果采用默认的表(T_COMMON_LOG)可填写null
     * @param dbConnName 对应DBConn的名字,如果采用默认的,课填写null
     * @return
     */
    public static int[] saveLog(List<GeneralLogEvent> events, String tableName, String dbConnName) throws Exception {

        if (events == null || events.size() == 0) return new int[0];
        int[] result = new int[events.size()];
        for (int i = 0; i < result.length; i++) result[i] = -1;
        if (CommonUtil.isNullOrEmpty(tableName)) tableName = defaultTableName;
        dbConnName = CommonUtil.trimToNull(dbConnName);

       // String sql = "insert into " + tableName + "(F_ID,F_WHO,F_WHERE,F_WHEN,F_ACTION,F_WHAT,F_LVL,F_IP,F_SYSTEMNAME)values(?,?,?,?,?,?,?,?,?)";
        String sql = "insert into " + tableName + "(PID,LOG_FROM,LOG_WHO,LOG_WHEN,LOG_LEVEL,LOG_DO,LOG_WHAT," +
                "LOG_DESCRIPTION,LOG_SYSTEMNAME,LOG_INVOKER)values(?,?,?,?,?,?,?,?,?,?)";
        Connection con = null;
        PreparedStatement pstmt = null;
            /*
原结构 现在的结构 对应含义 相关属性
F_ID PID 主键 UUID  Varchar(32) NOT NULL,
F_IP LOG_FROM 来源 Varchar(64) NULL
F_WHO LOG_WHO 谁? Varchar(64) NULL,
F_WHEN LOG_WHEN 什么时间? Datetime NULL
F_LVL LOG_LEVEL 重要级别 Varchar(64) NULL
F_ACTION LOG_DO 做 Varchar(64) NULL
F_WHAT LOG_WHAT 什么事情? Varchar(64) NULL
F_DESRCIPTION LOG_DESCRIPTION 事件描述(备注) Varchar(256) NULL
F_SYSTEMNAME LOG_SYSTEMNAME 在什么系统 Varchar(128) NULL
F_WHERE LOG_INVOKER 在何处(调用)记录的日志?

        */
        try {
            con = DBUtil.getConnection(dbConnName);
            con.setAutoCommit(false);
            pstmt = con.prepareStatement(sql);
            for (GeneralLogEvent e : events) {
                pstmt.setString(1, e.getId());
                pstmt.setString(2, CommonUtil.trimToEmpty(e.getIp()));
                pstmt.setString(3, CommonUtil.trimToEmpty(e.getWho()));
                pstmt.setTimestamp(4, new Timestamp(e.getWhen().getTime()));
                pstmt.setString(5, CommonUtil.trimToEmpty(e.getLvl()));
                pstmt.setString(6, CommonUtil.trimToEmpty(e.getAction()));
                pstmt.setString(7, e.getWhat());
                pstmt.setString(8, CommonUtil.trimToEmpty(e.getDescription()));
                pstmt.setString(9, CommonUtil.trimToEmpty(e.getSystemName()));
                pstmt.setString(10, CommonUtil.trimToEmpty(e.getWhere()));
                pstmt.addBatch();
            }
            result = pstmt.executeBatch();
            con.commit();
        } catch (Exception e) {
            DBUtil.rollBack(con);
            throw e;
        } finally {
            DBUtil.close(con, pstmt, null);
        }
        return result;
    }

    //when lvl
    //(String who, String action, String what, String description, String where, String ip, String systemName)
    public List<GeneralLogEvent> queryByCondition(String sql, String dbConnName) throws Exception {
        //Map m = new CaseInsensitiveMap(map);
        //if (CommonUtil.isNullOrEmpty(tableName)) tableName = defaultTableName;

        List<GeneralLogEvent> result = new ArrayList<GeneralLogEvent>();
        dbConnName = CommonUtil.trimToNull(dbConnName);
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = DBUtil.getConnection(dbConnName);
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);

            /*
              原结构 现在的结构 对应含义 相关属性
F_ID PID 主键 UUID  Varchar(32) NOT NULL,
F_IP LOG_FROM 来源 Varchar(64) NULL
F_WHO LOG_WHO 谁? Varchar(64) NULL,
F_WHEN LOG_WHEN 什么时间? Datetime NULL
F_LVL LOG_LEVEL 重要级别 Varchar(64) NULL
F_ACTION LOG_DO 做 Varchar(64) NULL
F_WHAT LOG_WHAT 什么事情? Varchar(64) NULL
F_DESRCIPTION LOG_DESCRIPTION 事件描述(备注) Varchar(256) NULL
F_SYSTEMNAME LOG_SYSTEMNAME 在什么系统 Varchar(128) NULL
F_WHERE LOG_INVOKER 在何处(调用)记录的日志?
            */
            while (rs.next()) {
                GeneralLogEvent e = new GeneralLogEvent();
                e.setId(rs.getString("PID"));
                e.setIp(rs.getString("LOG_FROM"));
                e.setWho(rs.getString("LOG_WHO"));
                e.setWhen(rs.getTimestamp("LOG_WHEN"));
                e.setLevel(Level.getLevel(rs.getString("LOG_LEVEL")));
                e.setAction(rs.getString("LOG_DO"));
                e.setWhat(rs.getString("LOG_WHAT"));
                e.setDescription(rs.getString("LOG_DESCRIPTION"));
                e.setSystemName(rs.getString("LOG_SYSTEMNAME"));
                e.setWhere(rs.getString("LOG_INVOKER"));
                result.add(e);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(con, stmt, rs);
        }
    }

}

⌨️ 快捷键说明

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