loggingx-py


Nameloggingx-py JSON
Version 0.12.0 PyPI version JSON
download
home_pageNone
SummaryDrop-in replacement for Python's built-in `logging` module
upload_time2024-10-23 15:32:22
maintainerNone
docs_urlNone
authorNone
requires_python<4.0,>=3.8
licenseMIT License Copyright (c) 2023-2024 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.
keywords logging contextual-logging structured-logging
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![license](https://img.shields.io/github/license/hhk7734/loggingx.py)
![pypi](https://img.shields.io/pypi/v/loggingx-py)
![language](https://img.shields.io/github/languages/top/hhk7734/loggingx.py)

# 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

handler = logging.StreamHandler()
handler.setFormatter(logging.ConsoleFormatter())
logging.basicConfig(level=logging.INFO, handlers=[handler])


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
2024-08-22T02:46:38.257+09:00 INFO   main.py:9  A {}
2024-08-22T02:46:38.257+09:00 INFO   main.py:15 B {"A": "a"}
2024-08-22T02:46:38.258+09:00 INFO   main.py:21 C {"A": "a", "B": "b"}
```

## Formatter

### 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": "main.py:10",
  "msg": "test",
  "ctx": "ctx",
  "thread_name": "MainThread",
  "extra": "extra"
}
```

### ConsoleFormatter

```python
import loggingx as logging

handler = logging.StreamHandler()
handler.setFormatter(logging.ConsoleFormatter())
logging.basicConfig(level=logging.INFO, handlers=[handler])

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

```shell
2024-08-22T02:48:17.868+09:00 INFO   main.py:9  test {"ctx": "ctx", "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"})
```

## orjson

If `orjson` is installed, this module will use `orjson` instead of `json`.

## jq

```shell
alias log2jq="jq -rRC --unbuffered '. as \$line | try fromjson catch \$line' | sed 's/\\\\n/\\n/g; s/\\\\t/\\t/g'"
```

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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "loggingx-py",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": "logging, contextual-logging, structured-logging",
    "author": null,
    "author_email": "Hyeonki Hong <hhk7734@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/cf/84/e575e1b976b82fd2a8792b296440d6b6f7e8d1b6f6c3438b9207b475c52b/loggingx_py-0.12.0.tar.gz",
    "platform": null,
    "description": "![license](https://img.shields.io/github/license/hhk7734/loggingx.py)\n![pypi](https://img.shields.io/pypi/v/loggingx-py)\n![language](https://img.shields.io/github/languages/top/hhk7734/loggingx.py)\n\n# loggingx.py\n\n`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.\n\n```shell\npython3 -m pip install loggingx-py\n```\n\n## Additional Format\n\n- https://docs.python.org/3/library/logging.html#logrecord-attributes\n\n| Attribute name | Format        | Description                   |\n| -------------- | ------------- | ----------------------------- |\n| caller         | %(caller)s    | Caller(`<pathname>:<lineno>`) |\n| ctxFields      | %(ctxFields)s | Context fields                |\n\n## Optimization\n\n| Configuration                | Description                                                    |\n| ---------------------------- | -------------------------------------------------------------- |\n| `logging.logThreads`         | If `False`, Record will not collect `thread` and `threadName`. |\n| `logging.logProcesses`       | If `False`, Record will not collect `process`.                 |\n| `logging.logMultiprocessing` | If `False`, Record will not collect `processName`.             |\n\n\n## Context\n\n```python\nimport loggingx as logging\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(logging.ConsoleFormatter())\nlogging.basicConfig(level=logging.INFO, handlers=[handler])\n\n\ndef A() -> None:\n    logging.info(\"A\")\n    with logging.addFields(A=\"a\"):\n        B()\n\n\ndef B() -> None:\n    logging.info(\"B\")\n    with logging.addFields(B=\"b\"):\n        C()\n\n\ndef C() -> None:\n    logging.info(\"C\")\n\n\nif __name__ == \"__main__\":\n    A()\n```\n\n```shell\n2024-08-22T02:46:38.257+09:00 INFO   main.py:9  A {}\n2024-08-22T02:46:38.257+09:00 INFO   main.py:15 B {\"A\": \"a\"}\n2024-08-22T02:46:38.258+09:00 INFO   main.py:21 C {\"A\": \"a\", \"B\": \"b\"}\n```\n\n## Formatter\n\n### JSONFormatter\n\n```python\nimport loggingx as logging\n\nhandler = logging.StreamHandler()\n# handler.setFormatter(logging.JSONFormatter())\nhandler.setFormatter(logging.JSONFormatter(logging.Information.THREAD_NAME))\nlogging.basicConfig(level=logging.INFO, handlers=[handler])\n\nif __name__ == \"__main__\":\n    with logging.addFields(ctx=\"ctx\"):\n        logging.info(\"test\", extra={\"extra\": \"extra\"})\n```\n\n```json\n{\n  \"time\": 1689697694.9980711,\n  \"level\": \"info\",\n  \"caller\": \"main.py:10\",\n  \"msg\": \"test\",\n  \"ctx\": \"ctx\",\n  \"thread_name\": \"MainThread\",\n  \"extra\": \"extra\"\n}\n```\n\n### ConsoleFormatter\n\n```python\nimport loggingx as logging\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(logging.ConsoleFormatter())\nlogging.basicConfig(level=logging.INFO, handlers=[handler])\n\nif __name__ == \"__main__\":\n    with logging.addFields(ctx=\"ctx\"):\n        logging.info(\"test\", extra={\"extra\": \"extra\"})\n```\n\n```shell\n2024-08-22T02:48:17.868+09:00 INFO   main.py:9  test {\"ctx\": \"ctx\", \"extra\": \"extra\"}\n```\n\n## With `logging`\n\n```python\nimport logging\n\nimport loggingx\n\n# handler = loggingx.StreamHandler()\nhandler = logging.StreamHandler()\nhandler.setFormatter(loggingx.JSONFormatter())\n\n# loggingx.basicConfig(level=loggingx.INFO, handlers=[handler])\nlogging.basicConfig(level=logging.INFO, handlers=[handler])\n\nif __name__ == \"__main__\":\n    with loggingx.addFields(ctx=\"ctx\"):\n        # loggingx.info(\"test\", extra={\"extra\": \"extra\"})\n        logging.info(\"test\", extra={\"extra\": \"extra\"})\n```\n\n## orjson\n\nIf `orjson` is installed, this module will use `orjson` instead of `json`.\n\n## jq\n\n```shell\nalias log2jq=\"jq -rRC --unbuffered '. as \\$line | try fromjson catch \\$line' | sed 's/\\\\\\\\n/\\\\n/g; s/\\\\\\\\t/\\\\t/g'\"\n```\n\n```shell\npython3 <path> 2>&1 | log2jq\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023-2024 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. ",
    "summary": "Drop-in replacement for Python's built-in `logging` module",
    "version": "0.12.0",
    "project_urls": {
        "repository": "https://github.com/hhk7734/loggingx.py"
    },
    "split_keywords": [
        "logging",
        " contextual-logging",
        " structured-logging"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3bd30b99204a3b6fcac86382f648402699e86401a2b56e7beb7ee7b08daa0724",
                "md5": "f75a8a99ad024525a85a1be26ee90f3f",
                "sha256": "fb9b8b8b4e9fbf8c202a6881e808ef1a57613178ab62da0fec7192e6b21ece47"
            },
            "downloads": -1,
            "filename": "loggingx_py-0.12.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f75a8a99ad024525a85a1be26ee90f3f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 8013,
            "upload_time": "2024-10-23T15:32:21",
            "upload_time_iso_8601": "2024-10-23T15:32:21.068288Z",
            "url": "https://files.pythonhosted.org/packages/3b/d3/0b99204a3b6fcac86382f648402699e86401a2b56e7beb7ee7b08daa0724/loggingx_py-0.12.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cf84e575e1b976b82fd2a8792b296440d6b6f7e8d1b6f6c3438b9207b475c52b",
                "md5": "1d566c36dba6e1735a2d4f7733af62d6",
                "sha256": "cf7378d9bb305a018cdb1433027ba6f951b26492d107c2a91eb77459a95e30ac"
            },
            "downloads": -1,
            "filename": "loggingx_py-0.12.0.tar.gz",
            "has_sig": false,
            "md5_digest": "1d566c36dba6e1735a2d4f7733af62d6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 19457,
            "upload_time": "2024-10-23T15:32:22",
            "upload_time_iso_8601": "2024-10-23T15:32:22.077467Z",
            "url": "https://files.pythonhosted.org/packages/cf/84/e575e1b976b82fd2a8792b296440d6b6f7e8d1b6f6c3438b9207b475c52b/loggingx_py-0.12.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-23 15:32:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hhk7734",
    "github_project": "loggingx.py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "loggingx-py"
}
        
Elapsed time: 0.97701s