Skip to content

Commit

Permalink
Merge pull request #10 from Jylpah/dev
Browse files Browse the repository at this point in the history
version 0.4.1
  • Loading branch information
Jylpah committed Apr 1, 2024
2 parents ee43caf + bd7821b commit 5a64c2c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

`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`):

## Install

*Python 3.11 or later is required.*

```sh
pip install multilevelformatter
```

## Usage

1. Define shortcuts for printing different level information instead of using `print() `:

```python

logger = logging.getLogger(__name__)
error = logger.error
message = logger.warning
verbose = logger.info
debug = logger.debug
```

2. 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` |


```python
# Not complete, does not run
def main() -> None:

Expand All @@ -39,9 +42,16 @@ def main() -> None:
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.*

```sh
pip install multilevelformatter
```

# Example
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "multilevelformatter"
version = "0.4.0"
version = "0.4.1"
authors = [{ name = "Jylpah", email = "jylpah@gmail.com" }]
description = "logger.Formatter to simplify setting formatting for multiple logging levels"
readme = "README.md"
Expand Down
23 changes: 19 additions & 4 deletions src/multilevelformatter/multilevelformatter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# -----------------------------------------------------------
# Class MultilevelFormatter(logging.Formatter)
#
# logging.Formatter that simplifies setting different log formats
# for different log levels.
#
# -----------------------------------------------------------

__author__ = "Jylpah"
__copyright__ = "Copyright 2024, Jylpah <Jylpah@gmail.com>"
__credits__ = ["Jylpah"]
__license__ = "MIT"
__maintainer__ = "Jylpah"
__email__ = "Jylpah@gmail.com"
__status__ = "Production"

import logging
import sys
from typing import Literal, Optional, Dict, ClassVar, List
Expand Down Expand Up @@ -77,17 +93,16 @@ def addLoggingLevelMessage() -> None:

class MultilevelFormatter(logging.Formatter):
"""
logging.Formatter that simplifies setting different log formats for different log levels
logging.Formatter that simplifies setting different log formats
for different log levels.
Add to the module file:
logger = logging.getLogger(__name__)
error = logger.error
warning = logger.warning
message = MultilevelFormatter.message
message = logger.warning
verbose = logger.info
debug = logger.debug
"""

_levels: ClassVar[List[int]] = [
Expand Down

0 comments on commit 5a64c2c

Please sign in to comment.