# mylogging
[](https://pypi.python.org/pypi/mylogging/) [](https://badge.fury.io/py/mylogging) [](https://pepy.tech/project/mylogging) [](https://lgtm.com/projects/g/Malachov/mylogging/context:python) [](https://mylogging.readthedocs.io/en/latest/?badge=latest) [](https://opensource.org/licenses/MIT) [](https://codecov.io/gh/Malachov/mylogging)
My python logging-warning module. It logs to console or to file based on configuration.
1) It's automatically colorized and formatted to be more readable and noticeable (you can immediately see what errors are yours)
2) It's possible to control logs and warnings behavior (ignore, once, always) as in warnings.
3) It's possible to filter messages by level (INFO, DEBUG, WARNING, ERROR, CRITICAL) as in logging.
Motivation for this project is to be able to have one very simple code base for logging and warning at once
and setup logging at one place, not in every project.
You can use one code for logging apps running on server (developers see what happens on server) and the same
code for printing info and warnings from developed library.
## Links
Official documentation - https://mylogging.readthedocs.io/
Official repo - https://github.com/Malachov/mylogging
## Installation
Python >=3.6 (Python 2 is not supported).
Install just with::
pip install mylogging
## Output
This is how the results of examples below look like in console.
<p align="center">
<img src="docs/source/_static/logging.png" width="620" alt="Logging output example"/>
</p>
For log file, just open example.log in your IDE.
This is how the results in log file opened in VS Code look like.
<p align="center">
<img src="docs/source/_static/logging_file.png" width="620" alt="Logging output example"/>
</p>
## Examples
The library is made to be as simple as possible, so configuration should be easy (you don't need
to configure anything actually)... Just setup path to log file (will be created if not exists).
If you do not set it up, log to console will be used.
Change filter (defaults to once) and level (defaults to WARNING) if you need.
Then syntax is same as in logging module. Functions debug, info, warn, error and critical are available.
<!--phmdoctest-setup-->
```python
import mylogging
mylogging.config.level = "WARNING"
mylogging.warn("I am interesting warning.")
```
You can log your caught errors with traceback, where you set level as input parameter. You can use traceback also
with no parameters, traceback type will be used as heading then. Stack trace in this example starts in
the try block.
```python
try:
print(10 / 0)
except ZeroDivisionError:
mylogging.traceback("Maybe try to use something different than 0.")
```
There is also a way how to work with raised errors. Stack trace is then used from the beginning of the script. Exceptions are formatted by default and it's not necessary to setup anything. If you want to turn this feature off, use
```python
mylogging.my_traceback.enhance_excepthook_reset()
```
`format_str` will return edited string (Color, indent and around signs).
`print` function omit the details like file name, line etc. and print formatted text.
```python
mylogging.print("No details about me.")
```
Another function is for ignoring specified warnings from imported libraries. Global warnings settings are edited, so if you use it in some library that other users will use, don't forget to reset user settings after end of your call with `reset_filter_always()` or use it in with `catch_warnings():` block.
Sometimes only message does not work, then ignore it with class and warning type
```python
import warnings
ignored_warnings = ["mean of empty slice"]
ignored_warnings_class_type = [
("TestError", FutureWarning),
]
mylogging.my_warnings.filter_always(ignored_warnings, ignored_warnings_class_type)
warnings.warn("mean of empty slice") # No output
mylogging.my_warnings.reset_filter_always()
```
If somebody is curious how it looks like on light color theme, here it goes...
<p align="center">
<img src="docs/source/_static/logging_white.png" width="620" alt="Logging output example"/>
</p>
## Config
Some config, that can be configured globally for not having to use in each function call.
Config values have docstrings, so description should be visible in IDE help.
`output` - Whether log to file or to console. Can be 'console' or path to file (string or pathlib.Path).
Defaults by "console"
`level` - Set level of severity that will be printed, e.g. DEBUG, ERROR, CRITICAL. Defaults to 'WARNING'.
`filter` - If the same logs, print it always, once or turn all logging off.
Possible values "ignore", "once", "always" or "error". Defaults to "once".
Usually that's everything you will set up. If you need different formatting of output, you can define
`blacklist` - You can filter out some specific messages by content.
`formatter_console_str` or `formatter_file_str` with for example::
"{asctime} {levelname} " + "{filename}:{lineno}" + "{message}"
Rest options should be OK by default, but it's all up to you of course: You can set up for example
`around` - Whether separate logs with line breaks and ==== or shrink to save space. Defaults to True.
`colorize` - Possible options: [True, False, 'auto']. Colorize is automated. If to console, it is
colorized, if to file, it's not (.log files can be colorized by IDE). Defaults to 'auto'.
`to_list` - You can save all the logs in the list and log it later (use case: used in multiprocessing
processes to be able to use once filter)
`stream` - If you want to use a stream (for example io.StringIO)
logger
=======
It's possible to use logger in any other way if you need (though it's usually not necessary), you can find used logger in logger_module. There are also used filters and handlers.
multiprocessing
===============
If using in subprocesses, to be able to use filters (just once), it's possible to redirect logs and warnings, send as results as log later in main process
```python
logs_list = []
warnings_list = []
logs_redirect = mylogging.misc.redirect_logs_and_warnings(logs_list, warnings_list)
logs_redirect.close_redirect()
mylogging.misc.log_and_warn_from_lists(logs_list, warnings_list)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Malachov/mylogging",
"name": "mylogging",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "",
"author": "Daniel Malachov",
"author_email": "malachovd@seznam.cz",
"download_url": "https://files.pythonhosted.org/packages/3e/87/909ff2523c59e04f2bbc2b7554b1268cdf08aaacfd61c983de4b1a5682ed/mylogging-4.0.6.tar.gz",
"platform": "any",
"description": "# mylogging\n\n[](https://pypi.python.org/pypi/mylogging/) [](https://badge.fury.io/py/mylogging) [](https://pepy.tech/project/mylogging) [](https://lgtm.com/projects/g/Malachov/mylogging/context:python) [](https://mylogging.readthedocs.io/en/latest/?badge=latest) [](https://opensource.org/licenses/MIT) [](https://codecov.io/gh/Malachov/mylogging)\n\n\nMy python logging-warning module. It logs to console or to file based on configuration.\n\n1) It's automatically colorized and formatted to be more readable and noticeable (you can immediately see what errors are yours)\n2) It's possible to control logs and warnings behavior (ignore, once, always) as in warnings.\n3) It's possible to filter messages by level (INFO, DEBUG, WARNING, ERROR, CRITICAL) as in logging.\n\nMotivation for this project is to be able to have one very simple code base for logging and warning at once\nand setup logging at one place, not in every project.\n\nYou can use one code for logging apps running on server (developers see what happens on server) and the same\ncode for printing info and warnings from developed library.\n\n## Links\n\nOfficial documentation - https://mylogging.readthedocs.io/\n\nOfficial repo - https://github.com/Malachov/mylogging\n\n\n## Installation\n\nPython >=3.6 (Python 2 is not supported).\n\nInstall just with::\n\n pip install mylogging\n\n\n## Output\n\nThis is how the results of examples below look like in console.\n\n<p align=\"center\">\n<img src=\"docs/source/_static/logging.png\" width=\"620\" alt=\"Logging output example\"/>\n</p>\n\nFor log file, just open example.log in your IDE.\nThis is how the results in log file opened in VS Code look like.\n\n<p align=\"center\">\n<img src=\"docs/source/_static/logging_file.png\" width=\"620\" alt=\"Logging output example\"/>\n</p>\n\n## Examples\n\nThe library is made to be as simple as possible, so configuration should be easy (you don't need\nto configure anything actually)... Just setup path to log file (will be created if not exists).\nIf you do not set it up, log to console will be used.\nChange filter (defaults to once) and level (defaults to WARNING) if you need.\nThen syntax is same as in logging module. Functions debug, info, warn, error and critical are available.\n\n<!--phmdoctest-setup-->\n```python\nimport mylogging\n\nmylogging.config.level = \"WARNING\"\nmylogging.warn(\"I am interesting warning.\")\n```\n\nYou can log your caught errors with traceback, where you set level as input parameter. You can use traceback also\nwith no parameters, traceback type will be used as heading then. Stack trace in this example starts in \nthe try block.\n\n```python\ntry:\n print(10 / 0)\nexcept ZeroDivisionError:\n mylogging.traceback(\"Maybe try to use something different than 0.\")\n```\n\nThere is also a way how to work with raised errors. Stack trace is then used from the beginning of the script. Exceptions are formatted by default and it's not necessary to setup anything. If you want to turn this feature off, use\n\n```python\nmylogging.my_traceback.enhance_excepthook_reset()\n```\n\n`format_str` will return edited string (Color, indent and around signs).\n\n\n`print` function omit the details like file name, line etc. and print formatted text.\n\n```python\nmylogging.print(\"No details about me.\")\n```\n\nAnother function is for ignoring specified warnings from imported libraries. Global warnings settings are edited, so if you use it in some library that other users will use, don't forget to reset user settings after end of your call with `reset_filter_always()` or use it in with `catch_warnings():` block.\n\nSometimes only message does not work, then ignore it with class and warning type\n\n```python\nimport warnings\n\nignored_warnings = [\"mean of empty slice\"]\nignored_warnings_class_type = [\n (\"TestError\", FutureWarning),\n]\n\nmylogging.my_warnings.filter_always(ignored_warnings, ignored_warnings_class_type)\n\nwarnings.warn(\"mean of empty slice\") # No output\n\nmylogging.my_warnings.reset_filter_always()\n```\n\nIf somebody is curious how it looks like on light color theme, here it goes...\n\n<p align=\"center\">\n<img src=\"docs/source/_static/logging_white.png\" width=\"620\" alt=\"Logging output example\"/>\n</p>\n\n## Config\n\nSome config, that can be configured globally for not having to use in each function call.\n\nConfig values have docstrings, so description should be visible in IDE help.\n\n`output` - Whether log to file or to console. Can be 'console' or path to file (string or pathlib.Path).\nDefaults by \"console\"\n\n`level` - Set level of severity that will be printed, e.g. DEBUG, ERROR, CRITICAL. Defaults to 'WARNING'.\n\n`filter` - If the same logs, print it always, once or turn all logging off.\nPossible values \"ignore\", \"once\", \"always\" or \"error\". Defaults to \"once\".\n\nUsually that's everything you will set up. If you need different formatting of output, you can define\n\n`blacklist` - You can filter out some specific messages by content.\n\n`formatter_console_str` or `formatter_file_str` with for example::\n\n \"{asctime} {levelname} \" + \"{filename}:{lineno}\" + \"{message}\"\n\nRest options should be OK by default, but it's all up to you of course: You can set up for example\n\n`around` - Whether separate logs with line breaks and ==== or shrink to save space. Defaults to True.\n\n`colorize` - Possible options: [True, False, 'auto']. Colorize is automated. If to console, it is\ncolorized, if to file, it's not (.log files can be colorized by IDE). Defaults to 'auto'.\n\n`to_list` - You can save all the logs in the list and log it later (use case: used in multiprocessing\nprocesses to be able to use once filter)\n\n`stream` - If you want to use a stream (for example io.StringIO)\n\nlogger\n=======\n\nIt's possible to use logger in any other way if you need (though it's usually not necessary), you can find used logger in logger_module. There are also used filters and handlers.\n\nmultiprocessing\n===============\n\nIf using in subprocesses, to be able to use filters (just once), it's possible to redirect logs and warnings, send as results as log later in main process\n\n```python\nlogs_list = []\nwarnings_list = []\n\nlogs_redirect = mylogging.misc.redirect_logs_and_warnings(logs_list, warnings_list)\n\nlogs_redirect.close_redirect()\n\nmylogging.misc.log_and_warn_from_lists(logs_list, warnings_list)\n```\n",
"bugtrack_url": null,
"license": "mit",
"summary": "Small library for printing warnings and creating logs.",
"version": "4.0.6",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "23f5732877dd2d18a3c89aa445e9260d25a905ded32439f95a7447b92c7f6c7c",
"md5": "6ac830eecab5dac0a689ffb6a19b7878",
"sha256": "59a8c1cb1b1797557178634a219ff21097462f07a3509fb4afb322763ef616ae"
},
"downloads": -1,
"filename": "mylogging-4.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6ac830eecab5dac0a689ffb6a19b7878",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 25823,
"upload_time": "2022-06-08T18:18:46",
"upload_time_iso_8601": "2022-06-08T18:18:46.178692Z",
"url": "https://files.pythonhosted.org/packages/23/f5/732877dd2d18a3c89aa445e9260d25a905ded32439f95a7447b92c7f6c7c/mylogging-4.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3e87909ff2523c59e04f2bbc2b7554b1268cdf08aaacfd61c983de4b1a5682ed",
"md5": "85a0b0b5c9a647daeca24f417933d439",
"sha256": "53a2a28d3fda8dbaf783520bb1266ad90b40f3f09603dad8c7b927ce55b42a3f"
},
"downloads": -1,
"filename": "mylogging-4.0.6.tar.gz",
"has_sig": false,
"md5_digest": "85a0b0b5c9a647daeca24f417933d439",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 21255,
"upload_time": "2022-06-08T18:18:48",
"upload_time_iso_8601": "2022-06-08T18:18:48.390448Z",
"url": "https://files.pythonhosted.org/packages/3e/87/909ff2523c59e04f2bbc2b7554b1268cdf08aaacfd61c983de4b1a5682ed/mylogging-4.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-06-08 18:18:48",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Malachov",
"github_project": "mylogging",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "pygments",
"specs": []
},
{
"name": "typing_extensions",
"specs": []
},
{
"name": "typeguard",
"specs": []
}
],
"lcname": "mylogging"
}