Skip to content

Commit

Permalink
update: @Slog 注解新增两个参数配置记录请求参数和执行结果,对应修改了系统日志查询功能 & redis参数配置增加了示例说明
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizzercn committed Nov 10, 2017
1 parent b1b9adb commit a19df1f
Show file tree
Hide file tree
Showing 35 changed files with 1,869 additions and 67 deletions.
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@
<artifactId>nutz-plugins-wkcache</artifactId>
<version>${nutz-version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz-plugins-views</artifactId>
<version>${nutz-version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzwx</artifactId>
Expand Down Expand Up @@ -440,8 +435,8 @@
</build>
<distributionManagement>
<snapshotRepository>
<id>nutzcn-snapshots</id>
<url>https://jfrog.nutz.cn/artifactory/snapshots/</url>
</snapshotRepository>
<id>nutzcn-snapshots</id>
<url>https://jfrog.nutz.cn/artifactory/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -19,51 +20,48 @@
* 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<String, El> 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<String, El>();
for (String key : msg.keys()) {
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;
}
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -26,7 +34,52 @@ public class SLogService implements Runnable {

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

/**
* 获取按月分表的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<Sys_log> queue = this.queue;
if (queue != null)
Expand All @@ -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);
}
Expand All @@ -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<String, El> 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<String, Map<String, List<String>>> caches = new HashMap<String, Map<String, List<String>>>();

public void init() {
queue = new LinkedBlockingQueue<Sys_log>();
int c = Runtime.getRuntime().availableProcessors();
Expand Down
Loading

0 comments on commit a19df1f

Please sign in to comment.