这里还遇到一个问题,我的LogChainMaker如下:
public class LogChainMaker implements ActionChainMaker {
@Override
public ActionChain eval(NutConfig config, ActionInfo ai) {
// 提醒: config可以获取ioc等信息, ai可以获取方法上的各种配置及方法本身
// 正常处理的列表
List<Processor> list = new ArrayList<>();
list.add(new LogTimeProcessor()); // 设置日志处理类
list.add(new UpdateRequestAttributesProcessor()); // 设置base/msg等内置属性
list.add(new EncodingProcessor()); // 设置编码信息@Encoding
list.add(new ModuleProcessor()); // 获取入口类的对象,从ioc或直接new
list.add(new ActionFiltersProcessor()); // 处理@Filters
list.add(new AdaptorProcessor()); // 处理@Adaptor
list.add(new MethodInvokeProcessor()); // 执行入口方法
list.add(new ViewProcessor()); // 对入口方法进行渲染@Ok
for (Processor p : list) {
try {
p.init(config, ai);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
// 最后是专门负责兜底的异常处理器,这个处理器可以认为是全局异常处理器,对应@Fail
Processor error = new ErrorProcessor();
try {
error.init(config, ai);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return new NutActionChain(list, error, ai);
}
}
LogTimeProcessor如下:
public class LogTimeProcessor extends AbstractProcessor {
private static final Log log = Logs.get();
@Override
public void process(ActionContext ac) throws Throwable {
Stopwatch sw = Stopwatch.begin();
try {
doNext(ac);
} finally {
sw.stop();
HttpServletRequest req = ac.getRequest();
String ip = Lang.getIP(req);
String path = ac.getPath();
String result = Json.toJson(ac.getMethodReturn(), JsonFormat.tidy());
switch (req.getMethod().trim()) {
case "GET":
log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "GET", ip, path, getParameters(req), result, sw.getDuration());
break;
case "POST":
if (req.getAttribute("req_xml") != null) {
log.infof("[%s] [clientIP:%s] [url:%s] params:%s result:%s time:%sms", "POST XML", ip, path, req.getAttribute("req_xml"), result, sw.getDuration());
} else if (req.getContentType().contains("json")) {
log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "POST JSON", ip, path, Json.toJson(ac.getMethodArgs(), JsonFormat.tidy()), result, sw.getDuration());
} else {
log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "POST FORM", ip, path, getParameters(req), result, sw.getDuration());
}
break;
}
}
}
private String getParameters(HttpServletRequest req) {
// 按json格式返回参数
// return Json.toJson(req.getParameterMap(), JsonFormat.tidy());
// 这里是按pv模式组织参数
List<String> params = new ArrayList<>();
Map map = req.getParameterMap();
for (Object o : map.keySet()) {
params.add(o.toString() + "=" + req.getParameter(o.toString()));
}
return Strings.join("&", params);
}
}
在出ResultException异常的情况下,我的Log就捕获不到ac.getMethodReturn()了