quickfixgo / quickfix

The Go FIX Protocol Library :rocket:
https://www.quickfixgo.org/
Other
735 stars 288 forks source link

Added log factory for zap. #536

Closed pilotso11 closed 1 year ago

pilotso11 commented 1 year ago

ZAP logger for quickfix

To use:

...
// Initialize zap
zapCfg := zap.NewDevelopmentConfig()
zapCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder  // colourize levels
Atom := zapCfg.Level   // use Atom to turn on/off log levels if you need to
logger, _ := zapCfg.Build()
zap.ReplaceGlobals(logger)
...
logFactory := quickfix.NewZapLogFactory("FIX_", true)  // FIX_ prefix and message logging enabled
acceptor, err := quickfix.NewAcceptor(app, messageStoreFactory, appSettings, logFactory)
...

Uses ZAP (https://pkg.go.dev/go.uber.org/zap), a very low latency logger for golang which supports:

This wrapper passes all the quickfix logging directly to zap at INFO level.

It sets a named logger for each session with Prefix+Sender:Target as the label. Setting prefix to "" results in the message being labeled with just the sender and target.

For example if the Sender is SEND and the target is TARG and the prefix is FIX_ then the name will be FIX_SEND:TARG.

There is also an option to enable or disable detailed message logging. If message logging is enabled, EOM field separators are replaced with | for readability.

With no prefix you will see log messages similar to this using console/debug logging.

2022-12-28T00:59:05.023Z        INFO    FROMCOMP:TOCOMP     quickfix@v0.6.1/session_state.go:69       incoming <-:  8=FIXT.1.1|9=76|35=CW|34=32|49=TOCOMP|52=20221228-00:59:05.022921|56=FROMCOMP|117=ID09876|297=0|10=187|

And with json logging

{"level":"info","ts":1672189226.004965,"logger":"FROMCOMP:TOCOMP","caller":"quickfix@v0.6.1/session.go:355","msg":"outgoing ->: 8=FIXT.1.1|9=81|35=A|34=1|49=FROMCOMP|52=20221228-01:00:26.004921|56=TOCOMP|98=0|108=30|141=Y|1137=9|10=039|"}

Tests have been added. Unfortunately without completely mocking the logger it is difficult to test the actual logging functions, but as they are passthroughs to ZAP there are unlikely to be any regression errors in this factory and logger.


Possible future improvements:

ackleymi commented 1 year ago

Hi, I appreciate the PR. Your integration of zap into FIX logging I'm sure would be quite useful, however, I'm not positive I want to support zap integration out-of-the-box with this project and add all the zap deps. Rather, I'd leave it up to users to implement the FIX logging interface however they wish, including any log pkg they want. In the future I hope to have some FIX logging examples in a demo repo - I'll refer back to this + credit you at that time! Closing this for now.