Skip to main content

Fully typed, simple, intuitive, sensible and pragmatic logger configurator for standard python logging.

Project description

logician

PyPI - Types GitHub License 🔧 test 💡 typecheck 🛠️ lint 📊 coverage 📤 Upload Python Package PyPI - Version


Fully typed, simple, intuitive, and pragmatic logger configurator for standard Python logging.

logician is a lightweight utility that simplifies configuring Python's built-in logging module. It supports logger setup using environment variables, CLI flags (-v, -q), and sensible defaults—all fully typed, tested, and documented.


🚀 Features

  • 🔧 Minimal boilerplate for structured logging
  • 🌐 Environment-variable driven configuration (e.g. VT_ALL_LOG=DEBUG)
  • ⚙️ Verbosity-aware: -v, -vv, -q, etc.
  • 🎛️ Different formats for different log levels
  • 🔌 Works seamlessly with standard loggers and any logger name used by 3rd-party libraries (e.g., "uvicorn", "sqlalchemy") — assuming those libraries use standard Python logging
  • 🧪 Fully type annotated and well-tested via doctests
  • 📚 Extensive docstrings with live examples

⚠️ Note: logician assumes that external libraries like uvicorn and sqlalchemy use Python's standard logging module. If a library uses a custom or non-standard logging system, logician may not affect it.


📦 Installation

pip install logician

🧰 Quick Start

from logician import configure

configure()

import logging
logger = logging.getLogger(__name__)
logger.info("Hello from logician!")

This sets up the root logger and all loggers derived from it with a sensible default formatter and log level.


🔄 Environment Variable Configuration

logician reads log levels from environment variables like:

LGCN_ALL_LOG=DEBUG
LGCN_SOME_MODULE_LOG=WARNING

These automatically control the logger levels without code changes.

You can change the prefix (default is LGCN_) via env_prefix:

configure(env_prefix="APP_")

You can also use the lower-level API directly:

from logician.std_log.configurator import StdLoggerConfigurator
from logician.configurators.env import LgcnEnvListLC
import logging

logger = logging.getLogger('ap-generator')
logger = LgcnEnvListLC(["APGEN"], StdLoggerConfigurator(level=logging.INFO)).configure(logger)

See LgcnEnvListLC in logician.configurators.env to learn more.


🗣️ CLI Verbosity Integration

Use -v, -vv, -q, --quiet flags from your CLI parser to dynamically set log levels:

from argparse import ArgumentParser
from logician.std_log.configurator import StdLoggerConfigurator, VQSepLoggerConfigurator
import logging

parser = ArgumentParser()
parser.add_argument("-v", "--verbose", action="count", default=0)
parser.add_argument("-q", "--quiet", action="count", default=0)
args = parser.parse_args()

lc = VQSepLoggerConfigurator(StdLoggerConfigurator(), verbosity=args.verbose, quietness=args.quiet)
logger = logging.getLogger(__name__)
logger = lc.configure(logger)

This configures the logger to reflect the verbosity or quietness of the CLI input.


🪄 Log Formatting by Log Level

Many-a-times it is the case that more refined (lower) log-levels need to output more (detailed) information. Hence, logician maps more-detailed log-formats to lower log-levels. Different log levels can be mapped to different log formats automatically which takes effects thoughout all log levels.

⚠️ These format mappings currently assume use with Python's standard logging module. In the future, support may expand to other logging libraries or frameworks.

The default setup looks like this:

WARN and up -> '%(levelname)s: %(message)s'
INFO -> '%(name)s: %(levelname)s: %(message)s'
DEBUG -> '%(name)s: %(levelname)s: [%(filename)s - %(funcName)10s() ]: %(message)s'
TRACE and lower -> '%(asctime)s: %(name)s: %(levelname)s: [%(filename)s:%(lineno)d - %(funcName)10s() ]: %(message)s'

You can override these or pass in your own formatting configuration.


🛠️ Advanced Configuration

from logician import configure

configure(
    logger_names=["sqlalchemy", "uvicorn"],
    level="INFO",
    env_prefix="MYAPP_",
    verbosity=1
)

Keyword Arguments

Param Type Description
logger_names list[str] Additional logger names to configure aside from root
level str / int Default log level (e.g., "INFO", 20)
verbosity int Verbosity count to decrease log level (-v)
quietness int Quietness count to increase log level (-q)
env_prefix str Prefix for reading env-based logger overrides

🧪 Example Doctest

All public APIs include doctests. Example:

>>> from logician import derive_level
>>> derive_level(base="INFO", verbosity=1)
10
>>> derive_level(base="INFO", verbosity=0, quietness=1)
30

🛠 Real-World Usage

FastAPI + Uvicorn + Environment config

from logician import configure
configure(logger_names=["uvicorn", "sqlalchemy"], env_prefix="API_")

CLI Tools

from vt.utils.logging import VQSepLoggerConfigurator, StdLoggerConfigurator
import logging

lc = VQSepLoggerConfigurator(StdLoggerConfigurator(), verbosity=args.verbose, quietness=args.quiet)
logger = logging.getLogger("my_tool")
logger = lc.configure(logger)

🧪 Testing & Typing

  • ✅ 100% typed (compatible with MyPy & Pyright)
  • ✅ Doctests validate examples
  • ✅ Poetry-managed project with tests in tests/

📃 License

Apache License 2.0. See LICENSE for full text.


🤝 Contributing

Contributions welcome!

git clone https://github.com/Vaastav-Technologies/py-logger.git
cd py-logger
poetry install
pytest

Please write tests and add doctests for public functions.


🔗 Links

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

logician-0.0.0.dev3.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

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

logician-0.0.0.dev3-py3-none-any.whl (39.0 kB view details)

Uploaded Python 3

File details

Details for the file logician-0.0.0.dev3.tar.gz.

File metadata

  • Download URL: logician-0.0.0.dev3.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for logician-0.0.0.dev3.tar.gz
Algorithm Hash digest
SHA256 0acae6850ae261793a3e1dcb331dff21b317df3da84849c391fa9841b859b74e
MD5 86cd033b9c5c4afd977a47c9e393541e
BLAKE2b-256 0eb30d8dc813cb317890ad7d43e4b6cb9c7895704ce765c7af35c0a4315a6911

See more details on using hashes here.

Provenance

The following attestation bundles were made for logician-0.0.0.dev3.tar.gz:

Publisher: python-publish.yml on Vaastav-Technologies/py-logician

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file logician-0.0.0.dev3-py3-none-any.whl.

File metadata

  • Download URL: logician-0.0.0.dev3-py3-none-any.whl
  • Upload date:
  • Size: 39.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for logician-0.0.0.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 bb9b4da1f93f8dd9d0502655ff03a8e1f5d6586ca9d68dcd543aa4313683dfe8
MD5 7577c5e9a9f2054630acd13c466158f1
BLAKE2b-256 9edf43f0c4252ab1f0d7c8276ddb4065fc07e1e7447d5f7b46c8d241300289a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for logician-0.0.0.dev3-py3-none-any.whl:

Publisher: python-publish.yml on Vaastav-Technologies/py-logician

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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