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.6.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

dslog-0.2.6-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dslog-0.2.6.tar.gz
Algorithm Hash digest
SHA256 d37d64f3a8076dc0b0ad273dca093f8fcbab61b1b39f4b855f968f395dfb0c26
MD5 05914185be764b851f5c5d240bdd0dda
BLAKE2b-256 2cb047d560e86a0ae87d89d582c49e3d35091d1a9a0be88245edc8b6bbd146c3

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dslog-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 336b4d7c52f78571aaa71fb2d4105e76a871e31995a62f9d52c50c0e861fbe89
MD5 db9f3051a1067fd006070f46fef89a2b
BLAKE2b-256 d5c0a935aa6afdeceda6ca2fecede92552a103ad12b1544ceebbfbcb78e41446

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