在项目中的约定this
生产环境禁止直接使用 System.out 或 System.err 输出日志或使用 e.printStackTrace() 打印异常堆栈. 因为标准日志输出与标准错误输出文件每次 Jboss 重启时才滚动, 若是大量输出送往这两个文件, 容易形成文件大小超过操做系统大小限制.操作系统
输出的 POJO 类建议重写 toString 方法,有利于在debugger过程当中,查看对象信息.debug
能够使用 warn 日志级别来记录用户输入参数错误的状况, 避免用户投诉时, 无所适从. 注意日志输出的级别,error 级别只记录系统逻辑出错、异常、或者重要的错误信息. 如非必要, 请不要在此场景打出 error 级别, 避免频繁报警.日志
异常和日志code
反例:抛出异常,则不须要记录logger,交给相应的ExceptionHandler记录日志信息对象
try { service.start(); }catch(Exception e){ logger.error("error Msg :{}", e.getMessage()); throw new RuntimeException("error Msg", e.getMessage()); }
正例:抛出异常,记录出错信息的堆栈信息,exception要放在参数末尾get
try { service.start(); }catch(Exception e){ logger.info("error Msg :{}",ex.getMessage(), e); } //参数列表中包含异常,异常会将堆栈信息打印出来 private void initThrowable(final Object[] params, final int usedParams) { if (params != null) { final int argCount = params.length; if (usedParams < argCount && this.throwable== null && params[argCount - 1] instanceof Throwable){ this.throwable = (Throwable) params[argCount - 1]; } } }