Metadata-Version: 2.1
Name: simple-logging-setup
Version: 0.5
Summary: Nice and simple log formatting for the standard library
Author-email: Florian Scherf <mail@florianscherf.de>
License: MIT License
        
        Copyright (c) 2022 Florian Scherf
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/fscherf/simple-logging-setup
Project-URL: Repository, https://github.com/fscherf/simple-logging-setup
Project-URL: Bug Tracker, https://github.com/fscherf/simple-logging-setup/issues
Classifier: License :: OSI Approved :: MIT License
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Provides-Extra: dev
Requires-Dist: build ; extra == 'dev'
Requires-Dist: twine ; extra == 'dev'
Requires-Dist: rlpython ; extra == 'dev'

# simple-logging-setup

![license MIT](https://img.shields.io/pypi/l/simple-logging-setup.svg)
![Latest Version](https://img.shields.io/pypi/v/simple-logging-setup.svg)

simple-logging-setup is a simple yet highly configurable library that provides
sane configuration defaults for the logging system in the Python
standard library.

![Screenshot](screenshot.png)

**Features:**
 - No external dependencies
 - Simple coloring and detection if the terminal supports colored output
 - Formatting for exceptions
 - Support for [syslog severity levels](https://en.wikipedia.org/wiki/Syslog)
 - Log filtering
 - Easy to integrate into command line options


## Installation

simple-logging-setup can be installed using pip

```
pip install simple-logging-setup
```


## Usage

### Simple Setup
```python
import logging

from simple_logging_setup import setup

logger = logging.getLogger('my-logger')


setup(
    level='info',
    # put your configuration here
) 

logger.info('Hi mom!')
```


### Setup using command line options

```python
import argparse

from simple_logging_setup import setup


# parse command line
parser = argparse.ArgumentParser()

parser.add_argument(
    '-l',
    '--log-level',
    choices=['debug', 'info', 'warn', 'error', 'critical'],
    default='info',
)

parser.add_argument(
    '--loggers',
    type=str,
    nargs='+',
)

parser.add_argument(
    '--show-timestamps',
    action='store_true',
)

args = parser.parse_args()

# setup logging
setup(
    level=args.log_level,
    loggers=args.loggers,
    show_timestamp=args.show_timestamps,
)
```

    $ my-tool -l debug --loggers my-logger -disabled-logger
    $ my-tool --show-timestamps


## Configuration

All configuration is done by adding keyword arguments to
`simple_logging_setup.setup`.


| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `level` | `str` | `info` | Log level. Choices (case insensitive): `['debug', 'info', 'warn', 'warning', 'error', 'critical']`
| `colors` | `switch` | `True` | Enables or disables colors. Gets disabled by default if the terminal does not support colored output |
| `syslog_priorities` | `switch` | `False` | Enables or disables [syslog severity levels](https://en.wikipedia.org/wiki/Syslog). Gets enabled by default if running in journald |
| `show_thread_name` | `switch` | `True` | Show thread name in log output |
| `show_level_name` | `switch` | `True` | Show log level name in log output. Gets enabled by default if colors are disabled or not available |
| `show_timestamp` | `switch` | `True` | Show timestamp in log output |
| `show_logger_name` | `switch` | `True` | Show logger name in log output |


`switch` is a special type that does best-effort parsing of incoming values.

Examples: `True`, `1`, `'True'`, `'TRUE'`, `'yes'`, `'on'`, [...]


## Filtering

Especially when the log level is set to `debug`, logging output can become hard
to read. simple-logging-setup allows you to filter loggers by including or
excluding specific logger names.

```python
from simple_logging_setup import setup

setup(
    loggers=[

        # include loggers to the output (the `+` is optional)
        'my-project.logger-1',
        '+my-project.logger-1',

        # exclude loggers from the output (both `-` and `_` can be used)
        '-my-project.logger-1',
        '_my-project.logger-1',
    ],
)
```

Why two prefixes to choose for excludes? The `-` notation makes more sense in
code but may not be suitable for command line options. This optional extra
notation allows for command line options like

  `--loggers +included-logger _excluded-logger`.


## Name Filtering

In some cases, it is not useful to always print all logger names. For example,
when creating a command line tool, a logger name like `my-project.my-sub-system`
is worth printing, but `root` is not. simple-logging-setup allows you to filter
out logger names, without excluding the logger entirely.

```python
from simple_logging_setup import setup

setup(
    filter_logger_names=['root'],
)
```


## Presets

simple-logging-setup comes with a list of useful presets which can be enabled
via the `preset` keyword argument.

```python
from simple_logging_setup import setup

setup(preset='service')  # 'service' is the default
```

| Name | Description |
| --- | --- |
| `service` | Enables all switches and features available |
| `cli` | Disables all switches but `show_logger_name` and filters the logger name `root` |
