Name | colorful-logger JSON |
Version |
0.2.0
JSON |
| download |
home_page | None |
Summary | A colorful logger for python3. |
upload_time | 2024-06-18 12:33:02 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3 |
license | MIT License Copyright (c) 2021 thep0y 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 |
log
logger
logging
colorful
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<h1 align="center">Python Colorful Logger</h1>
<p align="center">
<a href="https://pepy.tech/project/colorful-logger"><img alt="Downloads" src="https://static.pepy.tech/badge/colorful-logger"></a>
<a href="https://pepy.tech/project/colorful-logger"><img alt="Downloads" src="https://static.pepy.tech/badge/colorful-logger/month"></a>
<a href="https://pepy.tech/project/colorful-logger"><img alt="Downloads" src="https://static.pepy.tech/badge/colorful-logger/week"></a>
</p>
A colorful logger for python3.
## How to use
### Install
```shell
pip install colorful-logger
```
### Usage
#### 1 Default Logger
You can directly use the default logger. Colored logs will be printed on the terminal. The default logger level is **warning**.
```python
from colorful_logger import logger
with logger:
logger.debug("default logger")
logger.info("default logger")
logger.warning("default logger")
logger.error("default logger")
```
`logger` needs to be executed inside a `with` statement, because this package uses `QueueListener` for log output. You need to call the `start` method before using `logger` to output logs, and call `stop` after you are done. I encapsulated these two methods inside `the` with statement. In most cases, there is no need to call `start` and `stop` separately.
![image-20230221100744751](https://s2.loli.net/2023/02/21/yXh5d9n4vO1mW3x.png)
#### 2 Custom Logger
You can also change the log level, save logs to a file, change the logger name, etc. Logs may not be printed to the terminal.
```python
from colorful_logger import get_logger, DEBUG
def demo_logger(to_file=False):
file = "test_%d.log"
l1 = get_logger(
"demo",
DEBUG,
add_file_path=False,
disable_line_number_filter=False,
file_path=file % 1 if to_file else None,
)
with l1:
l1.debug("without file path")
l1.info("without file path")
l1.warning("without file path")
l1.error("without file path")
l2 = get_logger(
"demo",
DEBUG,
add_file_path=True,
disable_line_number_filter=False,
file_path=file % 2 if to_file else None,
)
with l2:
l2.debug("with file path")
l2.info("with file path")
l2.warning("with file path")
l2.error("with file path")
l3 = get_logger(
None,
DEBUG,
add_file_path=True,
disable_line_number_filter=True,
file_path=file % 3 if to_file else None,
)
with l3:
l3.debug("without name, and with path")
l3.info("without name, and with path")
l3.warning("without name, and with path")
l3.error("without name, and with path")
l4 = get_logger(
None,
DEBUG,
add_file_path=False,
disable_line_number_filter=True,
file_path=file % 4 if to_file else None,
)
with l4:
l4.debug("without name and path")
l4.info("without name and path")
l4.warning("without name and path")
l4.error("without name and path")
l5 = get_logger(None, DEBUG, asynchronous=False)
l5.debug("Synchronization log")
l5.info("Synchronization log")
l5.warning("Synchronization log")
l5.error("Synchronization log")
```
There may be unexpected behavior when logging outside of the `with` statement.
![截屏2023-08-06 11.40.48](https://s1.ax1x.com/2023/08/06/pPAUNFO.png)
Contents of the log file `./test.log` (example, inconsistent with the image above):
```
[90m10:09:33.146[0m [35mDEB[0m [36mdemo[0m[1m:26[0m [96m-[0m without file path
[90m10:09:33.146[0m [32mINF[0m [36mdemo[0m [96m-[0m without file path
[90m10:09:33.146[0m [33mWAR[0m [36mdemo[0m [96m-[0m without file path
[90m10:09:33.146[0m [91mERR[0m [36mdemo[0m[1m:29[0m [96m-[0m without file path
```
The log file does not contain color logs by default.
To save color logs to a file, set `file_colorful` to `True`. In this example, color logs are saved.
The only purpose of the color log file is to view logs in real-time in the terminal:
- Unix
```shell
tail -f test.log
# or
cat test.log
```
- Windows
```powershell
Get-Content -Path -Wait test.log
```
##### Synchronous
If you don't want to log asynchronously, you can create a synchronous logger by passing `asynchronous=False`. In the example above, `l5` is a synchronous logger. When using a synchronous logger, you don't need to wrap the logs in a `with` statement.
#### 3 Child Logger
After defining a `logger`, I want to use all the parameters of this `logger` except for `name` to output logs. You need to use the `child_logger` method to generate a child logger. The child logger needs to be executed inside the `with` statement of the parent logger:
```python
from colorful_logger import get_logger, DEBUG
# parent logger
logger = get_logger(name="sample_logger", level=DEBUG, file_path="./test.log")
with logger:
logger.error("parent error")
l1 = logger.child("l1")
l1.error("l1 error")
l1.fatal("l1 fatal")
```
The child logger is the same as the parent logger except for the name. It will not log third-party libraries.
Executing the child logger inside the `with` statement of the parent logger does not mean it has to be called directly inside the `with`. It can be executed inside a function in the `with` statement:
```python
# log.py
from colorful_logger import get_logger, DEBUG
logger = get_logger(name="sample_logger", level=DEBUG, file_path="./test.log")
```
```python
# main.py
from log import logger
from other_file import test
with logger:
test()
```
```python
# other_file.py
test_logger = logger.child("test_logger")
def test():
test_logger.error("test error")
```
Raw data
{
"_id": null,
"home_page": null,
"name": "colorful-logger",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3",
"maintainer_email": null,
"keywords": "log, logger, logging, colorful",
"author": null,
"author_email": "thep0y <thepoy@163.com>",
"download_url": "https://files.pythonhosted.org/packages/e0/04/080943bcd5482aead7fb2f2874d4ff63a83358d3da9b21fb9c5900aa4611/colorful_logger-0.2.0.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">Python Colorful Logger</h1>\n\n<p align=\"center\">\n <a href=\"https://pepy.tech/project/colorful-logger\"><img alt=\"Downloads\" src=\"https://static.pepy.tech/badge/colorful-logger\"></a> \n <a href=\"https://pepy.tech/project/colorful-logger\"><img alt=\"Downloads\" src=\"https://static.pepy.tech/badge/colorful-logger/month\"></a>\n <a href=\"https://pepy.tech/project/colorful-logger\"><img alt=\"Downloads\" src=\"https://static.pepy.tech/badge/colorful-logger/week\"></a>\n</p>\n\nA colorful logger for python3.\n\n## How to use\n\n### Install\n\n```shell\npip install colorful-logger\n```\n\n### Usage\n\n#### 1 Default Logger\n\nYou can directly use the default logger. Colored logs will be printed on the terminal. The default logger level is **warning**.\n\n```python\nfrom colorful_logger import logger\n\nwith logger:\n logger.debug(\"default logger\")\n logger.info(\"default logger\")\n logger.warning(\"default logger\")\n logger.error(\"default logger\")\n```\n\n`logger` needs to be executed inside a `with` statement, because this package uses `QueueListener` for log output. You need to call the `start` method before using `logger` to output logs, and call `stop` after you are done. I encapsulated these two methods inside `the` with statement. In most cases, there is no need to call `start` and `stop` separately.\n\n![image-20230221100744751](https://s2.loli.net/2023/02/21/yXh5d9n4vO1mW3x.png)\n\n#### 2 Custom Logger\n\nYou can also change the log level, save logs to a file, change the logger name, etc. Logs may not be printed to the terminal.\n\n```python\nfrom colorful_logger import get_logger, DEBUG\n\n\ndef demo_logger(to_file=False):\n file = \"test_%d.log\"\n\n l1 = get_logger(\n \"demo\",\n DEBUG,\n add_file_path=False,\n disable_line_number_filter=False,\n file_path=file % 1 if to_file else None,\n )\n with l1:\n l1.debug(\"without file path\")\n l1.info(\"without file path\")\n l1.warning(\"without file path\")\n l1.error(\"without file path\")\n\n l2 = get_logger(\n \"demo\",\n DEBUG,\n add_file_path=True,\n disable_line_number_filter=False,\n file_path=file % 2 if to_file else None,\n )\n with l2:\n l2.debug(\"with file path\")\n l2.info(\"with file path\")\n l2.warning(\"with file path\")\n l2.error(\"with file path\")\n\n l3 = get_logger(\n None,\n DEBUG,\n add_file_path=True,\n disable_line_number_filter=True,\n file_path=file % 3 if to_file else None,\n )\n with l3:\n l3.debug(\"without name, and with path\")\n l3.info(\"without name, and with path\")\n l3.warning(\"without name, and with path\")\n l3.error(\"without name, and with path\")\n\n l4 = get_logger(\n None,\n DEBUG,\n add_file_path=False,\n disable_line_number_filter=True,\n file_path=file % 4 if to_file else None,\n )\n with l4:\n l4.debug(\"without name and path\")\n l4.info(\"without name and path\")\n l4.warning(\"without name and path\")\n l4.error(\"without name and path\")\n \n l5 = get_logger(None, DEBUG, asynchronous=False)\n l5.debug(\"Synchronization log\")\n l5.info(\"Synchronization log\")\n l5.warning(\"Synchronization log\")\n l5.error(\"Synchronization log\")\n```\n\nThere may be unexpected behavior when logging outside of the `with` statement.\n\n![\u622a\u5c4f2023-08-06 11.40.48](https://s1.ax1x.com/2023/08/06/pPAUNFO.png)\n\nContents of the log file `./test.log` (example, inconsistent with the image above):\n\n```\n\u001b[90m10:09:33.146\u001b[0m \u001b[35mDEB\u001b[0m \u001b[36mdemo\u001b[0m\u001b[1m:26\u001b[0m \u001b[96m-\u001b[0m without file path\n\u001b[90m10:09:33.146\u001b[0m \u001b[32mINF\u001b[0m \u001b[36mdemo\u001b[0m \u001b[96m-\u001b[0m without file path\n\u001b[90m10:09:33.146\u001b[0m \u001b[33mWAR\u001b[0m \u001b[36mdemo\u001b[0m \u001b[96m-\u001b[0m without file path\n\u001b[90m10:09:33.146\u001b[0m \u001b[91mERR\u001b[0m \u001b[36mdemo\u001b[0m\u001b[1m:29\u001b[0m \u001b[96m-\u001b[0m without file path\n```\n\nThe log file does not contain color logs by default.\n\nTo save color logs to a file, set `file_colorful` to `True`. In this example, color logs are saved.\n\nThe only purpose of the color log file is to view logs in real-time in the terminal:\n\n- Unix\n\n```shell\ntail -f test.log\n# or\ncat test.log\n```\n\n- Windows\n\n```powershell\nGet-Content -Path -Wait test.log\n```\n\n##### Synchronous\n\nIf you don't want to log asynchronously, you can create a synchronous logger by passing `asynchronous=False`. In the example above, `l5` is a synchronous logger. When using a synchronous logger, you don't need to wrap the logs in a `with` statement.\n\n#### 3 Child Logger\n\nAfter defining a `logger`, I want to use all the parameters of this `logger` except for `name` to output logs. You need to use the `child_logger` method to generate a child logger. The child logger needs to be executed inside the `with` statement of the parent logger:\n\n```python\nfrom colorful_logger import get_logger, DEBUG\n\n# parent logger\nlogger = get_logger(name=\"sample_logger\", level=DEBUG, file_path=\"./test.log\")\n\nwith logger:\n logger.error(\"parent error\")\n l1 = logger.child(\"l1\")\n l1.error(\"l1 error\")\n l1.fatal(\"l1 fatal\")\n```\n\nThe child logger is the same as the parent logger except for the name. It will not log third-party libraries.\n\nExecuting the child logger inside the `with` statement of the parent logger does not mean it has to be called directly inside the `with`. It can be executed inside a function in the `with` statement:\n\n```python\n# log.py\nfrom colorful_logger import get_logger, DEBUG\n\nlogger = get_logger(name=\"sample_logger\", level=DEBUG, file_path=\"./test.log\")\n```\n\n```python\n# main.py\nfrom log import logger\nfrom other_file import test\n\nwith logger:\n test()\n```\n\n```python\n# other_file.py\n\ntest_logger = logger.child(\"test_logger\")\n\ndef test():\n test_logger.error(\"test error\")\n```\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2021 thep0y 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": "A colorful logger for python3.",
"version": "0.2.0",
"project_urls": {
"homepage": "https://github.com/thep0y/python-logger",
"repository": "https://github.com/thep0y/python-logger"
},
"split_keywords": [
"log",
" logger",
" logging",
" colorful"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4404833035963cdf0b9119eb928c0d1923114c17fe3455df0fa5ce27d6896826",
"md5": "f93438586f907c338c35de89ddb02583",
"sha256": "2b4d9e4307719f87086cd751f2f4bf5ca5f10253ae147a36601e05deac2e7f9c"
},
"downloads": -1,
"filename": "colorful_logger-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f93438586f907c338c35de89ddb02583",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3",
"size": 12863,
"upload_time": "2024-06-18T12:33:00",
"upload_time_iso_8601": "2024-06-18T12:33:00.717085Z",
"url": "https://files.pythonhosted.org/packages/44/04/833035963cdf0b9119eb928c0d1923114c17fe3455df0fa5ce27d6896826/colorful_logger-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e004080943bcd5482aead7fb2f2874d4ff63a83358d3da9b21fb9c5900aa4611",
"md5": "49248186dbae41b0545c5e3ad2bf61d8",
"sha256": "6dc70873fb9298a0210b64a3661d0e2559be10776515852cbfda9af088b5b276"
},
"downloads": -1,
"filename": "colorful_logger-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "49248186dbae41b0545c5e3ad2bf61d8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3",
"size": 15318,
"upload_time": "2024-06-18T12:33:02",
"upload_time_iso_8601": "2024-06-18T12:33:02.868681Z",
"url": "https://files.pythonhosted.org/packages/e0/04/080943bcd5482aead7fb2f2874d4ff63a83358d3da9b21fb9c5900aa4611/colorful_logger-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-18 12:33:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "thep0y",
"github_project": "python-logger",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "colorful-logger"
}