Skip to main content

Dead-simple logging: what python's logging should've been

Project description

Dead Simple Logging

What python's logging should've been

Installation

pip install dslog

Usage

  • Any custom "handler" (aka function to actually print)
import rich
from dslog import Logger

logger = Logger.of(rich.print) \
  .limit('WARNING') \
  .format(lambda *objs, level: (f'[bold][{level}][/]', *objs))

logger('My message', ..., level='INFO')
# doesn't print anything
logger('Oops!', { 'more': 'details' }, level='WARNING')
# [WARNING] Oops! { 'more', 'details' }     ([WARNING] in bold text)
  • Or some of the predefined ones, which come already formatted
Logger.rich()
Logger.file('log.txt')
  • Or the best default logger
Logger.empty()

uvicorn/fastapi Logging

If you've used it before, you know it sucks. No more:

from fastapi import FastAPI
import uvicorn
from dslog import Logger
from dslog.uvicorn import setup_loggers_lifespan, DEFAULT_FORMATTER, ACCESS_FORMATTER

logger = Logger.click().prefix('[MY API]')

app = FastAPI(lifespan=setup_loggers_lifespan(
  access=logger.prefix('[ACCESS]').format(ACCESS_FORMATTER).limit('WARNING'),
  uvicorn=logger.format(DEFAULT_FORMATTER).limit('INFO'),
))

uvicorn.run(app)

# The initial logs will run (uvicorn is quite stubborn):

# INFO: Started server process [95349]
# INFO: Waiting for application startup.

# But then, all the logs are controlled by you (ie. your logger)

# [INFO] [MY API] Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
# [WARNING] [MY API] [ACCESS] ...

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

dslog-0.2.8.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dslog-0.2.8-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file dslog-0.2.8.tar.gz.

File metadata

  • Download URL: dslog-0.2.8.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for dslog-0.2.8.tar.gz
Algorithm Hash digest
SHA256 c24f8734e40ac7433e4ea292767e244eef879e61abb731f6e0aafc1fe867b7c8
MD5 7d31fe3035ebccb0175e86bf628bc76a
BLAKE2b-256 5c5c385595552af54d8fe3ff86577f91c542da2ce27a151f6d8d99a3210a9212

See more details on using hashes here.

File details

Details for the file dslog-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: dslog-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for dslog-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 24365fde9aa9b069cc5c9ea18ea03e85a5dfafe22cc7a8c8eff730f0578038f9
MD5 4d20530fa2c64d78ff47e41412c9d4d4
BLAKE2b-256 7a3d878ba27712c4dad31999c5887790482cee332ecde19fcc929bf625f71d10

See more details on using hashes here.

Supported by

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