QuickfixJ如何禁用收发消息日志(保留管理消息与心跳日志)
在QuickfixJ中仅禁用普通收发消息日志(保留Admin消息与心跳)
直接使用ScreenLogFactory构造参数传入false会全局禁用所有收发消息日志,无法区分普通业务消息和Admin/心跳消息。要实现只过滤普通业务消息、保留Admin和心跳日志,需要自定义日志逻辑:
1. 自定义过滤型MessageLog
继承默认的ScreenLog(或FileLog,根据你的日志输出方式),重写onIncoming和onOutgoing方法,只对Admin类型消息(MsgType为0-9)进行日志记录:
public class FilteredScreenLog extends ScreenLog { public FilteredScreenLog(boolean includeIncoming, boolean includeOutgoing, boolean includeEvent) { super(includeIncoming, includeOutgoing, includeEvent); } @Override public void onIncoming(Message message) { try { String msgType = message.getHeader().getString(MsgType.FIELD); // 心跳(MsgType=0)和其他Admin消息(MsgType=1-9)才记录 if (msgType.matches("[0-9]")) { super.onIncoming(message); } } catch (FieldNotFound e) { // 若无法获取MsgType,默认记录(可选:也可以直接跳过) super.onIncoming(message); } } @Override public void onOutgoing(Message message) { try { String msgType = message.getHeader().getString(MsgType.FIELD); if (msgType.matches("[0-9]")) { super.onOutgoing(message); } } catch (FieldNotFound e) { super.onOutgoing(message); } } }
2. 自定义LogFactory
创建自定义的LogFactory,返回上面的过滤型日志实例:
public class FilteredLogFactory implements LogFactory { @Override public Log create(SessionID sessionID) { // 三个参数分别控制:是否记录入站、出站、事件日志,按需调整 return new FilteredScreenLog(true, true, true); } }
3. 替换Session的LogFactory
在初始化QuickfixJ Session时,使用自定义的FilteredLogFactory替代默认的ScreenLogFactory:
// 加载配置文件 SessionSettings settings = new SessionSettings("quickfixj.cfg"); // 你的Application实现 Application app = new YourFixApplication(); // 消息存储工厂(保持原有配置即可) MessageStoreFactory storeFactory = new FileStoreFactory(settings); // 使用自定义日志工厂 LogFactory logFactory = new FilteredLogFactory(); MessageFactory msgFactory = new DefaultMessageFactory(); // 创建Session Session.create(settings, storeFactory, logFactory, msgFactory, app);
这样配置后,普通业务消息(MsgType以A-Z开头,比如新订单单D、执行报告8等)的收发日志会被过滤,只有心跳(0)、测试请求(1)、序列重置(4)等Admin类型消息会被记录。
内容的提问来源于stack exchange,提问作者Ritwika Raha
相关产品推荐
相关产品推荐

