beans-logging


Namebeans-logging JSON
Version 6.0.0 PyPI version JSON
download
home_pagehttps://github.com/bybatkhuu/module.python-logging
Summary'beans_logging' is a python package for simple logger and easily managing logging modules. It is a Loguru based custom logging package for python projects.
upload_time2023-10-31 04:30:10
maintainer
docs_urlNone
authorBatkhuu Byambajav
requires_python>=3.8
licenseMIT
keywords beans_logging loguru logging logger logs log print custom-logging
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # beans_logging

[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/bybatkhuu/module.python-logging/2.build-publish.yml?logo=GitHub)](https://github.com/bybatkhuu/module.python-logging/actions/workflows/2.build-publish.yml)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/bybatkhuu/module.python-logging?logo=GitHub)](https://github.com/bybatkhuu/module.python-logging/releases)
[![PyPI](https://img.shields.io/pypi/v/beans-logging?logo=PyPi)](https://pypi.org/project/beans-logging)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/beans-logging?logo=Python)](https://docs.conda.io/en/latest/miniconda.html)

`beans_logging` is a python package for simple logger and easily managing logging modules.

It is a `Loguru` based custom logging package for python projects.

## Features

- Main **logger** based on **Loguru** logging - <https://pypi.org/project/loguru>
- Logging to **log files** (all, error, json)
- **Pre-defined** logging configs and handlers
- **Colorful** logging
- Auto **intercepting** and **muting** modules
- Load config from **YAML** or **JSON** file
- Custom options as a **config**
- Custom logging **formats**
- **Multiprocess** compatibility (Linux, macOS - 'fork')
- Add custom **handlers**
- **Base** logging module
- Support **Pydantic-v1** and **Pydantic-v2**

---

## Installation

### 1. Prerequisites

- **Python (>= v3.8)**
- **PyPi (>= v23)**

### 2. Install beans-logging package

Choose one of the following methods to install the package **[A ~ F]**:

**A.** [**RECOMMENDED**] Install from **PyPi**

```sh
# Install or upgrade beans-logging package:
pip install -U beans-logging
```

**B.** Install latest version from **GitHub**

```sh
# Install package by git:
pip install git+https://github.com/bybatkhuu/module.python-logging.git
```

**C.** Install from **pre-built release** files

1. Download **`.whl`** or **`.tar.gz`** file from **releases** - <https://github.com/bybatkhuu/module.python-logging/releases>
2. Install with pip:

```sh
# Install from .whl file:
pip install ./beans_logging-[VERSION]-py3-none-any.whl
# Or install from .tar.gz file:
pip install ./beans_logging-[VERSION].tar.gz
```

**D.** Install from **source code** by building package

```sh
# Clone repository by git:
git clone https://github.com/bybatkhuu/module.python-logging.git beans_logging
cd ./beans_logging

# Install python build tool:
pip install -U pip build

# Build python package:
python -m build

_VERSION=$(./scripts/get-version.sh)

# Install from .whl file:
pip install ./dist/beans_logging-${_VERSION}-py3-none-any.whl
# Or install from .tar.gz file:
pip install ./dist/beans_logging-${_VERSION}.tar.gz
```

**E.** Install with pip editable **development mode** (from source code)

```sh
# Clone repository by git:
git clone https://github.com/bybatkhuu/module.python-logging.git beans_logging
cd ./beans_logging

# Install with editable development mode:
pip install -e .
```

**F.** Manually add to **PYTHONPATH** (not recommended)

```sh
# Clone repository by git:
git clone https://github.com/bybatkhuu/module.python-logging.git beans_logging
cd ./beans_logging

# Install python dependencies:
pip install -r ./requirements.txt

# Add current path to PYTHONPATH:
export PYTHONPATH="${PWD}:${PYTHONPATH}"
```

## Usage/Examples

To use `beans_logging`, import the `logger` instance from the `beans_logging.auto` package:

```python
from beans_logging.auto import logger
```

You can call logging methods directly from the `logger` instance:

```python
logger.info("Logging info.")
```

### **Simple**

[**`configs/logger.yml`**](https://github.com/bybatkhuu/module.python-logging/blob/main/examples/simple/configs/logger.yml):

```yml
logger:
  app_name: "my-app"
  level: "TRACE"
  file:
    log_handlers:
      enabled: true
    json_handlers:
      enabled: true
```

[**`main.py`**](https://github.com/bybatkhuu/module.python-logging/blob/main/examples/simple/main.py):

```python
from beans_logging.auto import logger


logger.trace("Tracing...")
logger.debug("Debugging...")
logger.info("Logging info.")
logger.success("Success.")
logger.warning("Warning something.")
logger.error("Error occured.")
logger.critical("CRITICAL ERROR.")

def divide(a, b):
    _result = a / b
    return _result

def nested(c):
    try:
        divide(5, c)
    except ZeroDivisionError as err:
        logger.error(err)
        raise

try:
    nested(0)
except Exception as err:
    logger.exception("Show me, what value is wrong:")
```

Run the [**`examples/simple`**](https://github.com/bybatkhuu/module.python-logging/tree/main/examples/simple):

```sh
cd ./examples/simple

python ./main.py
```

**Output**:

```txt
[2023-09-01 00:00:00.000 +09:00 | TRACE | beans_logging._base:478]: Intercepted modules: ['concurrent', 'concurrent.futures', 'asyncio']; Muted modules: [];
[2023-09-01 00:00:00.000 +09:00 | TRACE | __main__:7]: Tracing...
[2023-09-01 00:00:00.000 +09:00 | DEBUG | __main__:8]: Debugging...
[2023-09-01 00:00:00.000 +09:00 | INFO  | __main__:9]: Logging info.
[2023-09-01 00:00:00.000 +09:00 | OK    | __main__:10]: Success.
[2023-09-01 00:00:00.000 +09:00 | WARN  | __main__:11]: Warning something.
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:12]: Error occured.
[2023-09-01 00:00:00.000 +09:00 | CRIT  | __main__:13]: CRITICAL ERROR.
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:25]: division by zero
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:32]: Show me, what value is wrong:
Traceback (most recent call last):

> File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 30, in <module>
    nested(0)
    └ <function nested at 0x10802a4c0>

  File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 23, in nested
    divide(5, c)
    │         └ 0
    └ <function divide at 0x1052f31f0>

  File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 17, in divide
    _result = a / b
              │   └ 0
              └ 5

ZeroDivisionError: division by zero
```

### **FastAPI**

Checkout `beans_logging_fastapi` package: <https://github.com/bybatkhuu/module.fastapi-logging>

- FastAPI HTTP access logging middleware
- Install with pip: `pip install -U beans-logging[fastapi]` or `pip install -U beans-logging-fastapi`

---

## Running Tests

To run tests, run the following command:

```sh
# Install python test dependencies:
pip install -r ./requirements.test.txt

# Run tests:
python -m pytest -v
```

## Environment Variables

You can use the following environment variables inside [**`.env.example`**](https://github.com/bybatkhuu/module.python-logging/blob/main/.env.example) file:

```sh
ENV=development
DEBUG=true

BEANS_LOGGING_DISABLE_DEFAULT=false
BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
BEANS_LOGGING_LOGS_DIR="./logs"
```

## Configuration

You can use the following configuration template [**`logger.yml`**](https://github.com/bybatkhuu/module.python-logging/blob/main/templates/configs/logger.yml): file:

```yaml
logger:
  # app_name: "app"
  level: "INFO"
  use_diagnose: false
  stream:
    use_color: true
    use_icon: false
    format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{level_short:<5}</level> | <w>{name}:{line}</w>]: <level>{message}</level>"
    std_handler:
      enabled: true
  file:
    logs_dir: "./logs"
    rotate_size: 10000000 # 10MB
    rotate_time: "00:00:00"
    backup_count: 90
    log_handlers:
      enabled: false
      format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}"
      log_path: "{app_name}.std.all.log"
      err_path: "{app_name}.std.err.log"
    json_handlers:
      enabled: false
      use_custom: false
      log_path: "{app_name}.json.all.log"
      err_path: "{app_name}.json.err.log"
  intercept:
    auto_load:
      enabled: true
      only_base: false
      ignore_modules: []
    include_modules: []
    mute_modules: []
  extra:
```

## Documentation

- [docs](https://github.com/bybatkhuu/module.python-logging/blob/main/docs/README.md)
- [scripts](https://github.com/bybatkhuu/module.python-logging/blob/main/docs/scripts/README.md)

---

## References

- <https://github.com/Delgan/loguru>
- <https://loguru.readthedocs.io/en/stable/api/logger.html>
- <https://loguru.readthedocs.io/en/stable/resources/recipes.html>
- <https://github.com/bybatkhuu/module.fastapi-logging>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bybatkhuu/module.python-logging",
    "name": "beans-logging",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "beans_logging,loguru,logging,logger,logs,log,print,custom-logging",
    "author": "Batkhuu Byambajav",
    "author_email": "batkhuu10@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/35/62/dee91ebf5d3d67e9f51c3b5b3340b84464ca89b0b91f8e7771750c35b9ad/beans_logging-6.0.0.tar.gz",
    "platform": null,
    "description": "# beans_logging\n\n[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/bybatkhuu/module.python-logging/2.build-publish.yml?logo=GitHub)](https://github.com/bybatkhuu/module.python-logging/actions/workflows/2.build-publish.yml)\n[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/bybatkhuu/module.python-logging?logo=GitHub)](https://github.com/bybatkhuu/module.python-logging/releases)\n[![PyPI](https://img.shields.io/pypi/v/beans-logging?logo=PyPi)](https://pypi.org/project/beans-logging)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/beans-logging?logo=Python)](https://docs.conda.io/en/latest/miniconda.html)\n\n`beans_logging` is a python package for simple logger and easily managing logging modules.\n\nIt is a `Loguru` based custom logging package for python projects.\n\n## Features\n\n- Main **logger** based on **Loguru** logging - <https://pypi.org/project/loguru>\n- Logging to **log files** (all, error, json)\n- **Pre-defined** logging configs and handlers\n- **Colorful** logging\n- Auto **intercepting** and **muting** modules\n- Load config from **YAML** or **JSON** file\n- Custom options as a **config**\n- Custom logging **formats**\n- **Multiprocess** compatibility (Linux, macOS - 'fork')\n- Add custom **handlers**\n- **Base** logging module\n- Support **Pydantic-v1** and **Pydantic-v2**\n\n---\n\n## Installation\n\n### 1. Prerequisites\n\n- **Python (>= v3.8)**\n- **PyPi (>= v23)**\n\n### 2. Install beans-logging package\n\nChoose one of the following methods to install the package **[A ~ F]**:\n\n**A.** [**RECOMMENDED**] Install from **PyPi**\n\n```sh\n# Install or upgrade beans-logging package:\npip install -U beans-logging\n```\n\n**B.** Install latest version from **GitHub**\n\n```sh\n# Install package by git:\npip install git+https://github.com/bybatkhuu/module.python-logging.git\n```\n\n**C.** Install from **pre-built release** files\n\n1. Download **`.whl`** or **`.tar.gz`** file from **releases** - <https://github.com/bybatkhuu/module.python-logging/releases>\n2. Install with pip:\n\n```sh\n# Install from .whl file:\npip install ./beans_logging-[VERSION]-py3-none-any.whl\n# Or install from .tar.gz file:\npip install ./beans_logging-[VERSION].tar.gz\n```\n\n**D.** Install from **source code** by building package\n\n```sh\n# Clone repository by git:\ngit clone https://github.com/bybatkhuu/module.python-logging.git beans_logging\ncd ./beans_logging\n\n# Install python build tool:\npip install -U pip build\n\n# Build python package:\npython -m build\n\n_VERSION=$(./scripts/get-version.sh)\n\n# Install from .whl file:\npip install ./dist/beans_logging-${_VERSION}-py3-none-any.whl\n# Or install from .tar.gz file:\npip install ./dist/beans_logging-${_VERSION}.tar.gz\n```\n\n**E.** Install with pip editable **development mode** (from source code)\n\n```sh\n# Clone repository by git:\ngit clone https://github.com/bybatkhuu/module.python-logging.git beans_logging\ncd ./beans_logging\n\n# Install with editable development mode:\npip install -e .\n```\n\n**F.** Manually add to **PYTHONPATH** (not recommended)\n\n```sh\n# Clone repository by git:\ngit clone https://github.com/bybatkhuu/module.python-logging.git beans_logging\ncd ./beans_logging\n\n# Install python dependencies:\npip install -r ./requirements.txt\n\n# Add current path to PYTHONPATH:\nexport PYTHONPATH=\"${PWD}:${PYTHONPATH}\"\n```\n\n## Usage/Examples\n\nTo use `beans_logging`, import the `logger` instance from the `beans_logging.auto` package:\n\n```python\nfrom beans_logging.auto import logger\n```\n\nYou can call logging methods directly from the `logger` instance:\n\n```python\nlogger.info(\"Logging info.\")\n```\n\n### **Simple**\n\n[**`configs/logger.yml`**](https://github.com/bybatkhuu/module.python-logging/blob/main/examples/simple/configs/logger.yml):\n\n```yml\nlogger:\n  app_name: \"my-app\"\n  level: \"TRACE\"\n  file:\n    log_handlers:\n      enabled: true\n    json_handlers:\n      enabled: true\n```\n\n[**`main.py`**](https://github.com/bybatkhuu/module.python-logging/blob/main/examples/simple/main.py):\n\n```python\nfrom beans_logging.auto import logger\n\n\nlogger.trace(\"Tracing...\")\nlogger.debug(\"Debugging...\")\nlogger.info(\"Logging info.\")\nlogger.success(\"Success.\")\nlogger.warning(\"Warning something.\")\nlogger.error(\"Error occured.\")\nlogger.critical(\"CRITICAL ERROR.\")\n\ndef divide(a, b):\n    _result = a / b\n    return _result\n\ndef nested(c):\n    try:\n        divide(5, c)\n    except ZeroDivisionError as err:\n        logger.error(err)\n        raise\n\ntry:\n    nested(0)\nexcept Exception as err:\n    logger.exception(\"Show me, what value is wrong:\")\n```\n\nRun the [**`examples/simple`**](https://github.com/bybatkhuu/module.python-logging/tree/main/examples/simple):\n\n```sh\ncd ./examples/simple\n\npython ./main.py\n```\n\n**Output**:\n\n```txt\n[2023-09-01 00:00:00.000 +09:00 | TRACE | beans_logging._base:478]: Intercepted modules: ['concurrent', 'concurrent.futures', 'asyncio']; Muted modules: [];\n[2023-09-01 00:00:00.000 +09:00 | TRACE | __main__:7]: Tracing...\n[2023-09-01 00:00:00.000 +09:00 | DEBUG | __main__:8]: Debugging...\n[2023-09-01 00:00:00.000 +09:00 | INFO  | __main__:9]: Logging info.\n[2023-09-01 00:00:00.000 +09:00 | OK    | __main__:10]: Success.\n[2023-09-01 00:00:00.000 +09:00 | WARN  | __main__:11]: Warning something.\n[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:12]: Error occured.\n[2023-09-01 00:00:00.000 +09:00 | CRIT  | __main__:13]: CRITICAL ERROR.\n[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:25]: division by zero\n[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:32]: Show me, what value is wrong:\nTraceback (most recent call last):\n\n> File \"/home/user/workspaces/projects/beans_logging/examples/simple/./main.py\", line 30, in <module>\n    nested(0)\n    \u2514 <function nested at 0x10802a4c0>\n\n  File \"/home/user/workspaces/projects/beans_logging/examples/simple/./main.py\", line 23, in nested\n    divide(5, c)\n    \u2502         \u2514 0\n    \u2514 <function divide at 0x1052f31f0>\n\n  File \"/home/user/workspaces/projects/beans_logging/examples/simple/./main.py\", line 17, in divide\n    _result = a / b\n              \u2502   \u2514 0\n              \u2514 5\n\nZeroDivisionError: division by zero\n```\n\n### **FastAPI**\n\nCheckout `beans_logging_fastapi` package: <https://github.com/bybatkhuu/module.fastapi-logging>\n\n- FastAPI HTTP access logging middleware\n- Install with pip: `pip install -U beans-logging[fastapi]` or `pip install -U beans-logging-fastapi`\n\n---\n\n## Running Tests\n\nTo run tests, run the following command:\n\n```sh\n# Install python test dependencies:\npip install -r ./requirements.test.txt\n\n# Run tests:\npython -m pytest -v\n```\n\n## Environment Variables\n\nYou can use the following environment variables inside [**`.env.example`**](https://github.com/bybatkhuu/module.python-logging/blob/main/.env.example) file:\n\n```sh\nENV=development\nDEBUG=true\n\nBEANS_LOGGING_DISABLE_DEFAULT=false\nBEANS_LOGGING_CONFIG_PATH=\"./configs/logger.yml\"\nBEANS_LOGGING_LOGS_DIR=\"./logs\"\n```\n\n## Configuration\n\nYou can use the following configuration template [**`logger.yml`**](https://github.com/bybatkhuu/module.python-logging/blob/main/templates/configs/logger.yml): file:\n\n```yaml\nlogger:\n  # app_name: \"app\"\n  level: \"INFO\"\n  use_diagnose: false\n  stream:\n    use_color: true\n    use_icon: false\n    format_str: \"[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{level_short:<5}</level> | <w>{name}:{line}</w>]: <level>{message}</level>\"\n    std_handler:\n      enabled: true\n  file:\n    logs_dir: \"./logs\"\n    rotate_size: 10000000 # 10MB\n    rotate_time: \"00:00:00\"\n    backup_count: 90\n    log_handlers:\n      enabled: false\n      format_str: \"[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}\"\n      log_path: \"{app_name}.std.all.log\"\n      err_path: \"{app_name}.std.err.log\"\n    json_handlers:\n      enabled: false\n      use_custom: false\n      log_path: \"{app_name}.json.all.log\"\n      err_path: \"{app_name}.json.err.log\"\n  intercept:\n    auto_load:\n      enabled: true\n      only_base: false\n      ignore_modules: []\n    include_modules: []\n    mute_modules: []\n  extra:\n```\n\n## Documentation\n\n- [docs](https://github.com/bybatkhuu/module.python-logging/blob/main/docs/README.md)\n- [scripts](https://github.com/bybatkhuu/module.python-logging/blob/main/docs/scripts/README.md)\n\n---\n\n## References\n\n- <https://github.com/Delgan/loguru>\n- <https://loguru.readthedocs.io/en/stable/api/logger.html>\n- <https://loguru.readthedocs.io/en/stable/resources/recipes.html>\n- <https://github.com/bybatkhuu/module.fastapi-logging>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "'beans_logging' is a python package for simple logger and easily managing logging modules. It is a Loguru based custom logging package for python projects.",
    "version": "6.0.0",
    "project_urls": {
        "Download": "https://github.com/bybatkhuu/module.python-logging/archive/v6.0.0.tar.gz",
        "Homepage": "https://github.com/bybatkhuu/module.python-logging"
    },
    "split_keywords": [
        "beans_logging",
        "loguru",
        "logging",
        "logger",
        "logs",
        "log",
        "print",
        "custom-logging"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4e71e5ddbbd2fcc607aa6597a42bb14a91cee51cef0e0d65db77dfd3129e6fa8",
                "md5": "efcd9f4bd5d428975ef217edbed372c0",
                "sha256": "2a1ccba070c9e36969638a5e6e5f1ab17ad439f5e081163183c1e958d4ae0bc0"
            },
            "downloads": -1,
            "filename": "beans_logging-6.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "efcd9f4bd5d428975ef217edbed372c0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 18201,
            "upload_time": "2023-10-31T04:30:08",
            "upload_time_iso_8601": "2023-10-31T04:30:08.946903Z",
            "url": "https://files.pythonhosted.org/packages/4e/71/e5ddbbd2fcc607aa6597a42bb14a91cee51cef0e0d65db77dfd3129e6fa8/beans_logging-6.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3562dee91ebf5d3d67e9f51c3b5b3340b84464ca89b0b91f8e7771750c35b9ad",
                "md5": "1a8bd3c8bc3148d13a8319e2cfa839df",
                "sha256": "10e7e0e9e0a1a362f64f4b51514677ce0d768ada9880e29aabf627c1de915d65"
            },
            "downloads": -1,
            "filename": "beans_logging-6.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "1a8bd3c8bc3148d13a8319e2cfa839df",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 17634,
            "upload_time": "2023-10-31T04:30:10",
            "upload_time_iso_8601": "2023-10-31T04:30:10.626973Z",
            "url": "https://files.pythonhosted.org/packages/35/62/dee91ebf5d3d67e9f51c3b5b3340b84464ca89b0b91f8e7771750c35b9ad/beans_logging-6.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-31 04:30:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bybatkhuu",
    "github_project": "module.python-logging",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "beans-logging"
}
        
Elapsed time: 1.95176s