diff --git a/pom.xml b/pom.xml index 7285fa33d..c022bc2fd 100644 --- a/pom.xml +++ b/pom.xml @@ -91,11 +91,6 @@ nutz-plugins-wkcache ${nutz-version} - - org.nutz - nutz-plugins-views - ${nutz-version} - org.nutz nutzwx @@ -440,8 +435,8 @@ - nutzcn-snapshots - https://jfrog.nutz.cn/artifactory/snapshots/ - + nutzcn-snapshots + https://jfrog.nutz.cn/artifactory/snapshots/ + \ No newline at end of file diff --git a/wk-app/wk-web/src/main/java/cn/wizzer/app/sys/modules/models/Sys_log.java b/wk-app/wk-web/src/main/java/cn/wizzer/app/sys/modules/models/Sys_log.java index 6616a9cc5..c433438c9 100644 --- a/wk-app/wk-web/src/main/java/cn/wizzer/app/sys/modules/models/Sys_log.java +++ b/wk-app/wk-web/src/main/java/cn/wizzer/app/sys/modules/models/Sys_log.java @@ -2,15 +2,16 @@ import cn.wizzer.framework.base.model.BaseModel; import cn.wizzer.framework.util.StringUtil; -import org.apache.shiro.SecurityUtils; import org.nutz.dao.entity.annotation.*; +import org.nutz.lang.Lang; +import org.nutz.mvc.Mvcs; import java.io.Serializable; /** * Created by wizzer on 2016/6/21. */ -@Table("sys_log") +@Table("sys_log_${month}") public class Sys_log extends BaseModel implements Serializable { private static final long serialVersionUID = 1L; @Column @@ -52,6 +53,16 @@ public class Sys_log extends BaseModel implements Serializable { @ColDefine(type = ColType.TEXT) private String msg; + @Column + @Comment("请求结果") + @ColDefine(type = ColType.TEXT) + private String param; + + @Column + @Comment("执行结果") + @ColDefine(type = ColType.TEXT) + private String result; + public long getId() { return id; } @@ -108,10 +119,26 @@ public void setMsg(String msg) { this.msg = msg; } - public static Sys_log c(String type, String tag, String msg, String source) { + public String getParam() { + return param; + } + + public void setParam(String param) { + this.param = param; + } + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + + public static Sys_log c(String type, String tag, String source, String msg, String param, String result) { Sys_log sysLog = new Sys_log(); - if (type == null || tag == null || msg == null) { - throw new RuntimeException("type/tag/msg can't null"); + if (type == null || tag == null) { + throw new RuntimeException("type/tag can't null"); } if (source == null) { StackTraceElement[] tmp = Thread.currentThread().getStackTrace(); @@ -122,16 +149,19 @@ public static Sys_log c(String type, String tag, String msg, String source) { } } - sysLog.type = type; - sysLog.tag = tag; - sysLog.src = source; - sysLog.msg = msg; - sysLog.ip = StringUtil.getRemoteAddr(); - String uid = StringUtil.getUid(); - String username = StringUtil.getUsername(); - sysLog.setOpBy(uid); + sysLog.setType(type); + sysLog.setTag(tag); + sysLog.setSrc(source); + sysLog.setMsg(msg); + sysLog.setParam(param); + sysLog.setResult(result); + if (Mvcs.getReq() != null) { + sysLog.setIp(Lang.getIP(Mvcs.getReq())); + } + sysLog.setOpBy(StringUtil.getUid()); sysLog.setOpAt((int) (System.currentTimeMillis() / 1000)); - sysLog.username = username; + sysLog.setDelFlag(false); + sysLog.setUsername(StringUtil.getUsername()); return sysLog; } } diff --git a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogAopInterceptor.java b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogAopInterceptor.java index fdf06b525..5b1beb2df 100644 --- a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogAopInterceptor.java +++ b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogAopInterceptor.java @@ -1,6 +1,5 @@ package cn.wizzer.app.web.commons.slog; -import cn.wizzer.app.sys.modules.models.Sys_log; import cn.wizzer.app.web.commons.slog.annotation.SLog; import org.nutz.aop.InterceptorChain; import org.nutz.aop.MethodInterceptor; @@ -9,6 +8,8 @@ import org.nutz.lang.Lang; import org.nutz.lang.segment.CharSegment; import org.nutz.lang.util.Context; +import org.nutz.log.Log; +import org.nutz.log.Logs; import org.nutz.mvc.Mvcs; import java.lang.reflect.Method; @@ -19,22 +20,22 @@ * Created by wizzer on 2016/6/22. */ public class SLogAopInterceptor implements MethodInterceptor { + private static final Log log = Logs.get(); protected SLogService sLogService; protected String source; + protected String type; protected String tag; protected CharSegment msg; - protected boolean before; - protected boolean after; - protected boolean error; + protected boolean param; + protected boolean result; protected boolean async; protected Map els; protected Ioc ioc; public SLogAopInterceptor(Ioc ioc, SLog slog, Method method) { - this.ioc = ioc; this.msg = new CharSegment(slog.msg()); if (msg.hasKey()) { els = new HashMap(); @@ -42,28 +43,25 @@ public SLogAopInterceptor(Ioc ioc, SLog slog, Method method) { els.put(key, new El(key)); } } + this.param = slog.param(); + this.result = slog.result(); + this.ioc = ioc; this.source = method.getDeclaringClass().getName() + "#" + method.getName(); this.tag = slog.tag(); SLog _s = method.getDeclaringClass().getAnnotation(SLog.class); if (_s != null) { this.tag = _s.tag() + "," + this.tag; } + this.type = slog.type(); this.async = slog.async(); - this.before = slog.before(); - this.after = slog.after(); - this.error = slog.error(); } public void filter(InterceptorChain chain) throws Throwable { - if (before) - doLog("aop.before", chain, null); try { chain.doChain(); - if (after) - doLog("aop.after", chain, null); + doLog("aop.after", chain, null); } catch (Throwable e) { - if (error) - doLog("aop.after", chain, e); + doLog("aop.error", chain, e); throw e; } } @@ -84,12 +82,25 @@ protected void doLog(String t, InterceptorChain chain, Throwable e) { } else { _msg = msg.getOrginalString(); } - Sys_log sysLog = Sys_log.c(t, tag, _msg, source); if (sLogService == null) sLogService = ioc.get(SLogService.class); - if (async) - sLogService.async(sysLog); - else - sLogService.sync(sysLog); + try { + sLogService.log(t, + type, + tag, + source, + _msg, + els, + param, + result, + async, + chain.getArgs(), + chain.getReturn(), + chain.getCallingMethod(), + chain.getCallingObj(), + e); + } catch (Exception e1) { + log.debug("slog fail", e1); + } } } \ No newline at end of file diff --git a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogService.java b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogService.java index 31bd6bd0b..861140702 100644 --- a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogService.java +++ b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/SLogService.java @@ -2,11 +2,19 @@ import cn.wizzer.app.sys.modules.models.Sys_log; import org.nutz.dao.Dao; +import org.nutz.dao.util.Daos; +import org.nutz.el.El; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; +import org.nutz.json.Json; import org.nutz.log.Log; import org.nutz.log.Logs; +import java.lang.reflect.Method; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; @@ -15,7 +23,7 @@ /** * Created by wizzer on 2016/6/22. */ -@IocBean(create="init", depose="close") +@IocBean(create = "init", depose = "close") public class SLogService implements Runnable { private static final Log log = Logs.get(); @@ -26,7 +34,52 @@ public class SLogService implements Runnable { @Inject protected Dao dao; + /** + * 按月分表的dao实例 + */ + protected Map ymDaos = new HashMap(); + /** + * 获取按月分表的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(Sys_log.class, false); + ymDaos.put(key, dao); + try { + Daos.migration(dao, Sys_log.class, true, false); + } catch (Throwable e) { + } + } + } + } + return dao; + } + + /** + * 异步插入日志 + * + * @param syslog 日志对象 + */ public void async(Sys_log syslog) { LinkedBlockingQueue queue = this.queue; if (queue != null) @@ -39,9 +92,14 @@ public void async(Sys_log syslog) { } } + /** + * 同步插入日志 + * + * @param syslog 日志对象 + */ public void sync(Sys_log syslog) { try { - dao.fastInsert(syslog); + dao().fastInsert(syslog); } catch (Throwable e) { log.info("insert syslog sync fail", e); } @@ -63,6 +121,49 @@ public void run() { } } + /** + * 本方法通常由aop拦截器调用. + * + * @param t 日志类型 + * @param tag 标签 + * @param source 源码位置 + * @param els 消息模板的EL表达式预处理表 + * @param actionParam 是否异步插入 + * @param methodReturn 是否异步插入 + * @param async 是否异步插入 + * @param args 方法参数 + * @param re 方法返回值 + * @param method 方法实例 + * @param obj 被拦截的对象 + * @param e 异常对象 + */ + public void log(String t, String type, String tag, String source, String msg, + Map els, boolean param, boolean result, + boolean async, + Object[] args, Object re, Method method, Object obj, + Throwable e) { + String _param = null; + String _result = null; + if (param) { + _param = Json.toJson(args); + } + if (result) { + _result = Json.toJson(re); + } + log(type, tag, source, msg, async, _param, _result); + } + + + public void log(String type, String tag, String source, String msg, boolean async, String param, String result) { + Sys_log slog = Sys_log.c(type, tag, source, msg, param, result); + if (async) + async(slog); + else + sync(slog); + } + + protected static Map>> caches = new HashMap>>(); + public void init() { queue = new LinkedBlockingQueue(); int c = Runtime.getRuntime().availableProcessors(); diff --git a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/annotation/SLog.java b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/annotation/SLog.java index a55dd9022..32a443d69 100644 --- a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/annotation/SLog.java +++ b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/commons/slog/annotation/SLog.java @@ -7,15 +7,34 @@ @Documented @Inherited public @interface SLog { + String type() default "platform"; + /** + * 标签 + * + * @return + */ String tag(); - String msg(); + String msg() default ""; - boolean before() default false; + /** + * 是否记录传递参数 + * + * @return 消息模板 + */ + boolean param() default false; - boolean after() default true; - - boolean error() default false; + /** + * 记录执行结果 + * + * @return 消息模板 + */ + boolean result() default false; + /** + * 是否异步执行,默认为true + * + * @return true, 如果需要异步执行 + */ boolean async() default true; } \ No newline at end of file diff --git a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/modules/controllers/platform/sys/SysLogController.java b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/modules/controllers/platform/sys/SysLogController.java index ba46a244c..9b7c2ac90 100644 --- a/wk-app/wk-web/src/main/java/cn/wizzer/app/web/modules/controllers/platform/sys/SysLogController.java +++ b/wk-app/wk-web/src/main/java/cn/wizzer/app/web/modules/controllers/platform/sys/SysLogController.java @@ -1,19 +1,28 @@ package cn.wizzer.app.web.modules.controllers.platform.sys; +import cn.wizzer.app.sys.modules.models.Sys_log; import cn.wizzer.app.sys.modules.services.SysLogService; +import cn.wizzer.app.sys.modules.services.impl.SysLogServiceImpl; import cn.wizzer.app.web.commons.slog.annotation.SLog; import cn.wizzer.framework.base.Result; import cn.wizzer.framework.page.datatable.DataTableColumn; import cn.wizzer.framework.page.datatable.DataTableOrder; +import cn.wizzer.framework.util.DateUtil; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.nutz.dao.Cnd; +import org.nutz.dao.util.Daos; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; +import org.nutz.lang.Strings; +import org.nutz.lang.Times; import org.nutz.log.Log; import org.nutz.log.Logs; -import org.nutz.mvc.annotation.*; +import org.nutz.mvc.annotation.At; +import org.nutz.mvc.annotation.Ok; +import org.nutz.mvc.annotation.Param; import javax.servlet.http.HttpServletRequest; +import java.util.Date; import java.util.List; /** @@ -29,16 +38,31 @@ public class SysLogController { @At("") @Ok("beetl:/platform/sys/log/index.html") @RequiresPermissions("sys.manager.log") - public void index() { - + public void index(HttpServletRequest req) { + req.setAttribute("today", DateUtil.getDate()); } @At @Ok("json:full") @RequiresPermissions("sys.manager.log") - public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List order, @Param("::columns") List columns) { + public Object data(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("type") String type, @Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List order, @Param("::columns") List columns) { Cnd cnd = Cnd.NEW(); - return sysLogService.data(length, start, draw, order, columns, cnd, null); + String tableName = Times.format("yyyyMM", new Date()); + if (Strings.isNotBlank(beginDate)) { + tableName = Times.format("yyyyMM", Times.D(beginDate + " 00:00:00")); + cnd.and("opAt", ">=", DateUtil.getTime(beginDate + " 00:00:00")); + } + if (Strings.isNotBlank(endDate)) { + cnd.and("opAt", "<=", DateUtil.getTime(endDate + " 23:59:59")); + } + if (Strings.isNotBlank(type)) { + cnd.and("type", "=", type); + } + SysLogService sysLogService2 = new SysLogServiceImpl(Daos.ext(sysLogService.dao(), tableName)); + if (sysLogService2.dao().exists(Sys_log.class)) { + return sysLogService2.data(length, start, draw, order, columns, cnd, null); + } else + return sysLogService.data(length, start, draw, order, columns, cnd, null); } @At diff --git a/wk-app/wk-web/src/main/resources/config/custom/redis.properties b/wk-app/wk-web/src/main/resources/config/custom/redis.properties index 18cbcba25..8a7070027 100644 --- a/wk-app/wk-web/src/main/resources/config/custom/redis.properties +++ b/wk-app/wk-web/src/main/resources/config/custom/redis.properties @@ -7,4 +7,4 @@ redis.maxTotal=100 #redis.password=wizzer #redis集群模式设置 redis.mode=cluster redis.mode=normal -#redis.nodes= \ No newline at end of file +#redis.nodes=192.168.0.101:6379,192.168.0.102:6379 \ No newline at end of file diff --git a/wk-app/wk-web/src/main/webapp/WEB-INF/views/platform/sys/log/index.html b/wk-app/wk-web/src/main/webapp/WEB-INF/views/platform/sys/log/index.html index 32986fe56..74ab8dd7b 100644 --- a/wk-app/wk-web/src/main/webapp/WEB-INF/views/platform/sys/log/index.html +++ b/wk-app/wk-web/src/main/webapp/WEB-INF/views/platform/sys/log/index.html @@ -2,23 +2,51 @@ layout("/layouts/platform.html"){ %> -