需求描述
- 在px事件程序中加入日志打印,方便问题排查和原因分析。
- 日志打印需要支持9.3.6.10最新版和老版本
解决思路
首先必须在Agile中启用PX的日志功能。请参考具体的实现步骤。
注:Agile9.3.6.10之后的版本改用了Log4j2,配置稍有不同。但总体步骤类似。
打印日志有2种实现思路:
- 使用log4j或者log4j2原生方法打印。
- 使用slf4j或者apache-common-logging打印。agile仅支持后面的。
实现步骤
PX日志功能启用
- 修改AgileDomain目录下的log.xml文件.加入日志打印配置。
- 如果不生效可以把weblogic发布包里面的同名文件也一起改掉。
- log4j和log4j2的配置文件格式稍有不同,但改的时候都可以从原来的agilelog配置中复制。如果不知道怎么配的可以联系我。
- 修改日志配置后需要重启Agile应用。
- 在事件程序的项目中引入log4j对应版本的jar包
日志打印代码
在事件程序项目中首先要引入log4j-1.2.15以上版本的jar包。
如果是Agile9.3.6.10以上版本需要引入log4j-api-2.8.2.jar,log4j-core-2.8.2.jar以上版本jar包
在每个事件程序的类中人工编写日志打印语句。代码参考如下:
private static final Logger log = LogManager.getLogger(IActionSupport.class);
public EventActionResult doAction(IAgileSession session, INode node, IEventInfo event) {
long start = System.currentTimeMillis();
IActionParam param = new IActionParam(session, node, event);
ActionResult result = null;
try {
log.info(this.getClass() + ".input:" + param );
result = doEventAction(param);
log.info(this.getClass() + ".output:" + result );
} catch (Exception e) {
log.error(this.getClass() + ".exception.result:" + result, e);
result = new ActionResult(ActionResult.EXCEPTION, e);
} finally {
long end = System.currentTimeMillis();
log.info(this.getClass() + ".time(ms):" + (end - start));
}
return new EventActionResult(event, result);
}
然后将事件程序发布到PLM的extensions目录。注:log4j的jar包不需要加入到目录中,因为Agile已经有log4j-1.2.15.jar!
打开Agile界面执行业务操作。日志打印结果如下:
2021-12-27 16:12:45,513 <ActionServlet:ERROR> No module name specified in the request.
2021-12-27 16:12:55,345 <IActionSupport:INFO> class com.purvar.px.ActionTemplate.input:com.purvar.agile.utils.IActionParam@42dee678
2021-12-27 16:12:55,345 <IActionSupport:INFO> class com.purvar.px.ActionTemplate.output:hello EventAction
2021-12-27 16:12:55,345 <IActionSupport:INFO> class com.purvar.px.ActionTemplate.time(ms):0
2021-12-27 16:32:34,613 <IActionSupport:INFO> class com.purvar.px.ActionTemplate.input, eventClass:class com.agile.px.WFChangeStatusEventInfo, eventName:ECO提交
2021-12-27 16:32:34,613 <IActionSupport:INFO> class com.purvar.px.ActionTemplate.output:hello EventAction
2021-12-27 16:32:34,613 <IActionSupport:INFO> class com.purvar.px.ActionTemplate.time(ms):0