fourcats-utils


Namefourcats-utils JSON
Version 0.0.4 PyPI version JSON
download
home_pagehttps://github.com/FourCats-Py/FourCats-Utils
SummaryA common Python toolkit package based on personal habits.
upload_time2022-08-19 06:20:35
maintainer
docs_urlNone
authorShiWeiDong
requires_python>=3.8
license
keywords fourcats utils
VCS
bugtrack_url
requirements certifi loguru mergedict pip PyYAML setuptools urllib3 wheel
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FourCats-Utils

### Statement
 - A common Python toolkit package based on personal habits.
 - The log module is a secondary processing based on `loguru` that conforms to the usage habits.

### Logger

#### Init

- Provides a fast configuration method for application logs.
- It provides a quick configuration method for `Json` logs.
- It provides a fast configuration method for special log processing.

##### Application

```python
import json
from fourcats_utils import logger

# init Please refer to `loguru.add` method.
logger.init_app(sink="./app.log")


# Special processing (day response style)
@logger.app.dispose
def app_dispose(record: dict) -> str:
    # The following content is only the test content (default content). You can customize the logic for style output.
    stderr_formatter = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"

    if record["extra"].get("json", False) is True:
        stderr_formatter += " - <level>{extra[serialized]}</level>"

    if "serialized" not in record["extra"]:
        record["extra"]["serialized"] = json.dumps(dict())

    stderr_formatter += "\n"
    return stderr_formatter

```

##### `Json`

```Python
import copy
import json
import datetime
from fourcats_utils import logger

# init Please refer to `loguru.add` method.
logger.init_json(sink="./json.log")


# Special processing (day response style)
@logger.json.dispose
def app_dispose(record: dict) -> None:
    # The following content is only the test content (default content). You can customize the logic for style output.
    data = copy.copy(record["extra"])
    data.pop("json", "")
    data.update(**dict(
        message=record.get("message", ""),
        level=record.get("level", dict()).name,
        fileline=":".join([record["name"], record["function"], str(record["line"])]),
        datetime=record.get("time", datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S.%f"),
        timestamp=record.get("time", datetime.datetime.now()).timestamp()
    ))
    record["extra"]["serialized"] = json.dumps(data, ensure_ascii=False)
    return

```

##### Stderr

```python
import json
from fourcats_utils import logger


# Special processing (day response style)
@logger.stderr.dispose
def app_dispose(record: dict) -> str:
    # The following content is only the test content (default content). You can customize the logic for style output.
    stderr_formatter = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"

    if record["extra"].get("json", False) is True:
        stderr_formatter += " - <level>{extra[serialized]}</level>"

    if "serialized" not in record["extra"]:
        record["extra"]["serialized"] = json.dumps(dict())

    stderr_formatter += "\n"
    return stderr_formatter

```

##### Bind

```Python
from fourcats_utils import logger

# Use global binding.
logger.global_bind(a=1, b=2)

# Use context binding.
logger.context_bind(c=3, d=4)

```

#### Thread test

```python
import time
import threading
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED

from fourcats_utils.logger import logger


def init():
    logger.init_app(sink="./app.log")
    logger.global_bind(a=1, b=2)
    logger.init_json(sink="./json.log")


def second():
    """"""
    thread_name = threading.currentThread().getName()
    logger.info(f"线程 - {thread_name} 输出内容 - 第二次", json=True, alias=thread_name, state="success")


def first(num):
    thread_name = threading.currentThread().getName()
    logger.context_bind(c=num, d=num ** 2, thread_name=thread_name)
    logger.info(f"线程 - {thread_name} 输出内容", json=True, aaa=thread_name, alias=thread_name)
    time.sleep(1)
    second()


if __name__ == '__main__':
    init()
    executor = ThreadPoolExecutor(max_workers=10)
    tasks = [executor.submit(first, i) for i in range(100)]
    wait(tasks, return_when=ALL_COMPLETED)
```

#### Asyncio test

```python
import asyncio

from fourcats_utils import logger


def init():
    logger.init_app(sink="./app.log")
    logger.global_bind(a=1, b=2)
    logger.init_json(sink="./json.log")


async def second(num):
    """"""
    await asyncio.sleep(1)
    logger.info(f"协程 - {num} 输出内容 - 第二次", json=True, alias=num, state="success")


async def first():
    for i in range(100):
        logger.context_bind(c=i, d=i ** 2, thread_name=i)
        logger.info(f"协程 - {i} 输出内容", json=True, aaa=i, alias=i)
        asyncio.create_task(second(i))
    await asyncio.sleep(10)


if __name__ == '__main__':
    init()
    asyncio.run(first())
```

### The default configuration is the distinction between the Jason log and the application log

- Setting the flag is mainly to display the JSON content you output in the console output (which does not contain the
  flag identification field). If it is not set, it defaults to ` JSON `
- Flag is the keyword parameter you need in the output log method. Currently, boolean type is supported.

```python
from fourcats_utils import logger

# Default
print(logger.flag)
# json

logger.setter_flag(flag="json_logger")
print(logger.flag)

# Output application log
logger.debug("1")

# And output to the application log and the Json log.
# The default is json, but the configuration above has been changed to JSON_ logger
logger.debug("1", json_logger=True)

```

### About customizing the renaming method of log files after cutting.
 - [https://github.com/Delgan/loguru/issues/529](https://github.com/Delgan/loguru/issues/529)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/FourCats-Py/FourCats-Utils",
    "name": "fourcats-utils",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "fourcats,utils",
    "author": "ShiWeiDong",
    "author_email": "shiweidong1993@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8e/1a/e9ef53a03c7e812b39ae5e817f417310a1c04241ec3f80b43d6ba8999e1d/fourcats-utils-0.0.4.tar.gz",
    "platform": null,
    "description": "# FourCats-Utils\n\n### Statement\n - A common Python toolkit package based on personal habits.\n - The log module is a secondary processing based on `loguru` that conforms to the usage habits.\n\n### Logger\n\n#### Init\n\n- Provides a fast configuration method for application logs.\n- It provides a quick configuration method for `Json` logs.\n- It provides a fast configuration method for special log processing.\n\n##### Application\n\n```python\nimport json\nfrom fourcats_utils import logger\n\n# init Please refer to `loguru.add` method.\nlogger.init_app(sink=\"./app.log\")\n\n\n# Special processing (day response style)\n@logger.app.dispose\ndef app_dispose(record: dict) -> str:\n    # The following content is only the test content (default content). You can customize the logic for style output.\n    stderr_formatter = \"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>\"\n\n    if record[\"extra\"].get(\"json\", False) is True:\n        stderr_formatter += \" - <level>{extra[serialized]}</level>\"\n\n    if \"serialized\" not in record[\"extra\"]:\n        record[\"extra\"][\"serialized\"] = json.dumps(dict())\n\n    stderr_formatter += \"\\n\"\n    return stderr_formatter\n\n```\n\n##### `Json`\n\n```Python\nimport copy\nimport json\nimport datetime\nfrom fourcats_utils import logger\n\n# init Please refer to `loguru.add` method.\nlogger.init_json(sink=\"./json.log\")\n\n\n# Special processing (day response style)\n@logger.json.dispose\ndef app_dispose(record: dict) -> None:\n    # The following content is only the test content (default content). You can customize the logic for style output.\n    data = copy.copy(record[\"extra\"])\n    data.pop(\"json\", \"\")\n    data.update(**dict(\n        message=record.get(\"message\", \"\"),\n        level=record.get(\"level\", dict()).name,\n        fileline=\":\".join([record[\"name\"], record[\"function\"], str(record[\"line\"])]),\n        datetime=record.get(\"time\", datetime.datetime.now()).strftime(\"%Y-%m-%d %H:%M:%S.%f\"),\n        timestamp=record.get(\"time\", datetime.datetime.now()).timestamp()\n    ))\n    record[\"extra\"][\"serialized\"] = json.dumps(data, ensure_ascii=False)\n    return\n\n```\n\n##### Stderr\n\n```python\nimport json\nfrom fourcats_utils import logger\n\n\n# Special processing (day response style)\n@logger.stderr.dispose\ndef app_dispose(record: dict) -> str:\n    # The following content is only the test content (default content). You can customize the logic for style output.\n    stderr_formatter = \"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>\"\n\n    if record[\"extra\"].get(\"json\", False) is True:\n        stderr_formatter += \" - <level>{extra[serialized]}</level>\"\n\n    if \"serialized\" not in record[\"extra\"]:\n        record[\"extra\"][\"serialized\"] = json.dumps(dict())\n\n    stderr_formatter += \"\\n\"\n    return stderr_formatter\n\n```\n\n##### Bind\n\n```Python\nfrom fourcats_utils import logger\n\n# Use global binding.\nlogger.global_bind(a=1, b=2)\n\n# Use context binding.\nlogger.context_bind(c=3, d=4)\n\n```\n\n#### Thread test\n\n```python\nimport time\nimport threading\nfrom concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED\n\nfrom fourcats_utils.logger import logger\n\n\ndef init():\n    logger.init_app(sink=\"./app.log\")\n    logger.global_bind(a=1, b=2)\n    logger.init_json(sink=\"./json.log\")\n\n\ndef second():\n    \"\"\"\"\"\"\n    thread_name = threading.currentThread().getName()\n    logger.info(f\"\u7ebf\u7a0b - {thread_name} \u8f93\u51fa\u5185\u5bb9 - \u7b2c\u4e8c\u6b21\", json=True, alias=thread_name, state=\"success\")\n\n\ndef first(num):\n    thread_name = threading.currentThread().getName()\n    logger.context_bind(c=num, d=num ** 2, thread_name=thread_name)\n    logger.info(f\"\u7ebf\u7a0b - {thread_name} \u8f93\u51fa\u5185\u5bb9\", json=True, aaa=thread_name, alias=thread_name)\n    time.sleep(1)\n    second()\n\n\nif __name__ == '__main__':\n    init()\n    executor = ThreadPoolExecutor(max_workers=10)\n    tasks = [executor.submit(first, i) for i in range(100)]\n    wait(tasks, return_when=ALL_COMPLETED)\n```\n\n#### Asyncio test\n\n```python\nimport asyncio\n\nfrom fourcats_utils import logger\n\n\ndef init():\n    logger.init_app(sink=\"./app.log\")\n    logger.global_bind(a=1, b=2)\n    logger.init_json(sink=\"./json.log\")\n\n\nasync def second(num):\n    \"\"\"\"\"\"\n    await asyncio.sleep(1)\n    logger.info(f\"\u534f\u7a0b - {num} \u8f93\u51fa\u5185\u5bb9 - \u7b2c\u4e8c\u6b21\", json=True, alias=num, state=\"success\")\n\n\nasync def first():\n    for i in range(100):\n        logger.context_bind(c=i, d=i ** 2, thread_name=i)\n        logger.info(f\"\u534f\u7a0b - {i} \u8f93\u51fa\u5185\u5bb9\", json=True, aaa=i, alias=i)\n        asyncio.create_task(second(i))\n    await asyncio.sleep(10)\n\n\nif __name__ == '__main__':\n    init()\n    asyncio.run(first())\n```\n\n### The default configuration is the distinction between the Jason log and the application log\n\n- Setting the flag is mainly to display the JSON content you output in the console output (which does not contain the\n  flag identification field). If it is not set, it defaults to ` JSON `\n- Flag is the keyword parameter you need in the output log method. Currently, boolean type is supported.\n\n```python\nfrom fourcats_utils import logger\n\n# Default\nprint(logger.flag)\n# json\n\nlogger.setter_flag(flag=\"json_logger\")\nprint(logger.flag)\n\n# Output application log\nlogger.debug(\"1\")\n\n# And output to the application log and the Json log.\n# The default is json, but the configuration above has been changed to JSON_ logger\nlogger.debug(\"1\", json_logger=True)\n\n```\n\n### About customizing the renaming method of log files after cutting.\n - [https://github.com/Delgan/loguru/issues/529](https://github.com/Delgan/loguru/issues/529)\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A common Python toolkit package based on personal habits.",
    "version": "0.0.4",
    "split_keywords": [
        "fourcats",
        "utils"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ce5ec6befbbd94125a4b5d0f9f98aab776bde32d5fd37306fc0bdf2e32c1f5a4",
                "md5": "fff04cf0e7a48d80a3e9c8e85bf78ac9",
                "sha256": "70d1a6d405259b75740d9e560ba8730b65773938969f7a7f9770a52caaab0d35"
            },
            "downloads": -1,
            "filename": "fourcats_utils-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fff04cf0e7a48d80a3e9c8e85bf78ac9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 20870,
            "upload_time": "2022-08-19T06:20:34",
            "upload_time_iso_8601": "2022-08-19T06:20:34.242069Z",
            "url": "https://files.pythonhosted.org/packages/ce/5e/c6befbbd94125a4b5d0f9f98aab776bde32d5fd37306fc0bdf2e32c1f5a4/fourcats_utils-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8e1ae9ef53a03c7e812b39ae5e817f417310a1c04241ec3f80b43d6ba8999e1d",
                "md5": "78152c61b6f34c1ebb927c88033b5822",
                "sha256": "f2186460c39cd3d788337d1712d4a957e12382b9140d596a3d9c6ffee8d407d5"
            },
            "downloads": -1,
            "filename": "fourcats-utils-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "78152c61b6f34c1ebb927c88033b5822",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 20822,
            "upload_time": "2022-08-19T06:20:35",
            "upload_time_iso_8601": "2022-08-19T06:20:35.463368Z",
            "url": "https://files.pythonhosted.org/packages/8e/1a/e9ef53a03c7e812b39ae5e817f417310a1c04241ec3f80b43d6ba8999e1d/fourcats-utils-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-08-19 06:20:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "FourCats-Py",
    "github_project": "FourCats-Utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "certifi",
            "specs": [
                [
                    "==",
                    "2022.6.15"
                ]
            ]
        },
        {
            "name": "loguru",
            "specs": [
                [
                    "==",
                    "0.6.0"
                ]
            ]
        },
        {
            "name": "mergedict",
            "specs": [
                [
                    "==",
                    "1.0.0"
                ]
            ]
        },
        {
            "name": "pip",
            "specs": [
                [
                    "==",
                    "22.1.2"
                ]
            ]
        },
        {
            "name": "PyYAML",
            "specs": [
                [
                    "==",
                    "6.0"
                ]
            ]
        },
        {
            "name": "setuptools",
            "specs": [
                [
                    "==",
                    "61.2.0"
                ]
            ]
        },
        {
            "name": "urllib3",
            "specs": [
                [
                    "==",
                    "1.26.11"
                ]
            ]
        },
        {
            "name": "wheel",
            "specs": [
                [
                    "==",
                    "0.37.1"
                ]
            ]
        }
    ],
    "lcname": "fourcats-utils"
}
        
Elapsed time: 0.03992s