NutzCN Logo
问答 syslog 记录错误信息该怎么记录?
发布于 2429天前 作者 zp8821138 1989 次浏览 复制 上一个帖子 下一个帖子
标签:
@Slog(tag="编辑用户", after="用户名={args[0].loginname},新用户信息=${args[0]},编辑结果=${re.msg}")

这个是正常的 ,如果出现错误的时候我该怎么记录了,我想把堆栈错误信息记录到数据库中

@Slog(tag="编辑用户", after="用户名={args[0].loginname},新用户信息=${args[0]},编辑结果=${re.msg}" error="${e}")

这样写貌似没啥效果啊,另外我想把错误专门进行分类便于查找错误改怎么改?

15 回复

注册个el方法,把e传进去,返回堆栈信息

我这样写的不行

@At("/addDo")
	@Ok("json")
	@RequiresPermissions("sys:area:save")
	@Slog(tag="添加地区", after="地区信息=${args[0]},添加结果=${re.msg}", error="错误原因为${e}")
	public Object addDo(@Param("area") Area area) {
		try {
			int i = 1/0;
			if (!Strings.isBlank(area.getName())) {
				Area a = areaService.fetch(Cnd.where("name", "=",area.getName()));
				if (a != null)
					return Result.error("sys.area.duplicate");
			}
			area.setOpBy(getUserId());
			area.setOpAt((int) (System.currentTimeMillis() / 1000));
			areaService.insertArea(area);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

看nutz more中是这样说的
args 方法参数
re 方法返回值,仅 after时可用
e 异常对象,仅error时可用
req 请求对象,仅mvc请求作用域内可用
resp 响应对象,仅mvc请求作用域内可用
方法参数 依赖ecj或jdk8的-paramters编译选项.
是有异常对象e的捏。

是报错还是怎样?

看你的代码, 不像有异常抛出去呢?

int i = 1/0; runtime异常啊 我看了下 因为slog是以前的代码 没有捕获异常,所以没有e,现在更新为最新的,应该没问题。

 public void filter(InterceptorChain chain) throws Throwable {
        if (before != null)
            doLog("aop.before", before, chain, null);
        try {
            chain.doChain();
            if (after != null)
                doLog("aop.after", after, chain, null);
        }
        catch (Throwable e) {
            if (error != null)
                doLog("aop.error", error, chain, e);
            throw e;
        }
    }

doLog("aop.error", error, chain, e); 貌似没有执行 ,我像外抛异常也没有进入filter的catch里面去

进这个filter方法没

进入了filter方法啊 但是不会进入catch里面去

那就是你的方法没抛出异常啊

抛了异常啊

@At("/addDo")
	@Ok("json")
	@RequiresPermissions("sys:area:save")
	@Slog(tag="添加地区", after="地区信息=${args[0]},添加结果=${re.msg}", error="错误原因为${e}")
	public Object addDo(@Param("area") Area area) throws Exception{
		try {
			int i = 1/0;
			if (!Strings.isBlank(area.getName())) {
				Area a = areaService.fetch(Cnd.where("name", "=",area.getName()));
				if (a != null)
					return Result.error("sys.area.duplicate");
			}
			area.setOpBy(getUserId());
			area.setOpAt((int) (System.currentTimeMillis() / 1000));
			areaService.insertArea(area);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

这样写有错吗?

日志信息

java.lang.ArithmeticException: / by zero
	at com.kanq.controller.sys.AreaController.addDo(AreaController.java:72)
	at com.kanq.controller.sys.AreaController$$NUTZAOP._aop_invoke(AreaController.java:2)
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
	at com.kanq.aop.SlogAopInterceptor.filter(SlogAopInterceptor.java:64)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at com.kanq.controller.sys.AreaController$$NUTZAOP.addDo(AreaController.java:1)
	at com.kanq.controller.sys.AreaController$FM$addDo$6c33c9cd284f7825b5134b6e917914d3.invoke(AreaController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:30)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.kanq.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.kanq.processor.NutShiroProcessor.process(NutShiroProcessor.java:47)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.kanq.processor.LogTimeProcessor.process(LogTimeProcessor.java:17)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
[DEBUG] 2018-03-23 17:27:35,251 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:165) - Get 'logService'<class com.kanq.service.sys.LogService>
[DEBUG] 2018-03-23 17:27:35,251 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:191) - 	 >> Load definition name=logService
[DEBUG] 2018-03-23 17:27:35,252 org.nutz.ioc.loader.combo.ComboIocLoader.printFoundIocBean(ComboIocLoader.java:226) - Found IocObject(logService) in AnnotationIocLoader(packages=[com.kanq])
[DEBUG] 2018-03-23 17:27:35,252 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:222) - 	 >> Make...'logService'<class com.kanq.service.sys.LogService>
[DEBUG] 2018-03-23 17:27:35,253 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'logService' to [app] 
[DEBUG] 2018-03-23 17:27:35,254 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:165) - Get 'dao'<>
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:744)
[DEBUG] 2018-03-23 17:27:35,255 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:165) - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
[DEBUG] 2018-03-23 17:27:35,256 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:165) - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
[DEBUG] 2018-03-23 17:27:35,279 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:165) - Get 'dao'<>
[DEBUG] 2018-03-23 17:27:35,293 org.nutz.service.EntityService.<init>(EntityService.java:41) - Get TypeParams for self : com.kanq.model.sys.Log
[DEBUG] 2018-03-23 17:27:35,297 com.kanq.processor.LogTimeProcessor.process(LogTimeProcessor.java:22) - [POST]URI=/sjw/platform/sys/area/addDo 52ms
[DEBUG] 2018-03-23 17:27:35,313 org.nutz.trans.Trans._begain(Trans.java:63) - Start New Transaction id=0, level=2
[DEBUG] 2018-03-23 17:27:35,315 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) - Exception when parser SQL sql, skip cache detect!! SQL=RUN 
[DEBUG] 2018-03-23 17:27:35,315 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - // NOT SQL // ElFieldMacro=uuid()
[DEBUG] 2018-03-23 17:27:35,319 org.nutz.resource.Scans.scan(Scans.java:280) - Found 1 resource by src( org/nutz/conf/NutzDefaultConfig.js ) , regex( \.(js|json)$ )
[DEBUG] 2018-03-23 17:27:35,371 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) - Exception when parser SQL sql, skip cache detect!! SQL=RUN 
[DEBUG] 2018-03-23 17:27:35,371 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - // NOT SQL // ElFieldMacro=$me.uid()
[DEBUG] 2018-03-23 17:27:35,373 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) - Exception when parser SQL sql, skip cache detect!! SQL=RUN 
[DEBUG] 2018-03-23 17:27:35,373 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - // NOT SQL // ElFieldMacro=$me.now()
[DEBUG] 2018-03-23 17:27:35,374 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) - Exception when parser SQL sql, skip cache detect!! SQL=RUN 
[DEBUG] 2018-03-23 17:27:35,374 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - // NOT SQL // ElFieldMacro=$me.flag()
[DEBUG] 2018-03-23 17:27:35,377 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:148) - sql = INSERT INTO sys_log(id,t,tg,src,u_id,u_name,ip,msg,ct,opBy,opAt,delFlag) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) , tables = [sys_log]
[DEBUG] 2018-03-23 17:27:35,383 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - INSERT INTO sys_log(id,t,tg,src,u_id,u_name,ip,msg,ct,opBy,opAt,delFlag) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) 
    |                                1 |         2 |    3 |                                            4 |                                5 |          6 |         7 |                                                                                                                               8 |                   9 |                               10 |         11 |    12 |
    |----------------------------------|-----------|------|----------------------------------------------|----------------------------------|------------|-----------|---------------------------------------------------------------------------------------------------------------------------------|---------------------|----------------------------------|------------|-------|
    | 22c67b1a7e0f4897ae77fbf2a59d8db9 | aop.after | 添加地区 | com.kanq.controller.sys.AreaController#addDo | ce99a527703d438d8a178a517d494d1d | superadmin | 127.0.0.1 | 地区信息=Area [id=null, areaCode=1, parentId=, parentName=null, name=1, layer=1, orderNum=1, disabled=false, remark=null],添加结果=操作失败 | 2018-03-23 17:27:35 | ce99a527703d438d8a178a517d494d1d | 1521797255 | false |
  For example:> "INSERT INTO sys_log(id,t,tg,src,u_id,u_name,ip,msg,ct,opBy,opAt,delFlag) VALUES('22c67b1a7e0f4897ae77fbf2a59d8db9','aop.after','添加地区','com.kanq.controller.sys.AreaController#addDo','ce99a527703d438d8a178a517d494d1d','superadmin','127.0.0.1','地区信息=Area [id=null, areaCode=1, parentId=, parentName=null, name=1, layer=1, orderNum=1, disabled=false, remark=null],添加结果=操作失败','2018-03-23 17:27:35','ce99a527703d438d8a178a517d494d1d',1521797255,false) "
[DEBUG] 2018-03-23 17:27:35,395 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:195) - Clear Cache=sys_log
[DEBUG] 2018-03-23 17:27:35,396 org.nutz.trans.Trans._commit(Trans.java:80) - Transaction Commit id=0
[DEBUG] 2018-03-23 17:27:35,397 org.nutz.trans.Trans._depose(Trans.java:92) - Transaction depose id=0, count=0

被catch了, 拦截器又怎么能catch到呢?

} catch (Exception e) {

这个不try cach我前台又没办法给提示啊。

难道我日志打到service层 然后再controller里面try catch?

是的, 业务逻辑放在service层嘛

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