logalpha.handler
The base Handler class should be considered an abstract class. If deriving directly from the base class you should implement both the format() and the write(). methods.
Handler
format()
write()
Core message handling interface. A Handler associates a level with a resource.
Level
The level for this handler.
Any
Some resource to publish messages to.
write
Publish message to resource after calling format.
format
Format message.
A minimum viable implementation is provided in StreamHandler. This handler wants a file-like resource to write to. It’s write() method literally calls print() with the resource as the file.
StreamHandler
print()
Bases: logalpha.handler.Handler
logalpha.handler.Handler
Publish messages to a file-like resource.
The level for this handler (default: WARNING).
WARNING
File-like resource to write to (default: sys.stderr).
sys.stderr
Returns message.content.
message.content
The StreamHandler class implements everything needed for messages to be published to stderr or some other file-like object. To customize formatting, extend the class by overriding the format() method. Just for formatting this seems like a lot of boilerplate; however, by making it a function call it’s possible to inject any arbitrary code.
@dataclass class MyHandler(StreamHandler): """A :class:`~StreamHandler` with custom formatting.""" def format(self, message: Message) -> str: return f'{message.level.name} {message.content}'
logalpha.message
logalpha.logger