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()
.
Install
Python 3.11 or later is required.
pip install multilevelformatter
Usage
logger = logging.getLogger(__name__)
error = logger.error
message = logger.warning
verbose = logger.info
debug = logger.debug
# 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)
...
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
Close
Hashes for multilevelformatter-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6156649d158c5102357280792ebb36b9c57e9cee606a24f32c04c3910e42325 |
|
MD5 | c890732451b5f75f92af913720d28f04 |
|
BLAKE2b-256 | 379c84e2561da3b0a67a94b26815a84976650b3350ca360238f46440d86e1f82 |
Close
Hashes for multilevelformatter-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51d7b60cb359729d246aaf481c72ab8c524f8b0b960fc8b031a98c0e4cc1f040 |
|
MD5 | e5102a7a198de113ef55817fb7ddec57 |
|
BLAKE2b-256 | 571d14ed1519d1c1cdf6c8caa53cb1b9589172926ef0e1daf6e37d2ec9280b0d |