How to log Zinc events using Beacon
Zinc is a framework to deal with the HTTP networking protocol. This page gives details on how to log Zinc events.
Below is an example of a Zinc request with logging enabled, for experimenting.
rawData := ZnClient new loggingOn; get: ('https://raw.githubusercontent.com/feenkcom/gtoolkit/v0.8.1837/.baseline-metadata.ston')
Logging overview
Zinc models log events through subclasses of ZnLogEvent
; specific type of events are modeled using dedicated subclasses, like ZnClientRetryingEvent
.
ZnClient
instances can enabled logging using:
- ZnClient>>#loggingOn
for enabling all log events, or using
- ZnClient>>#logLevel:
for controling the log level.
The class side of ZnLogEvent
maintains a singleton announcer, accessed through ZnLogEvent>>#announcer
, that is used to announce all log events. Consumers interested in log events can register with that announcer.
ZnLogEvent announcer when: ZnLogEvent do: [ :event | self inform: 'Zinc Log: ', event printString ].
This is a global announcer. The subscription remains there until explicitly removed.
A different subclass of ZnLogEvent
can be used to listen only for events of a given type.
Direct logging to transcript
The simples way to log all Zinc events is using ZnLogEvent>>#logToTranscript
. That registers a consumer for logging announcements and prints them to the transcript. It does not use Beacon, but directly prints announcements.
ZnLogEvent logToTranscript
Direct logging with Beacon
The first and simplest way to use Beacon with Zinc log events is through ZnLogEvent>>#logToBeacon
. This captures all log events raised by Zinc and converts them to Beacon signals, by wrapping them in an instance of ZnLogEventSignal
.
ZnLogEvent logToBeacon.
We can the work with any Beacon logger to handle these signals; for example a MemoryLogger
to save the signals in memory, or a NonInteractiveTranscriptLogger
logger do directly print siglans to stdout
.
MemoryLogger startFor: ZnLogEventSignal.
To filter for particular types of announcements we can use a custom condition:
MemoryLogger startFor: (ZnLogEventSignal where: [ :aSignal | aSignal target isKindOf: ZnClientLogEvent]).
MemoryLogger stop
MemoryLogger reset.
MemoryLogger instance
Using a custom Beacon
The second, and more complex way to handle Zinc log events using Beacon, is to create a new Beacon
instance that directly listens for announcement announced by ZnLogEvent>>#announcer
.
In this case the log announcements from Zinc are directly used as Beacon signals without any conversion.
beacon := Beacon new announcer: ZnLogEvent announcer
The logger that we use next needs to explicitly listed to the Beacon instance we previously created.
logger := MemoryLogger new beacon: beacon; startFor: ZnClientLogEvent
logger stop.
logger reset