baselog
baselog is a Python module intended to simplify and standardize logging in our Python applications, particularly Dockerized applications. It is simply a thin wrapper around the stdlib logging module.
Many of our codebases are containerized and have the following operational requirements:
- Log messages get logged to the standard error (in agreement with the 12 factor design principles)
- If a
log_dirpath is supplied, a new log file is also created in that directory for every run of the app - All uncaught exceptions are logged as described above
- Standard timezone-aware formats for log messages and timestamps
Example
In the main file in an application, BaseLog is used to initialize both the console and file loggers.
#!/usr/bin/env python3
from baselog import BaseLog
from .util import otherfunc
logger = BaseLog(
__package__,
log_dir="/logs",
console_log_level="DEBUG",
file_log_level="DEBUG",
)
def main():
logger.info("starting a thing")
otherfunc()
if __name__ == '__main__':
main()
In other files of the project you just import the standard python logging module and get a logger.
#!/usr/bin/env python3
import logging
logger = logging.getLogger(__name__)
def otherfunc():
logger.warning("Log 'em'!")
A sys.excepthook is set by BaseLog, so in the case of an uncaught exception, the exception and traceback are logged to the console and log files:
2023-04-11T20:06:59+0000 - logdemo - INFO - starting a thing
2023-04-11T20:06:59+0000 - logdemo.util - WARNING - Things may be happening
2023-04-11T20:06:59+0000 - logdemo.other.funcs - INFO - Things aren't happening yet
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - uncaught exception: RuntimeError; What the heck?!
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-000: Traceback (most recent call last):
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-001: File "<frozen runpy>", line 198, in _run_module_as_main
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-002: File "<frozen runpy>", line 88, in _run_code
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-003: File "/app/logdemo/__main__.py", line 21, in <module>
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-004: main()
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-005: File "/app/logdemo/__main__.py", line 17, in main
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-006: whatsit()
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-007: File "/app/logdemo/other/funcs.py", line 10, in whatsit
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-008: raise RuntimeError("What the heck?!")
2023-04-11T20:06:59+0000 - logdemo - CRITICAL - traceback-009: RuntimeError: What the heck?!
Metadata
Release files for baselog 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| baselog-2.1.0.tar.gz | 13.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| baselog-2.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.1 kB
Release files / baselog-2.1.0.tar.gz
| Download URL | baselog-2.1.0.tar.gz |
|---|---|
| Size | 13.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
91dead310eaaf04c3b8716c88ef00dba0eb008f8c317b4f6a3b70e98b2a6b8eb
|
|
BLAKE2b-256 checksum How to use checksums |
0805377d728c0a1abd1d985c5fdc784bac1dad74c3020968d69cbd6b86a07093
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Release files / baselog-2.1.0-py3-none-any.whl
| Download URL | baselog-2.1.0-py3-none-any.whl |
|---|---|
| Size | 14.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
347f0784a859c81f6e354c31514f25391147e68cc0d09c37ecfa86220d923ee6
|
|
BLAKE2b-256 checksum How to use checksums |
8d7282fa99f645110ec0c91d98b6477027e93f8afa1c9823cdbbc9c3b90d48d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|