This post is opinionated.
Python's default log setup is useless ; it works against the "batteries included" approach we have come to expect.
When I want to write a log message, I want to know when, what level, and what message. I may want it on console, I may want it in a file.
This should be simple - but I end up every time having to look up how to create a full logging utility with custom file handling and string formatting.
It should be as simple as logger = getLogger(), but the default behaviour for some unknown reason is to provide a completely useless formatting, and no
shorthand for a sensible default.
That or I need to download some pip package of unknown provenance, trust that it hasn't been name-hijacked or doing some obfuscated exfilration.
External dependencies are the devil.
This utility allows for a single call with minimal configuration:
from minilog import SimpleLogger
LOG = SimpleLogger(name="mylog", level=SimpleLogger.INFO)
LOG.info("this is useful")Which prints to console:
2024-11-20 10:43:44,567 | WARNING | mylog : this is useful
- The associated code in this gist can just be copied into your project, as-is.
- It's MIT licensed which is equivalent for "do whatever you want".
- It's extremely simple - you can review it yourself
- It can't be name-hijacked - you keep your own copy, not an external dependency
License text included for reference.