Hakisto
The name: Hakisto means Logger in Esperanto.
Logging reimagined…
TL;DR
The following gives some background and explains the why of Hakisto. If you want to get going, feel free to skip to Quick Start.
I use logging all the time and it works, especially together with logzero. I recommend to everybody to use logging in their programs, too.
But…
Timestamps are important. And while I am on this topic, a timestamp should always be UTC!
I usually work with different systems that communicate with each other so I need to be able to look for information during similar times (or time-ranges).
Logs should be available for a reasonable time.
Users don’t always tell you immediately that something didn’t work as expected, rather “I had that problem last week…”.
Colors improve readability and allow better understanding at a glance.
logzero does a great job, anything aiming to improve on it, should have colors by default as well.
More logging levels (or severities)
I work a lot with SAP® and there is a message type Success, a bit different from
INFO.Sometimes I (have to) go a little crazy and start to log almost every other line, way to much for
DEBUG.
Ease of use
I like the approach of logzero to make a logger available with just one import, but still have to change some configuration, because it doesn’t fit completely.
Extendability
The core implementation carries a lot of Python 2 related code and is a bit convoluted to cover different paradigms from way back. This makes it hard to use a object-oriented approaches to change or improve the existing solution. At the time of writing, Python 3.8 has reached it’s end-of-live and Python 3.13 has been released, I don’t consider Python 2 compatibility a factor anymore.
Unfortunately Development of logzero has ended in November 2022.
So…
Here is a redesign featuring:
Object-orientation to allow consistent extension and adaption.
Ease of Use - again just one import.
Sensible defaults
At least for me, a colorful output at the console and rotating files (2MB, 10 generations), but if you like it different, just inherit the class and change a few lines.
Colors
Additional severities - now there are eight of them.
Severity
Description
TRACE
More information than regular DEBUG (e.g. variable values).
DEBUG
Debugging (technical) information.
VERBOSE
Additional (non-technical) information.
INFO
Information.
SUCCESS
Successful execution.
WARNING
Warning that might require attention.
ERROR
Significant issue. Processing might continue.
CRITICAL
Critical issue. Usually processing is aborted.
Automatic Exception Handling
Exception get logged by default, including the respective source code and local variables.
Decisions and Reasons
I always ask myself why somebody (or even myself years ago) decided to implement something this way, and not differently. Without any claim of completeness you might be interested in the following.
Timestamp
Timestamps use a 2 digit year, followed by month and day and the 24-hour time. A Z is appended to declare the
timezone unambiguously.
yy-mm-dd HH:MM:SSZ (e.g. 06-01-02 22:04:05Z [1] )
Logs are something for humans to read, so using Epoch doesn’t really work.
While those not living around the same longitude as Greenwich Observatory has a different local time (and even those that do have to struggle with Daylight Saving Time), UTC (Zulu for those with a military, aviation or maritime background) has become the common denominator.
The next turn of the century is 75 years away.
This Timestamp sorts naturally.
Severity Names
The first letter of the severity name is used to classify a record, therefore no first letter should be duplicated. While every severity has a fixed integer value. Please be aware that this value is considered an implementation detail, which might change in the future.
Only names should be used to prevent issues in the future.
Severity |
Integer |
Description |
|---|---|---|
TRACE |
100 |
More information than regular DEBUG (e.g. variable values) |
DEBUG |
200 |
Debugging (technical) information |
VERBOSE |
300 |
Additional (non-technical) information |
INFO |
400 |
Information |
SUCCESS |
500 |
Successful execution |
WARNING |
600 |
Warning that might require attention |
ERROR |
800 |
Significant issue, processing might continue |
CRITICAL |
900 |
Critical issue, usually processing is aborted |
Identifier
An identifier starts and ends with [ and ] respectively.
The continuation mark contains only the severity indicator.
This allows tools to apply color when reviewing log files.
The identifier should identify the message, and should only contain the minimum amount required, while being consistent. The full format contains the parts below, with every part separated by one blank character.
Part |
Format |
Description |
|---|---|---|
Opening and Severity |
|
Marks the beginning of an identifier and the first letter of the severity. |
Date and Time |
YY-MM-DD HH:MM:SSZ |
This is always UTC. |
Message ID |
<message_id> |
Message ID from systems like SAP® or OS/400 or other systems. |
Process Information |
*name:id* |
Process Name and ID when appropriate. Used when Process Name is not |
Thread Information |
(name:id) |
Tread Name and ID when appropriate. Used when Thread Name is not |
Asyncio Task |
‘name’ |
Asyncio Task Name when appropriate. |
Topic |
topic name |
Can be used to identify a specific logger name. |
Closing Mark |
|
Appended without a separating blank character. |
An INFO record could look like
[I 06-01-02 22:04:05Z <SYS0001> *Worker01:3456* (NoteThread:5678) 'task' mymodule]
Additional Information for Severities
While not true for all severities, some messages might benefit from additional information:
The file name, line number and function name in the format used by the standard traceback. This allows IDEs to jump directly to the source and line.
This is added after the provided message (possibly in a separate line) for ERROR and CRITICAL.
In addition, the respective source section including the local variables is shown for CRITICAL.
The Automatic Exception Handling lists source section and local variables for all items in the traceback.
Logger
The Logger is a thin PyPubSub Publisher [2].
The Logger’s purpose is to:
keep the respective topic
determine the correct frame in the call stack
build the
hakisto.Subjectsend the message with the correct severity
logger
The out-of-the-box logger goes to the console and to a file (rotating, 2MB max size with 10 generations).
Mixins
Mixins provide prebuild behavior.
more to come…
While there is no Roadmap right now, I plan to add Handlers and functionality.
Please let me know your ideas!
Indices and tables
Footnotes