The question is simple, which is meant by Handler . I know that in English Handler is a kind of Handler . But I can't understand the concept behind it, if it follows some design pattern or something like that. Whether it's synchronous or asynchronous, etc.
My question arises from using Python logging . In which several Handlers can be configured to send the logs to different sites.
'handlers': {
'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
},
'info_file_handler': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/info.log',
'formatter': 'default',
'maxBytes': 485760,
'backupCount': 5,
'encoding': 'utf8'
}
If someone has a good explanation and example, welcome. There is no tag on handler (and I can't create it due to low reputation).
A
Handler
is a fairly ambiguous concept in the sense that there is no univocal definition of what it is, nor how it should be built, only speaking of Python , in the documentation there is no precise definition and the termHandler
is used in many classes and objects without get to specify a single pattern, at most we can get to generalize what you already know, that aHandler
is a "handler", something that is capable of "receiving" an event, a message, etc. and act according to it.But in the context of the module
logging
we can specify a little more than what aHandler
, let's first see how an information log works:Logger
, an object with which we can define multiple messages to "log" during the execution of our scripts.warning()
,error()
,log()
etc. They all end up calling an inner function_log()
._log()
constructs the "record" that we want to save and finally calls thecallHandlers()
.callHandlers()
, as its name indicates, iterates through theHandlers
previously configured list and calls the methodhandle()
.It is important to note that with it
Logger
we abstract from the "where" and the "how" the messages will be recorded, that is why we have theHandler
, with some characteristics:logging.Handler
logging.Handler
has a fundamental method that ishandle()
, allHandlers
inherit this method, which ends up invoking another, not implemented in this class calledemit()
.emit()
it is the method that eachHandler
inherited from should implementlogging.Handler
and that encapsulates the final logic to treat the log record, to save it in a certain file, to send it over the network, to register it as a system event, etc.emit()
andHandler
ultimately, they are the ones that manage the "where" and "how" the messages will be recorded.With this context we could say that a
Handler
is an object that is in charge of a specific task, which also has a certain interface, to be invoked by a hierarchically superior object that "delegates" to it, part of the work, either for a delegation reason. of responsibilities or to promote functional extensibility.As an example, I add the definition of the
Handler
most basic and elemental elNullHandler
:You can see that only the aforementioned methods
handle()
and are defined (blank)emit()
. Note: thiscreateLock()
is to re-define the standard locking mechanismHandler
since in this case no locking of any kind is needed.