NutzCN Logo
问答 mvn clean package nutzboot:shade nutzboot:war 打包 没有打包进入 我重写的方法 运行报错了
发布于 2010天前 作者 Hamming 1756 次浏览 复制 上一个帖子 下一个帖子
标签:

mvn clean package nutzboot:shade nutzboot:war 打包 没有打包进入 我重写的方法 运行报错了
重写SlogService

package org.nutz.plugins.slog.service;

import com.alibaba.fastjson.JSONObject;
import eu.bitwalker.useragentutils.UserAgent;
import io.nutz.nutzsite.common.utils.AddressUtils;
import io.nutz.nutzsite.common.utils.ShiroUtils;
import io.nutz.nutzsite.common.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.nutz.Nutz;
import org.nutz.aop.interceptor.async.Async;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.el.El;
import org.nutz.lang.Lang;
import org.nutz.lang.segment.CharSegment;
import org.nutz.lang.util.ClassMetaReader;
import org.nutz.lang.util.Context;
import org.nutz.lang.util.MethodParamNamesScaner;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.plugins.slog.bean.SlogBean;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.Callable;

/**
 * @Author: Haimming
 * @Date: 2019-04-28 18:04
 * @Version 1.0
 */
public class SlogService {
    private static final Log log = Logs.get();

    /**
     * 原始Dao实例
     */
    protected Dao dao;

    /**
     * 按月分表的dao实例
     */
    protected Map<String, Dao> ymDaos = new HashMap<String, Dao>();

    /**
     * 异步插入日志
     * @param syslog 日志对象
     */
    @Async
    public void async(Object syslog) {
        this.sync(syslog);
    }

    /**
     * 同步插入日志
     * @param syslog 日志对象
     */
    public void sync(Object syslog) {
        try {
            dao().insert(syslog);
        } catch (Throwable e) {
            log.info("insert syslog sync fail", e);
        }
    }

    public SlogBean c(String t, String tag, String source, String msg) {
        SlogBean sysLog = new SlogBean();
        sysLog.setCt(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 > 3) {
                source = tmp[3].getClassName() + "#" + tmp[3].getMethodName();
            } else {
                source = "main";
            }
        }
        sysLog.setT(t);;
        sysLog.setTag(tag);;
        sysLog.setSource(source);;
        sysLog.setMsg(msg);;
        if (Mvcs.getReq() != null) {
            sysLog.setUrl(Mvcs.getReq().getRequestURI());
            sysLog.setIp(Lang.getIP(Mvcs.getReq()));
            //获取地址
            sysLog.setLocation(AddressUtils.getRealAddressByIP(sysLog.getIp()));
            Map<String, String[]> map = Mvcs.getReq().getParameterMap();
            String params = JSONObject.toJSONString(map);
            //设置参数值
            sysLog.setParam(StringUtils.substring(params, 0, 255));
            UserAgent userAgent = UserAgent.parseUserAgentString(Mvcs.getReq().getHeader("User-Agent"));
            if(Lang.isNotEmpty(userAgent)){
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                sysLog.setOs(os);
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                sysLog.setBrowser(browser);
            }
        }
        return sysLog;
    }

    public void log(String t, String tag, String source, String msg, boolean async) {
        SlogBean slog = c(t, tag, source, msg);
        try {
            slog.setUid(ShiroUtils.getSysUserId());
        }
        catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("get user id fail", e);
            }
        }
        try {
            slog.setUsername(ShiroUtils.getLoginName());
        }
        catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("get user name fail", e);
            }
        }
        if (async) {
            async(slog);
        } else {
            sync(slog);
        }
    }

    /**
     * 获取用户id, 长整型
     */
    public static Callable<Object> GET_USER_ID = new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            Object u;
            try {
                u = SecurityUtils.getSubject().getPrincipal();
            } catch (Throwable e) {
                return 0;
            }
            if (u != null) {
                return u;
            }
            return 0;
        };
    };

    /**
     * 获取用户名称, 字符串类型
     */
    public static Callable<Object> GET_USER_NAME = new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            Object uid = GET_USER_ID.call();
            if (uid != null) {
                return uid;
            }
            return "";
        };
    };

    /**
     * 获取按月分表的Dao实例,即当前日期的dao实例
     * @return
     */
    public Dao dao() {
        Calendar cal = Calendar.getInstance();
        String key = String.format("%d%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
        return dao(key);
    }

    /**
     * 获取特定月份的Dao实例
     * @param key
     * @return
     */
    public 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(SlogBean.class, false);
                    ymDaos.put(key, dao);
                    try {
                        Daos.migration(dao, SlogBean.class, true, false);
                    }
                    catch (Throwable e) {}
                }
            }
        }
        return dao;
    }


    /**
     * 本方法通常由aop拦截器调用.
     * @param t 日志类型
     * @param tag 标签
     * @param source 源码位置
     * @param seg 消息模板
     * @param els 消息模板的EL表达式预处理表
     * @param async 是否异步插入
     * @param args 方法参数
     * @param re 方法返回值
     * @param method 方法实例
     * @param obj 被拦截的对象
     * @param e 异常对象
     */
    public void log(String t, String tag, String source,
                    CharSegment seg, Map<String, El> els,
                    boolean async,
                    Object[] args, Object re, Method method, Object obj,
                    Throwable e) {
        String _msg = null;
        if (seg.hasKey()) {
            Context ctx = Lang.context();
            List<String> names = null;
            if (Nutz.majorVersion() == 1 && Nutz.minorVersion() < 60) {
                Class<?> klass = re.getClass();
                if (klass.getName().endsWith("$$NUTZAOP")) {
                    klass = klass.getSuperclass();
                }
                String key = re.getClass().getName();
                if (caches.containsKey(key)) {
                    names = caches.get(key).get(ClassMetaReader.getKey(method));
                } else {
                    try {
                        Map<String, List<String>> tmp = MethodParamNamesScaner.getParamNames(klass);
                        names = tmp.get(ClassMetaReader.getKey(method));
                        caches.put(key, tmp);
                    }
                    catch (IOException e1) {
                        log.debug("error when reading param name");
                    }
                }
            } else {
                names = MethodParamNamesScaner.getParamNames(method);
            }
            if (names != null) {
                for (int i = 0; i < names.size() && i < args.length; i++) {
                    ctx.set(names.get(i), args[i]);
                }
            }
            ctx.set("obj", obj);
            ctx.set("args", args);
            ctx.set("re", re);
            ctx.set("return", re);
            ctx.set("req", Mvcs.getReq());
            ctx.set("resp", Mvcs.getResp());
            Context _ctx = Lang.context();
            for (String key :seg.keys()) {
                _ctx.set(key, els.get(key).eval(ctx));
            }
            _msg = seg.render(_ctx).toString();
        } else {
            _msg = seg.getOrginalString();
        }
        log(t, tag, source, _msg, async);
    }

    protected static Map<String, Map<String, List<String>>> caches = new HashMap<String, Map<String,List<String>>>();


}

package org.nutz.plugins.slog.bean;

import java.io.Serializable;
import java.util.Date;

import org.nutz.dao.entity.annotation.*;

/**
 * 操作日志记录
 * @author Haiming
 */
@Table("sys_log")
public class SlogBean implements Serializable {

    private static final long serialVersionUID = 4048681972879639280L;

    @Name
    @Column
    @Prev(els={@EL("uuid()")})
    protected String uu32;

    /**
     * aop.before aop.after aop.error
     */
    @Column("t")
    protected String t;

    @Column("tg")
    @Comment("系统模块")
    protected String tag;

    @Column("url")
    @Comment("请求地址")
    @ColDefine(width = 1024)
    protected String url;

    @Column("src")
    @Comment("操作方法")
    @ColDefine(width = 1024)
    protected String source;

    @Column
    @ColDefine(width = 4000)
    @Comment("操作信息")
    protected String msg;

    @Column("u_id")
    @Comment("操作用户")
    protected String uid;

    @Column("u_name")
    protected String username;

    @Column("param")
    @Comment("请求参数")
    @ColDefine(width = 255)
    protected String param;

    @Column("os")
    @Comment("操作系统")
    protected String os;

    @Column("browser")
    @Comment("客户端浏览器")
    protected String browser;

    @Column("ip")
    @Comment("主机地址")
    protected String ip;


    @Column("location")
    @Comment("操作地点")
    protected String location;


    @Column("ct")
    @Comment("操作时间")
    protected Date ct;

    public String getT() {
        return t;
    }

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

    public String getTag() {
        return tag;
    }

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

    public String getSource() {
        return source;
    }

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

    public String getUid() {
        return uid;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getMsg() {
        return msg;
    }

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

    public Date getCt() {
        return ct;
    }

    public void setCt(Date ct) {
        this.ct = ct;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUu32() {
        return uu32;
    }

    public void setUu32(String uu32) {
        this.uu32 = uu32;
    }
}

这个问题 在解决呢

2 回复

jar模式下是否正常

自己的问题 已经处理
感谢

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