分享

如何把log4j中的message信息存储到数据库中 DataBase

 tingx 2016-10-09
{

    protected Connection connection = null;

    protected String sqlStatement = "";

    public JDBCAppenderExtended() {
        super();
    }

    public void append(LoggingEvent event) {
        buffer.clear();// 清空缓冲器
        buffer.add(event);
        flushBuffer();// 调用execute
    }

    protected void execute(String sql) throws SQLException {
        ThreadLocal myThread = (ThreadLocal) MDC.get("myThread");
        for (Iterator i = buffer.iterator(); i.hasNext();) {
            LoggingEvent event = (LoggingEvent) i.next();
            String message = (String) event.getMessage();
            Map map = (Map) myThread.get();
            Long packageid = Long.valueOf(map.get("packageid").toString());
            Long userid = Long.valueOf(map.get("userid").toString());
            Long operateType = (Long) map.get("operateType");
            Connection con = null;
            PreparedStatement stmt = null;
            PreparedStatement stmt1 = null;
            ResultSet res = null;
            Statement myStmt=null;
            String sql2 = "select seq_logrecord_id.nextval from dual";
            Long id = null;
            try {
                if (message.length() > 2000) {
                    String msg = message.substring(0, 2000);
                    con = getConnection();
                    myStmt = con.createStatement();
                    res = myStmt.executeQuery(sql2);
                    if (res.next()) {
                        id = res.getLong(1);
                    }
                    stmt = con.prepareStatement(sql);
                    stmt.setLong(1, id);
                    stmt.setLong(2, packageid);
                    stmt.setLong(3, userid);
                    stmt.setLong(4, operateType);
                    stmt.setString(5, msg);
                    stmt.executeUpdate();
                    String sql1 = "update logrecord set info=? where id=?";
                    stmt1 = con.prepareStatement(sql1);
                    if (message.length() > 4000) {
                        message = message.substring(0, 4000);
                    }
                    stmt1.setString(1, message);
                    stmt1.setLong(2, id);
                    stmt1.executeUpdate();
                } else {
                    con = getConnection();
                    myStmt = con.createStatement();
                    res = myStmt.executeQuery(sql2);
                    if (res.next()) {
                        id = res.getLong(1);
                    }
                    stmt = con.prepareStatement(sql);
                    stmt.setLong(1, id);
                    stmt.setLong(2, packageid);
                    stmt.setLong(3, userid);
                    stmt.setLong(4, operateType);
                    stmt.setString(5, message);
                    stmt.executeUpdate();
                }
            } catch (SQLException e) {
                if (stmt != null)
                    stmt.close();
                if (stmt1 != null)
                    stmt1.close();
                if (res != null)
                    res.close();
                if(myStmt!=null)
                    myStmt.close();
                if(buffer!=null)
                    buffer.clear();
                closeConnection(con);
                throw e;
            }
            if (stmt != null)
                stmt.close();
            if (stmt1 != null)
                stmt1.close();
            if (res != null)
                res.close();
            if(myStmt!=null)
                myStmt.close();
            closeConnection(con);
        }
    }

    /**
     * Override this to return the connection , or to clean up the resource.
     * 
     * The default behavior holds a single connection open until the appender is
     * closed (typically when garbage collected).
     
*/
    protected void closeConnection(Connection con) {
        try {
            if (connection != null && !connection.isClosed())
                connection.close();
        } catch (SQLException e) {
            errorHandler.error("Error closing connection", e,
                    ErrorCode.GENERIC_FAILURE);
        }
    }

    /**
     * 
     
*/
    public void setSql(String s) {
        sqlStatement = s;
        if (getLayout() == null) {
            this.setLayout(new PatternLayout(s));
        } else {
            ((PatternLayout) getLayout()).setConversionPattern(s);
        }
    }

    /**
     * Returns pre-formated statement eg: insert into LogTable (msg) values
     * ("%m")
     
*/
    public String getSql() {
        return sqlStatement;
    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多