NutzCN Logo
分享 SLog在oracle下的实现
发布于 2688天前 作者 Leewell1989 1825 次浏览 复制 上一个帖子 下一个帖子
标签:

slog在oracle下使用遇到的问题:
1.SLogBean 中的msg字段默认长度8192,而oracle的varchar2最大长度4000
2.id字段类型默认long类型,我习惯定义位string类型
3.我的userid也是string类型的,所以uid也要定义位string

基于以上,做了如下尝试:
1.新建YlogBean, 重新定义msg和id字段(本来想继承一下,发现字段类型不同是不让继承的)
2.新建YlogService,*重新写*create()代替父类c(), 重写父类的sync(), log(), Callable GET_USER_ID, dao(),

搞定, 附一下代码:

@Table("ysm_sys_log_${ym}")
public class YlogBean implements Serializable {

	private static final long serialVersionUID = 4048681972879639280L;

	@Name
	@Prev({
			@SQL( db = DB.ORACLE, value = "SELECT SYS_GUID() FROM DUAL")
	})
	private String id;
	
	@Column("t")// aop.before aop.after aop.error
	private String t;
	
	@Column("tg")
	private String tag; 
	
	@Column("src")
	@ColDefine(width=1024)
	private String source;
	
	@Column("u_id")
	private String uid;
	
	@Column("ip")
	private String ip;
	
	@Column
	@ColDefine(width=4000)
	private String msg;

	@Column("ct")
	protected Date createTime;

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getT() {
		return t;
	}

	public void setT(String t) {
		this.t = t;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
}
public class YlogService extends SlogService{
	
    private static final Log log = Logs.get();

    /**
     * 创建一个YlogBean对象
     * @param t
     * @param tag
     * @param source
     * @param msg
     * @return
     */
    public YlogBean create(String t, String tag, String source, String msg) {
        YlogBean ylog = new YlogBean();
        ylog.setCreateTime(new Date());
        if (t == null || tag == null || msg == null) {
            throw new RuntimeException("t/tag/msg can't null");
        }
        if (source == null) {
            StackTraceElement[] tmp = Thread.currentThread().getStackTrace();
            if (tmp.length > 2) {
                source = tmp[2].getClassName() + "#" + tmp[2].getMethodName();
            } else {
                source = "main";
            }
        }
        ylog.setT(t);
        ylog.setTag(tag);
        ylog.setSource(source);
        ylog.setMsg(msg);
        if (Mvcs.getReq() != null) {
            ylog.setIp(Lang.getIP(Mvcs.getReq()));
        }
        return ylog;
    }

    /**
     * 重写父类方法
     * 将原来调用dao().fastInsert()改为dao().insert(),原因是将id改成String类型了
     * @param ylog
     */
    @Override
    public void sync(Object ylog) {
        try {
            this.dao().insert(ylog);
        } catch (Throwable var3) {
            log.info("insert syslog sync fail", var3);
        }

    }

    /**
     * 重写父类方法
     * 此处做修改: ylog.setUid(uid.toString());
     * @param t
     * @param tag
     * @param source
     * @param msg
     * @param async
     */
    @Override
    public void log(String t, String tag, String source, String msg, boolean async) {
        YlogBean ylog = this.create(t, tag, source, msg);
        try {
            Object uid = GET_USER_ID.call();
            if (uid != null )
                ylog.setUid(uid.toString());
        }
        catch (Exception e) {
            log.debug("get user id fail", e);
        }
        if (async)
            async(ylog);
        else
            sync(ylog);
    }

    /**
     * 改为从Session中获取uid
     */
	public static Callable<Object> GET_USER_ID = new Callable<Object>() {
        public String call() throws Exception {
            Object uid;
            try {
                uid = Mvcs.getSessionAttrSafe("uid");
            } catch (Throwable e) {
                return null;
            }
            if (uid != null) {
                return uid.toString();
            }
            return null;
        };
    };

    /**
     * 重写父类方法
     * 获取特定月份的Dao实例
     * 修改为:以YlogBean建表
     * @param key
     * @return
     */
    @Override
    protected Dao dao(String key) {
        Dao dao = ymDaos.get(key);
        if (dao == null) {
            synchronized (this) {
                dao = ymDaos.get(key);
                if (dao == null) {
                    dao = Daos.ext(this.dao, key);
                    dao.create(YlogBean.class, false);
                    ymDaos.put(key, dao);
                }
            }
        }
        return dao;
    }

   
}

还有个疑问:ColType这个枚举好像比较偏MySql, 能不能支持一下Oracle,比如Clob,Blog, Long...遇到这几种就只能手动操作了。

4 回复

username是String哦
长度我改改吧...

ColType里面

    /**
     * 长文本,对应 Clob
     */
    TEXT,

    /**
     * 二进制,对应 Blob
     */
    BINARY,

没想到会发到分享区,2天没看到,我的锅。。。

添加回复
请先登陆
回到顶部