Skip to main content

My logging improvement

Project description

BetterLogging

BetterLogging

Improved version of the standard logger.

  • Added TRACE level.
  • Added ColorizedFormatter.
  • Added better traceback formatting.

This package patching the standard logging library.

Thus, after import, all improvements will be available inside the logging module.

But for better typing, I prefer to use betterlogging everywhere.

Requirements

Python 3.7 and above. No additional dependencies.

Installation

pip install betterlogging

Usage

Simple usage

import betterlogging as logging


logging.basic_colorized_config(level=logging.INFO)

logger = logging.getLogger('test')

logger.trace("some trace message")
logger.debug("some debug message")
logger.info("some info message")
logger.warning("some warning message")
logger.error("some error message")
logger.critical("some critical message")

More advanced usage

import betterlogging as logging


# You can use shortcut:
logger = logging.get_colorized_logger(name="test")

# Or, if you want to configure formatter:
handler = logging.StreamHandler()
handler.setFormatter(logging.ColorizedFormatter(hide_lib_diagnose=False))

logger.addHandler(handler)
logger.setLevel(logging.TRACE)

logger.trace("some trace message")
logger.debug("some debug message")
logger.info("some info message")
logger.warning("some warning message")
logger.error("some error message")
logger.critical("some critical message")

def div(x: int, y: int) -> float:
    return x / y

try:
    div(1, 0)
except:
    logger.exception("Some exception")

Config for uvicorn

{
  "version": 1,
  "disable_existing_loggers": false,
  "formatters": {
    "default": {
      "()": "betterlogging.ColorizedFormatter"
    },
    "access": {
      "()": "betterlogging.ColorizedFormatter",
      "fmt": "%(c_fg_green)s%(asctime)s %(c_color)s%(levelname)-8s%(c_reset)s %(c_fg_cyan)s[%(name)s] %(c_reset)s%(message)s"
    }
  },
  "handlers": {
    "default": {
      "formatter": "default",
      "class": "logging.StreamHandler"
    },
    "access": {
      "formatter": "access",
      "class": "logging.StreamHandler",
      "stream": "ext://sys.stdout"
    }
  },
  "loggers": {
    "": {
      "handlers": [
        "default"
      ],
      "level": "DEBUG"
    },
    "uvicorn.error": {
      "level": "INFO"
    },
    "uvicorn.access": {
      "handlers": [
        "access"
      ],
      "level": "INFO",
      "propagate": false
    }
  }
}
uvicorn application:app --debug --reload --log-config ./logging.json

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

betterlogging-1.0.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

betterlogging-1.0.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file betterlogging-1.0.0.tar.gz.

File metadata

  • Download URL: betterlogging-1.0.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for betterlogging-1.0.0.tar.gz
Algorithm Hash digest
SHA256 324fab02af0a1a6ea05869ac8dd5b1bcfa59f6fa882cf75e24dfa99f412f54d3
MD5 864cb8ffe93a40761bc132f7ec92edfa
BLAKE2b-256 a1fb9f400b2faedab8229698356580de53e97cf3569d40a3a826d07773e762eb

See more details on using hashes here.

File details

Details for the file betterlogging-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for betterlogging-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b20f4d39af98faf540e9c3fe3d0a08872b3c5da0d02724e7c33be7358aae2a7b
MD5 aa9c494ac8c81fcf07d7f017e38502fa
BLAKE2b-256 569bdb1ed2b226a3dd243a09e449a21797e8c386fbce22f8c559a88b5c40de90

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