logger.Formatter to simplify setting formatting for multiple logging levels
Project description
MultiLevelFormatter
MultiLevelFormatter is a Python logging.Formatter that simplifies setting log formats for different log levels. Log records with level logging.ERROR or higher are printed to STDERR if using defaults with MultilevelFormatter.setDefaults().
Motivation for the class has been the use of logging package for CLI verbosity control (--verbose, --debug):
- Define shortcuts for printing different level information instead of using
print():
logger = logging.getLogger(__name__)
error = logger.error
message = logger.warning
verbose = logger.info
debug = logger.debug
- Set logging level based on CLI option given. Mapping of logging levels:
| CLI option | logging level |
|---|---|
--debug |
logging.DEBUG |
--verbose |
logging.INFO |
| default | logging.WARNING |
--silent |
logging.ERROR |
# Not complete, does not run
def main() -> None:
...
# assumes command line arguments have been parsed into
# boolean flags: arg_verbose, arg_debug, arg_silent
LOG_LEVEL: int = logging.WARNING
if arg_verbose:
LOG_LEVEL = logging.INFO
elif arg_debug:
LOG_LEVEL = logging.DEBUG
elif arg_silent:
LOG_LEVEL = logging.ERROR
MultilevelFormatter.setDefaults(logger, log_file=log)
logger.setLevel(LOG_LEVEL)
See the example below for more details.
Install
Python 3.11 or later is required.
pip install multilevelformatter
Example
Full runnable example below. It can be found in demos/ folder.
import logging
from typer import Typer, Option
from typing import Annotated, Optional
from pathlib import Path
from multilevelformatter import MultilevelFormatter
logger = logging.getLogger(__name__)
error = logger.error
message = logger.warning
verbose = logger.info
debug = logger.debug
# the demo uses typer for CLI parsing.
# Typer has nothing to do with MultiLevelFormatter
app = Typer()
@app.callback(invoke_without_command=True)
def cli(
print_verbose: Annotated[
bool,
Option(
"--verbose",
"-v",
show_default=False,
help="verbose logging",
),
] = False,
print_debug: Annotated[
bool,
Option(
"--debug",
show_default=False,
help="debug logging",
),
] = False,
print_silent: Annotated[
bool,
Option(
"--silent",
show_default=False,
help="silent logging",
),
] = False,
log: Annotated[Optional[Path], Option(help="log to FILE", metavar="FILE")] = None,
) -> None:
"""MultilevelFormatter demo"""
global logger
try:
LOG_LEVEL: int = logging.WARNING
if print_verbose:
LOG_LEVEL = logging.INFO
elif print_debug:
LOG_LEVEL = logging.DEBUG
elif print_silent:
LOG_LEVEL = logging.ERROR
MultilevelFormatter.setDefaults(logger, log_file=log, level=LOG_LEVEL)
except Exception as err:
error(f"{err}")
message("standard")
verbose("verbose")
error("error")
debug("debug")
if __name__ == "__main__":
app()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multilevelformatter-0.4.1.tar.gz.
File metadata
- Download URL: multilevelformatter-0.4.1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e5fd11e95851bbdcfc849f0faf2c9172f0c314fb531791e783bd8c18cd8657a
|
|
| MD5 |
bfd13db78b633a328b483fc05f211396
|
|
| BLAKE2b-256 |
a52647da1dfb869447c5b981fdeca89e67e96c013b00fe5d8c11e5f3b6e754ca
|
File details
Details for the file multilevelformatter-0.4.1-py3-none-any.whl.
File metadata
- Download URL: multilevelformatter-0.4.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6472d4af7ba6e62bb68b0d1f64a66d943ef716424421cd67ff471fdc1a6b96cc
|
|
| MD5 |
474576c73a3e957c424c55e269099028
|
|
| BLAKE2b-256 |
226b6cc3523a5664cae3ce6fee4abc9fe7d5c187b537f3c29e1766b30586ee05
|