risclog.logging 1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- risclog.logging-1.0/AUTHORS.rst +13 -0
- risclog.logging-1.0/CHANGES.rst +9 -0
- risclog.logging-1.0/CONTRIBUTING.rst +123 -0
- risclog.logging-1.0/LICENSE +21 -0
- risclog.logging-1.0/MANIFEST.in +32 -0
- risclog.logging-1.0/PKG-INFO +234 -0
- risclog.logging-1.0/README.rst +187 -0
- risclog.logging-1.0/docs/Makefile +20 -0
- risclog.logging-1.0/docs/authors.rst +1 -0
- risclog.logging-1.0/docs/conf.py +169 -0
- risclog.logging-1.0/docs/contributing.rst +1 -0
- risclog.logging-1.0/docs/history.rst +1 -0
- risclog.logging-1.0/docs/index.rst +20 -0
- risclog.logging-1.0/docs/installation.rst +51 -0
- risclog.logging-1.0/docs/make.bat +36 -0
- risclog.logging-1.0/docs/readme.rst +1 -0
- risclog.logging-1.0/docs/usage.rst +7 -0
- risclog.logging-1.0/pyproject.toml +57 -0
- risclog.logging-1.0/setup.cfg +4 -0
- risclog.logging-1.0/setup.py +62 -0
- risclog.logging-1.0/src/risclog/__init__.py +1 -0
- risclog.logging-1.0/src/risclog/logging/__init__.py +414 -0
- risclog.logging-1.0/src/risclog/logging/conftest.py +13 -0
- risclog.logging-1.0/src/risclog/logging/tests/__init__.py +0 -0
- risclog.logging-1.0/src/risclog/logging/tests/test_logger.py +284 -0
- risclog.logging-1.0/src/risclog.logging.egg-info/PKG-INFO +234 -0
- risclog.logging-1.0/src/risclog.logging.egg-info/SOURCES.txt +30 -0
- risclog.logging-1.0/src/risclog.logging.egg-info/dependency_links.txt +1 -0
- risclog.logging-1.0/src/risclog.logging.egg-info/namespace_packages.txt +1 -0
- risclog.logging-1.0/src/risclog.logging.egg-info/not-zip-safe +1 -0
- risclog.logging-1.0/src/risclog.logging.egg-info/requires.txt +18 -0
- risclog.logging-1.0/src/risclog.logging.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
.. highlight:: shell
|
|
2
|
+
|
|
3
|
+
============
|
|
4
|
+
Contributing
|
|
5
|
+
============
|
|
6
|
+
|
|
7
|
+
Contributions are welcome, and they are greatly appreciated! Every little bit
|
|
8
|
+
helps, and credit will always be given.
|
|
9
|
+
|
|
10
|
+
You can contribute in many ways:
|
|
11
|
+
|
|
12
|
+
Types of Contributions
|
|
13
|
+
----------------------
|
|
14
|
+
|
|
15
|
+
Report Bugs
|
|
16
|
+
~~~~~~~~~~~
|
|
17
|
+
|
|
18
|
+
Report bugs at https://github.com/risclog-solution/risclog.logging/issues.
|
|
19
|
+
|
|
20
|
+
If you are reporting a bug, please include:
|
|
21
|
+
|
|
22
|
+
* Your operating system name and version.
|
|
23
|
+
* Any details about your local setup that might be helpful in troubleshooting.
|
|
24
|
+
* Detailed steps to reproduce the bug.
|
|
25
|
+
|
|
26
|
+
Fix Bugs
|
|
27
|
+
~~~~~~~~
|
|
28
|
+
|
|
29
|
+
Look through the GitHub issues for bugs. Anything tagged with "bug" and "help
|
|
30
|
+
wanted" is open to whoever wants to implement it.
|
|
31
|
+
|
|
32
|
+
Implement Features
|
|
33
|
+
~~~~~~~~~~~~~~~~~~
|
|
34
|
+
|
|
35
|
+
Look through the GitHub issues for features. Anything tagged with "enhancement"
|
|
36
|
+
and "help wanted" is open to whoever wants to implement it.
|
|
37
|
+
|
|
38
|
+
Write Documentation
|
|
39
|
+
~~~~~~~~~~~~~~~~~~~
|
|
40
|
+
|
|
41
|
+
risclog.logging could always use more documentation, whether as part of the
|
|
42
|
+
official risclog.logging docs, in docstrings, or even on the web in blog posts,
|
|
43
|
+
articles, and such.
|
|
44
|
+
|
|
45
|
+
Submit Feedback
|
|
46
|
+
~~~~~~~~~~~~~~~
|
|
47
|
+
|
|
48
|
+
The best way to send feedback is to file an issue at https://github.com/risclog-solution/risclog.logging/issues.
|
|
49
|
+
|
|
50
|
+
If you are proposing a feature:
|
|
51
|
+
|
|
52
|
+
* Explain in detail how it would work.
|
|
53
|
+
* Keep the scope as narrow as possible, to make it easier to implement.
|
|
54
|
+
* Remember that this is a volunteer-driven project, and that contributions
|
|
55
|
+
are welcome :)
|
|
56
|
+
|
|
57
|
+
Get Started!
|
|
58
|
+
------------
|
|
59
|
+
|
|
60
|
+
Ready to contribute? Here's how to set up `risclog.logging` for local development.
|
|
61
|
+
|
|
62
|
+
1. Fork the `risclog.logging` repo on GitHub.
|
|
63
|
+
2. Clone your fork locally::
|
|
64
|
+
|
|
65
|
+
$ git clone git@github.com:your_name_here/risclog.logging.git
|
|
66
|
+
|
|
67
|
+
3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development::
|
|
68
|
+
|
|
69
|
+
$ mkvirtualenv risclog.logging
|
|
70
|
+
$ cd risclog.logging/
|
|
71
|
+
$ python setup.py develop
|
|
72
|
+
|
|
73
|
+
4. Create a branch for local development::
|
|
74
|
+
|
|
75
|
+
$ git checkout -b name-of-your-bugfix-or-feature
|
|
76
|
+
|
|
77
|
+
Now you can make your changes locally.
|
|
78
|
+
|
|
79
|
+
5. When you're done making changes, check that your changes pass flake8 and the
|
|
80
|
+
tests::
|
|
81
|
+
|
|
82
|
+
$ ./pytest
|
|
83
|
+
|
|
84
|
+
6. Commit your changes and push your branch to GitHub::
|
|
85
|
+
|
|
86
|
+
$ git add .
|
|
87
|
+
$ git commit -m "Your detailed description of your changes."
|
|
88
|
+
$ git push origin name-of-your-bugfix-or-feature
|
|
89
|
+
|
|
90
|
+
7. Submit a pull request through the GitHub website.
|
|
91
|
+
|
|
92
|
+
Pull Request Guidelines
|
|
93
|
+
-----------------------
|
|
94
|
+
|
|
95
|
+
Before you submit a pull request, check that it meets these guidelines:
|
|
96
|
+
|
|
97
|
+
1. The pull request should include tests.
|
|
98
|
+
2. If the pull request adds functionality, the docs should be updated. Put
|
|
99
|
+
your new functionality into a function with a docstring, and add the
|
|
100
|
+
feature to the list in README.rst.
|
|
101
|
+
3. The pull request should work for Python 3.10 and 3.11. Check
|
|
102
|
+
https://travis-ci.com/risclog-solution/risclog.logging/pull_requests
|
|
103
|
+
and make sure that the tests pass for all supported Python versions.
|
|
104
|
+
|
|
105
|
+
Tips
|
|
106
|
+
----
|
|
107
|
+
|
|
108
|
+
To run a subset of tests::
|
|
109
|
+
|
|
110
|
+
$ ./pytest -k test_main
|
|
111
|
+
|
|
112
|
+
Deploying
|
|
113
|
+
---------
|
|
114
|
+
|
|
115
|
+
A reminder for the maintainers on how to deploy.
|
|
116
|
+
Make sure all your changes are committed (including an entry in HISTORY.rst).
|
|
117
|
+
Then run::
|
|
118
|
+
|
|
119
|
+
$ bump2version patch # possible: major / minor / patch
|
|
120
|
+
$ git push
|
|
121
|
+
$ git push --tags
|
|
122
|
+
|
|
123
|
+
Travis will then deploy to PyPI if tests pass.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, riscLOG Solution GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
include AUTHORS.rst
|
|
2
|
+
include CHANGES.rst
|
|
3
|
+
include CONTRIBUTING.rst
|
|
4
|
+
include HISTORY.rst
|
|
5
|
+
include LICENSE
|
|
6
|
+
include README.rst
|
|
7
|
+
include src/risclog/logging/alembic/README
|
|
8
|
+
|
|
9
|
+
recursive-include tests *
|
|
10
|
+
recursive-exclude * __pycache__
|
|
11
|
+
recursive-exclude * *.py[co]
|
|
12
|
+
recursive-include src *.mako
|
|
13
|
+
recursive-include src *.py
|
|
14
|
+
|
|
15
|
+
recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif
|
|
16
|
+
recursive-include src *.pdf
|
|
17
|
+
exclude .env
|
|
18
|
+
|
|
19
|
+
exclude requirements.lock
|
|
20
|
+
exclude requirements.txt
|
|
21
|
+
exclude appenv
|
|
22
|
+
exclude pytest
|
|
23
|
+
exclude pytest.ini
|
|
24
|
+
exclude .editorconfig
|
|
25
|
+
exclude .pre-commit-config.yaml
|
|
26
|
+
exclude .isort.cfg
|
|
27
|
+
exclude .env*
|
|
28
|
+
exclude uvicorn
|
|
29
|
+
exclude main
|
|
30
|
+
exclude alembic
|
|
31
|
+
exclude alembic.ini
|
|
32
|
+
exclude Dockerfile
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: risclog.logging
|
|
3
|
+
Version: 1.0
|
|
4
|
+
Summary: A logger based on structlog
|
|
5
|
+
Home-page: https://github.com/risclog-solution/risclog.logging
|
|
6
|
+
Author: riscLOG Solution GmbH
|
|
7
|
+
Author-email: info@risclog.de
|
|
8
|
+
License: MIT license
|
|
9
|
+
Keywords: risclog.logging
|
|
10
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Natural Language :: German
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Requires-Python: >=3.6
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
License-File: AUTHORS.rst
|
|
20
|
+
Requires-Dist: structlog
|
|
21
|
+
Provides-Extra: docs
|
|
22
|
+
Requires-Dist: Sphinx; extra == "docs"
|
|
23
|
+
Provides-Extra: test
|
|
24
|
+
Requires-Dist: pytest-cache; extra == "test"
|
|
25
|
+
Requires-Dist: pytest-cov; extra == "test"
|
|
26
|
+
Requires-Dist: pytest-flake8; extra == "test"
|
|
27
|
+
Requires-Dist: pytest-rerunfailures; extra == "test"
|
|
28
|
+
Requires-Dist: pytest-sugar; extra == "test"
|
|
29
|
+
Requires-Dist: pytest; extra == "test"
|
|
30
|
+
Requires-Dist: coverage; extra == "test"
|
|
31
|
+
Requires-Dist: flake8<4; extra == "test"
|
|
32
|
+
Requires-Dist: mock; extra == "test"
|
|
33
|
+
Requires-Dist: requests; extra == "test"
|
|
34
|
+
Requires-Dist: httpx; extra == "test"
|
|
35
|
+
Requires-Dist: pytest-asyncio; extra == "test"
|
|
36
|
+
|
|
37
|
+
===============
|
|
38
|
+
risclog.logging
|
|
39
|
+
===============
|
|
40
|
+
|
|
41
|
+
.. image:: https://github.com/risclog-solution/risclog.logging/workflows/Test/badge.svg?branch=master
|
|
42
|
+
:target: https://github.com/risclog-solution/risclog.logging/actions?workflow=Test
|
|
43
|
+
:alt: CI Status
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
.. image:: https://img.shields.io/pypi/v/risclog.logging.svg
|
|
47
|
+
:target: https://pypi.python.org/pypi/risclog.logging
|
|
48
|
+
|
|
49
|
+
.. image:: https://img.shields.io/travis/risclog-solution/risclog.logging.svg
|
|
50
|
+
:target: https://travis-ci.com/risclog-solution/risclog.logging
|
|
51
|
+
|
|
52
|
+
.. image:: https://readthedocs.org/projects/risclog.logging/badge/?version=latest
|
|
53
|
+
:target: https://risclog.logging.readthedocs.io/en/latest/?version=latest
|
|
54
|
+
:alt: Documentation Status
|
|
55
|
+
|
|
56
|
+
The risclog.logging package provides a comprehensive solution for structured logging in Python applications. Risclog.logging uses structlog and logging to generate detailed and formatted log entries. The package supports both synchronous and asynchronous log messages and provides options for automatic e-mail notification of exception errors.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
* Free software: MIT license
|
|
60
|
+
* Documentation: https://risclog.logging.readthedocs.io.
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
Features
|
|
64
|
+
========
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
Creating a logger
|
|
68
|
+
-----------------
|
|
69
|
+
|
|
70
|
+
To create a logger, use the get_logger function. This function ensures that you get an instance of RiscLogger that is properly configured.
|
|
71
|
+
|
|
72
|
+
.. code-block:: python
|
|
73
|
+
|
|
74
|
+
from risclog.logging import get_logger
|
|
75
|
+
|
|
76
|
+
# create logger
|
|
77
|
+
logger = get_logger(name='my_logger')
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
Configuration of the logger
|
|
81
|
+
---------------------------
|
|
82
|
+
|
|
83
|
+
The logger configuration takes place automatically when the logger instance is created using get_logger. The _configure_logger method sets up structlog and logging to provide logs with timestamps, context variables and formatting. You can customize the configuration as required.
|
|
84
|
+
|
|
85
|
+
The module is configured to automatically read the logging level from an environment variable. By default, the level is set to `INFO`. To adjust this, set the `LOG_LEVEL` environment variable:
|
|
86
|
+
|
|
87
|
+
.. code-block:: bash
|
|
88
|
+
|
|
89
|
+
export LOG_LEVEL=DEBUG
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
Use the following methods to log messages with different log levels:
|
|
93
|
+
|
|
94
|
+
* Debug-message: logger.debug("This is a debug message")
|
|
95
|
+
* Info-message: logger.info("This is an info message")
|
|
96
|
+
* Warning-message: logger.warning("This is a warning message")
|
|
97
|
+
* Error-message: logger.error("This is an error message")
|
|
98
|
+
* Critical-message: logger.critical("This is a critical message")
|
|
99
|
+
* Fatal-message: logger.fatal("This is a fatal message")
|
|
100
|
+
* Exception-message: logger.exception("This is an exception message")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
Asynchronous and synchronous log messages
|
|
104
|
+
-----------------------------------------
|
|
105
|
+
|
|
106
|
+
The risclog.logging package supports both synchronous and asynchronous log messages. If you are working in an asynchronous environment, use the asynchronous versions of the log methods:
|
|
107
|
+
|
|
108
|
+
* Asynchronous debug message: await logger.debug("Async debug message")
|
|
109
|
+
* Asynchronous info message: await logger.info("Async info message")
|
|
110
|
+
* And so on...
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
Decorator for logging
|
|
114
|
+
---------------------
|
|
115
|
+
|
|
116
|
+
The decorator decorator can be used to provide methods with automatic logging and optional e-mail notification of exceptions
|
|
117
|
+
|
|
118
|
+
.. code-block:: python
|
|
119
|
+
|
|
120
|
+
from risclog.logging import get_logger
|
|
121
|
+
|
|
122
|
+
logger = get_logger(name='my_logger')
|
|
123
|
+
|
|
124
|
+
@logger.decorator(send_email=True)
|
|
125
|
+
async def some_async_function(x, y):
|
|
126
|
+
return x + y
|
|
127
|
+
|
|
128
|
+
.. code-block:: python
|
|
129
|
+
|
|
130
|
+
from risclog.logging import get_logger
|
|
131
|
+
|
|
132
|
+
logger = get_logger(name='my_logger')
|
|
133
|
+
|
|
134
|
+
@logger.decorator
|
|
135
|
+
def some_sync_function(x, y):
|
|
136
|
+
return x + y
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
Error handling and e-mail notification
|
|
140
|
+
--------------------------------------
|
|
141
|
+
|
|
142
|
+
If you set the send_email parameter to True, an email notification is automatically sent in the event of an exception. The email is sent asynchronously via a ThreadPoolExecutor and contains the exception details.
|
|
143
|
+
|
|
144
|
+
**To be able to send e-mails, the following environment variables must be set!**
|
|
145
|
+
|
|
146
|
+
* 'logging_email_smtp_user'
|
|
147
|
+
* 'logging_email_smtp_password'
|
|
148
|
+
* 'logging_email_to'
|
|
149
|
+
* 'logging_email_smtp_server'
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
Example
|
|
153
|
+
-------
|
|
154
|
+
|
|
155
|
+
Here is a complete example showing how to use the risclog.logginng package in an application
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
.. code-block:: python
|
|
159
|
+
|
|
160
|
+
import os
|
|
161
|
+
import asyncio
|
|
162
|
+
from risclog.logging import get_logger
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
os.environ["LOG_LEVEL"] = "DEBUG"
|
|
166
|
+
|
|
167
|
+
logger = get_logger("async_debug_example")
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@logger.decorator(send_email=True)
|
|
171
|
+
async def fetch_data(url: str):
|
|
172
|
+
await logger.debug(f"Start retrieving data from {url}")
|
|
173
|
+
await asyncio.sleep(2) # Simulates a delay, such as a network request
|
|
174
|
+
await logger.debug(f"Successfully retrieved data from {url}")
|
|
175
|
+
return {"data": f"Sample data from {url}"}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@logger.decorator
|
|
179
|
+
async def main():
|
|
180
|
+
url = "https://example.com"
|
|
181
|
+
await logger.debug(f"Start main function with URL: {url}")
|
|
182
|
+
data = await fetch_data(url)
|
|
183
|
+
await logger.debug(f"Data received: {data}")
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
if __name__ == "__main__":
|
|
187
|
+
logger.info("Start main function")
|
|
188
|
+
asyncio.run(main())
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
output:
|
|
192
|
+
|
|
193
|
+
.. code-block:: bash
|
|
194
|
+
|
|
195
|
+
2024-08-05 11:38:51 [info ] [async_debug_example] __id=4378622064 __sender=inline message=Start main function
|
|
196
|
+
2024-08-05 11:38:51 [info ] [async_debug_example] __id=4384943584 __sender=async_logging_decorator _function=main _script=example.py message=Method "main" called with no arguments.
|
|
197
|
+
2024-08-05 11:38:51 [debug ] [async_debug_example] __id=4378552584 __sender=inline _function=main _script=example.py message=Start main function with URL: https://example.com
|
|
198
|
+
2024-08-05 11:38:51 [info ] [async_debug_example] __id=4384943744 __sender=async_logging_decorator _function=fetch_data _script=example.py message=Method called: "fetch_data" with: "{'arg_0': 'https://example.com'}"
|
|
199
|
+
2024-08-05 11:38:51 [debug ] [async_debug_example] __id=4366292144 __sender=inline _function=fetch_data _script=example.py message=Start retrieving data from https://example.com
|
|
200
|
+
2024-08-05 11:38:53 [debug ] [async_debug_example] __id=4366292144 __sender=inline _function=fetch_data _script=example.py message=Successfully retrieved data from https://example.com
|
|
201
|
+
2024-08-05 11:38:53 [info ] [async_debug_example] __id=4384943744 __sender=async_logging_decorator _function=fetch_data _script=example.py message=Method "fetch_data" returned: "{'data': 'Sample data from https://example.com'}"
|
|
202
|
+
2024-08-05 11:38:53 [debug ] [async_debug_example] __id=4378552584 __sender=inline message=Data received: {'data': 'Sample data from https://example.com'}
|
|
203
|
+
2024-08-05 11:38:53 [info ] [async_debug_example] __id=4384943584 __sender=async_logging_decorator message=Method "main" returned: "None"
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
Run tests::
|
|
208
|
+
|
|
209
|
+
$ ./pytest
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
Credits
|
|
213
|
+
=======
|
|
214
|
+
|
|
215
|
+
This package was created with Cookiecutter_ and the `risclog-solution/risclog-cookiecutter-pypackage`_ project template.
|
|
216
|
+
|
|
217
|
+
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
|
|
218
|
+
.. _`risclog-solution/risclog-cookiecutter-pypackage`: https://github.com/risclog-solution/risclog-cookiecutter-pypackage
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
This package uses AppEnv_ for running tests inside this package.
|
|
222
|
+
|
|
223
|
+
.. _AppEnv: https://github.com/flyingcircusio/appenv
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
==============================
|
|
227
|
+
Change log for risclog.logging
|
|
228
|
+
==============================
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
1.0 (2024-08-06)
|
|
232
|
+
================
|
|
233
|
+
|
|
234
|
+
* initial release
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
===============
|
|
2
|
+
risclog.logging
|
|
3
|
+
===============
|
|
4
|
+
|
|
5
|
+
.. image:: https://github.com/risclog-solution/risclog.logging/workflows/Test/badge.svg?branch=master
|
|
6
|
+
:target: https://github.com/risclog-solution/risclog.logging/actions?workflow=Test
|
|
7
|
+
:alt: CI Status
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
.. image:: https://img.shields.io/pypi/v/risclog.logging.svg
|
|
11
|
+
:target: https://pypi.python.org/pypi/risclog.logging
|
|
12
|
+
|
|
13
|
+
.. image:: https://img.shields.io/travis/risclog-solution/risclog.logging.svg
|
|
14
|
+
:target: https://travis-ci.com/risclog-solution/risclog.logging
|
|
15
|
+
|
|
16
|
+
.. image:: https://readthedocs.org/projects/risclog.logging/badge/?version=latest
|
|
17
|
+
:target: https://risclog.logging.readthedocs.io/en/latest/?version=latest
|
|
18
|
+
:alt: Documentation Status
|
|
19
|
+
|
|
20
|
+
The risclog.logging package provides a comprehensive solution for structured logging in Python applications. Risclog.logging uses structlog and logging to generate detailed and formatted log entries. The package supports both synchronous and asynchronous log messages and provides options for automatic e-mail notification of exception errors.
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
* Free software: MIT license
|
|
24
|
+
* Documentation: https://risclog.logging.readthedocs.io.
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
Features
|
|
28
|
+
========
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Creating a logger
|
|
32
|
+
-----------------
|
|
33
|
+
|
|
34
|
+
To create a logger, use the get_logger function. This function ensures that you get an instance of RiscLogger that is properly configured.
|
|
35
|
+
|
|
36
|
+
.. code-block:: python
|
|
37
|
+
|
|
38
|
+
from risclog.logging import get_logger
|
|
39
|
+
|
|
40
|
+
# create logger
|
|
41
|
+
logger = get_logger(name='my_logger')
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
Configuration of the logger
|
|
45
|
+
---------------------------
|
|
46
|
+
|
|
47
|
+
The logger configuration takes place automatically when the logger instance is created using get_logger. The _configure_logger method sets up structlog and logging to provide logs with timestamps, context variables and formatting. You can customize the configuration as required.
|
|
48
|
+
|
|
49
|
+
The module is configured to automatically read the logging level from an environment variable. By default, the level is set to `INFO`. To adjust this, set the `LOG_LEVEL` environment variable:
|
|
50
|
+
|
|
51
|
+
.. code-block:: bash
|
|
52
|
+
|
|
53
|
+
export LOG_LEVEL=DEBUG
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
Use the following methods to log messages with different log levels:
|
|
57
|
+
|
|
58
|
+
* Debug-message: logger.debug("This is a debug message")
|
|
59
|
+
* Info-message: logger.info("This is an info message")
|
|
60
|
+
* Warning-message: logger.warning("This is a warning message")
|
|
61
|
+
* Error-message: logger.error("This is an error message")
|
|
62
|
+
* Critical-message: logger.critical("This is a critical message")
|
|
63
|
+
* Fatal-message: logger.fatal("This is a fatal message")
|
|
64
|
+
* Exception-message: logger.exception("This is an exception message")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
Asynchronous and synchronous log messages
|
|
68
|
+
-----------------------------------------
|
|
69
|
+
|
|
70
|
+
The risclog.logging package supports both synchronous and asynchronous log messages. If you are working in an asynchronous environment, use the asynchronous versions of the log methods:
|
|
71
|
+
|
|
72
|
+
* Asynchronous debug message: await logger.debug("Async debug message")
|
|
73
|
+
* Asynchronous info message: await logger.info("Async info message")
|
|
74
|
+
* And so on...
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
Decorator for logging
|
|
78
|
+
---------------------
|
|
79
|
+
|
|
80
|
+
The decorator decorator can be used to provide methods with automatic logging and optional e-mail notification of exceptions
|
|
81
|
+
|
|
82
|
+
.. code-block:: python
|
|
83
|
+
|
|
84
|
+
from risclog.logging import get_logger
|
|
85
|
+
|
|
86
|
+
logger = get_logger(name='my_logger')
|
|
87
|
+
|
|
88
|
+
@logger.decorator(send_email=True)
|
|
89
|
+
async def some_async_function(x, y):
|
|
90
|
+
return x + y
|
|
91
|
+
|
|
92
|
+
.. code-block:: python
|
|
93
|
+
|
|
94
|
+
from risclog.logging import get_logger
|
|
95
|
+
|
|
96
|
+
logger = get_logger(name='my_logger')
|
|
97
|
+
|
|
98
|
+
@logger.decorator
|
|
99
|
+
def some_sync_function(x, y):
|
|
100
|
+
return x + y
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
Error handling and e-mail notification
|
|
104
|
+
--------------------------------------
|
|
105
|
+
|
|
106
|
+
If you set the send_email parameter to True, an email notification is automatically sent in the event of an exception. The email is sent asynchronously via a ThreadPoolExecutor and contains the exception details.
|
|
107
|
+
|
|
108
|
+
**To be able to send e-mails, the following environment variables must be set!**
|
|
109
|
+
|
|
110
|
+
* 'logging_email_smtp_user'
|
|
111
|
+
* 'logging_email_smtp_password'
|
|
112
|
+
* 'logging_email_to'
|
|
113
|
+
* 'logging_email_smtp_server'
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
Example
|
|
117
|
+
-------
|
|
118
|
+
|
|
119
|
+
Here is a complete example showing how to use the risclog.logginng package in an application
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
.. code-block:: python
|
|
123
|
+
|
|
124
|
+
import os
|
|
125
|
+
import asyncio
|
|
126
|
+
from risclog.logging import get_logger
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
os.environ["LOG_LEVEL"] = "DEBUG"
|
|
130
|
+
|
|
131
|
+
logger = get_logger("async_debug_example")
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@logger.decorator(send_email=True)
|
|
135
|
+
async def fetch_data(url: str):
|
|
136
|
+
await logger.debug(f"Start retrieving data from {url}")
|
|
137
|
+
await asyncio.sleep(2) # Simulates a delay, such as a network request
|
|
138
|
+
await logger.debug(f"Successfully retrieved data from {url}")
|
|
139
|
+
return {"data": f"Sample data from {url}"}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@logger.decorator
|
|
143
|
+
async def main():
|
|
144
|
+
url = "https://example.com"
|
|
145
|
+
await logger.debug(f"Start main function with URL: {url}")
|
|
146
|
+
data = await fetch_data(url)
|
|
147
|
+
await logger.debug(f"Data received: {data}")
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
if __name__ == "__main__":
|
|
151
|
+
logger.info("Start main function")
|
|
152
|
+
asyncio.run(main())
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
output:
|
|
156
|
+
|
|
157
|
+
.. code-block:: bash
|
|
158
|
+
|
|
159
|
+
2024-08-05 11:38:51 [info ] [async_debug_example] __id=4378622064 __sender=inline message=Start main function
|
|
160
|
+
2024-08-05 11:38:51 [info ] [async_debug_example] __id=4384943584 __sender=async_logging_decorator _function=main _script=example.py message=Method "main" called with no arguments.
|
|
161
|
+
2024-08-05 11:38:51 [debug ] [async_debug_example] __id=4378552584 __sender=inline _function=main _script=example.py message=Start main function with URL: https://example.com
|
|
162
|
+
2024-08-05 11:38:51 [info ] [async_debug_example] __id=4384943744 __sender=async_logging_decorator _function=fetch_data _script=example.py message=Method called: "fetch_data" with: "{'arg_0': 'https://example.com'}"
|
|
163
|
+
2024-08-05 11:38:51 [debug ] [async_debug_example] __id=4366292144 __sender=inline _function=fetch_data _script=example.py message=Start retrieving data from https://example.com
|
|
164
|
+
2024-08-05 11:38:53 [debug ] [async_debug_example] __id=4366292144 __sender=inline _function=fetch_data _script=example.py message=Successfully retrieved data from https://example.com
|
|
165
|
+
2024-08-05 11:38:53 [info ] [async_debug_example] __id=4384943744 __sender=async_logging_decorator _function=fetch_data _script=example.py message=Method "fetch_data" returned: "{'data': 'Sample data from https://example.com'}"
|
|
166
|
+
2024-08-05 11:38:53 [debug ] [async_debug_example] __id=4378552584 __sender=inline message=Data received: {'data': 'Sample data from https://example.com'}
|
|
167
|
+
2024-08-05 11:38:53 [info ] [async_debug_example] __id=4384943584 __sender=async_logging_decorator message=Method "main" returned: "None"
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
Run tests::
|
|
172
|
+
|
|
173
|
+
$ ./pytest
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
Credits
|
|
177
|
+
=======
|
|
178
|
+
|
|
179
|
+
This package was created with Cookiecutter_ and the `risclog-solution/risclog-cookiecutter-pypackage`_ project template.
|
|
180
|
+
|
|
181
|
+
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
|
|
182
|
+
.. _`risclog-solution/risclog-cookiecutter-pypackage`: https://github.com/risclog-solution/risclog-cookiecutter-pypackage
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
This package uses AppEnv_ for running tests inside this package.
|
|
186
|
+
|
|
187
|
+
.. _AppEnv: https://github.com/flyingcircusio/appenv
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Minimal makefile for Sphinx documentation
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
# You can set these variables from the command line.
|
|
5
|
+
SPHINXOPTS =
|
|
6
|
+
SPHINXBUILD = python -msphinx
|
|
7
|
+
SPHINXPROJ = risclog.logging
|
|
8
|
+
SOURCEDIR = .
|
|
9
|
+
BUILDDIR = _build
|
|
10
|
+
|
|
11
|
+
# Put it first so that "make" without argument is like "make help".
|
|
12
|
+
help:
|
|
13
|
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
14
|
+
|
|
15
|
+
.PHONY: help Makefile
|
|
16
|
+
|
|
17
|
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
|
18
|
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
|
19
|
+
%: Makefile
|
|
20
|
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.. include:: ../AUTHORS.rst
|