Skip to main content

typed 12-factor app configuration helper

Project description

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_dir path 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 crap!
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?!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

baselog-2.0.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

baselog-2.0.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file baselog-2.0.0.tar.gz.

File metadata

  • Download URL: baselog-2.0.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for baselog-2.0.0.tar.gz
Algorithm Hash digest
SHA256 6484effcc21d61565bb90a6cf8092bc0e12c00591746c635715dc11d302ad852
MD5 c5c5ec95dd9d58bfc64de4b120babbc4
BLAKE2b-256 bd933b0e4d6e548ed89755001a261fb6c54302fb0587ee255cd0b64a5b2db06a

See more details on using hashes here.

File details

Details for the file baselog-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: baselog-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for baselog-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bcef023e34f2525042839b6f6a785fb0c8013b36d9448ef1c5c6847ae789dc94
MD5 f337cf4d9c6eafbcf3bc9b7db866c8c2
BLAKE2b-256 9c4592b9144c18771612e29b7d2cf19c5a597ff7d58e039368da017fb2e7782f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page