It helps to get the context of a frame from the caller's stack. Can be used to improve service and microservice logs.
Project description
Stack Frame Analyzer
This package was created to help improve the quality of application logs.
It's only uses building libraries, therefore, it has no external dependency. It is also lightweight and thread-safe, which makes it ideal for use in services and micro-services.
However, the module has some limitations. The most important thing to note in this scenario is that it only works with the CPython implementation.
The returned context is formatted according to the following pattern: project_name:package_name:module_name:class_name:callable_name(callable_arguments)
An example of the context returned could be: authentication_service:src.domain.user:model:UserModel:has_permission(self=..., permission="add_user")
Typical usage example:
With Caller's Stack Frame Depth 0
stack_frame_analyzer = StackFrameAnalyzer("my_service_name")
def foo(bar):
try:
...
except Exception as error:
context = stack_frame_analyzer.get_caller_context()
logging.error(context)
With Caller's Stack Frame Depth 1
stack_frame_analyzer = StackFrameAnalyzer("my_service_name")
class MyException(Exception):
def __init__(self):
self.context = stack_frame_analyzer.get_caller_context(depth_in_the_stack=1)
super().__init__()
def foo(bar):
try:
...
except MyException as error:
logging.error(error.context)
With Caller's Stack Frame Depth 2
class ExceptionWithContext(Exception):
"""
Base class to make exceptions capture the context of whoever raises them.
"""
def __init__(self, message: str):
self.message = message
self.context = stack_frame_analyzer.get_caller_context(depth_in_the_stack=2)
super().__init__(self.message)
class FooException(ExceptionWithContext):
def __init__(self, message: str = "message"):
self.message = message
super().__init__(self.message)
def foo(bar):
try:
raise FooException
except FooException as error:
logging.error(error.context)
Test Coverage
Name Stmts Miss Cover
-----------------------------------------------------------------------------
src/__init__.py 1 0 100%
src/stack_frame_analyzer/__init__.py 2 0 100%
src/stack_frame_analyzer/exceptions.py 36 0 100%
src/stack_frame_analyzer/main.py 100 0 100%
tests/__init__.py 0 0 100%
tests/stack_frame_analyzer/__init__.py 0 0 100%
tests/stack_frame_analyzer/test_exceptions.py 27 0 100%
tests/stack_frame_analyzer/test_in_class.py 17 0 100%
tests/stack_frame_analyzer/test_main.py 131 0 100%
tests/stack_frame_analyzer/test_memory_leak.py 55 0 100%
tests/stack_frame_analyzer/test_on_function.py 12 0 100%
tests/stack_frame_analyzer/test_speed.py 7 0 100%
tests/stack_frame_analyzer/test_with_decorator.py 8 0 100%
tests/stack_frame_analyzer/test_with_depth_two_frame.py 13 0 100%
tests/stack_frame_analyzer/test_with_exception.py 8 0 100%
tests/stack_frame_analyzer/test_with_threads.py 33 0 100%
tests/stack_frame_analyzer/utils/__init__.py 0 0 100%
tests/stack_frame_analyzer/utils/bar.py 4 0 100%
tests/stack_frame_analyzer/utils/baz.py 13 0 100%
tests/stack_frame_analyzer/utils/child.py 7 0 100%
tests/stack_frame_analyzer/utils/foo.py 4 0 100%
tests/stack_frame_analyzer/utils/foo_with_decorator.py 11 0 100%
tests/stack_frame_analyzer/utils/foo_with_exception.py 7 0 100%
tests/stack_frame_analyzer/utils/main.py 2 0 100%
tests/stack_frame_analyzer/utils/memory_leak.py 6 0 100%
tests/stack_frame_analyzer/utils/parent.py 8 0 100%
tests/stack_frame_analyzer/utils/solve_with_queue.py 7 0 100%
-----------------------------------------------------------------------------
TOTAL 519 0 100%
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Stack Frame Analyzer-0.2.1.tar.gz
.
File metadata
- Download URL: Stack Frame Analyzer-0.2.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1dafb839751c9df1725c692303100323a74ba95844bb3164644fbee2643fe7c |
|
MD5 | 358d69f2b2e9b7575400a3448528e895 |
|
BLAKE2b-256 | f6dfdcc9366451a5f1587ac7860b8fbf40975c8535004beadba96768f1f13c66 |
File details
Details for the file Stack_Frame_Analyzer-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: Stack_Frame_Analyzer-0.2.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8f36abf2285c38c45820227c7ff505b275ec19b2b48ab0ea97fafbc3a28eb2e |
|
MD5 | a6700588818d0d00204e1090667c6cb5 |
|
BLAKE2b-256 | be3c33fc85a4975aaff9d1920d05b773ab31e43da6ce2dab82ffa3f76510431a |