Metadata-Version: 2.1
Name: loggingx-py
Version: 0.6.0
Summary: Drop-in replacement for Python's built-in `logging` module
Author-email: Hyeonki Hong <hhk7734@gmail.com>
License: MIT License
        
        Copyright (c) 2023 Hyeonki Hong <hhk7734@gmail.com>
        
        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: repository, https://github.com/hhk7734/loggingx.py
Keywords: logging,contextual-logging,structured-logging
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Developers
Requires-Python: <4.0,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE

## loggingx.py

`loggingx` is a drop-in replacement for Python's built-in `logging` module. Even better, once you've imported `loggingx`, you don't need to modify your existing `logging` module.

```shell
python3 -m pip install loggingx-py
```

### Additional Format

- https://docs.python.org/3/library/logging.html#logrecord-attributes

| Attribute name | Format        | Description                   |
| -------------- | ------------- | ----------------------------- |
| caller         | %(caller)s    | Caller(`<pathname>:<lineno>`) |
| ctxFields      | %(ctxFields)s | Context fields                |

### Optimization

| Configuration                | Description                                                    |
| ---------------------------- | -------------------------------------------------------------- |
| `logging.logThreads`         | If `False`, Record will not collect `thread` and `threadName`. |
| `logging.logProcesses`       | If `False`, Record will not collect `process`.                 |
| `logging.logMultiprocessing` | If `False`, Record will not collect `processName`.             |


### Context

```python
import loggingx as logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s\t%(levelname)s\t%(caller)s\t%(message)s\t%(ctxFields)s",
)


def A() -> None:
    logging.info("A")
    with logging.addFields(A="a"):
        B()


def B() -> None:
    logging.info("B")
    with logging.addFields(B="b"):
        C()


def C() -> None:
    logging.info("C")


if __name__ == "__main__":
    A()
```

```shell
2023-07-19 01:15:33,981 INFO    loggingx.py/main.py:10  A       {}
2023-07-19 01:15:33,981 INFO    loggingx.py/main.py:16  B       {'A': 'a'}
2023-07-19 01:15:33,982 INFO    loggingx.py/main.py:22  C       {'A': 'a', 'B': 'b'}
```

### JSONFormatter

```python
import loggingx as logging

handler = logging.StreamHandler()
# handler.setFormatter(logging.JSONFormatter())
handler.setFormatter(logging.JSONFormatter(logging.Information.THREAD_NAME))
logging.basicConfig(level=logging.INFO, handlers=[handler])

if __name__ == "__main__":
    with logging.addFields(ctx="ctx"):
        logging.info("test", extra={"extra": "extra"})
```

```json
{"time": 1689697694.9980711, "level": "info", "caller": "loggingx.py/main.py:9", "msg": "test", "ctx": "ctx", "thread_name": "MainThread", "extra": "extra"}
```

### With `logging`

```python
import logging

import loggingx

# handler = loggingx.StreamHandler()
handler = logging.StreamHandler()
handler.setFormatter(loggingx.JSONFormatter())

# loggingx.basicConfig(level=loggingx.INFO, handlers=[handler])
logging.basicConfig(level=logging.INFO, handlers=[handler])

if __name__ == "__main__":
    with loggingx.addFields(ctx="ctx"):
        # loggingx.info("test", extra={"extra": "extra"})
        logging.info("test", extra={"extra": "extra"})
```

### jq

```shell
alias log2jq="jq -R -r '. as \$line | try fromjson catch \$line'"
```

```shell
python3 <path> 2>&1 | log2jq
```
