Ideal logging setup with utilities to dump the stack and local variables
Project description
Yogger
Yogger aims to provide an ideal logging setup with utilities to dump the stack and local variables.
Supports
requests.Request
andrequests.Response
objects if the Requests package is installed.
Example of logger output:
[ 2022-11-17 10:16:09.0918 INFO my_package.base ] Something we want to log.
[ 2022-11-17 10:16:09.0918 DEBUG my_package.base ] Something we want to log.
[ 2022-11-17 10:16:09.0918 WARNING my_package.base ] Something we want to log.
[ 2022-11-17 10:16:09.0918 ERROR my_package.base ] Something we want to log.
[ 2022-11-17 10:16:09.0918 CRITICAL my_package.base ] Something we want to log.
Example of object representation in dump:
example = {
"id": 123456790,
"profile": {
"name": "John Doe",
"birthdate": datetime.date(2000, 1, 1),
"weight_kg": 86.18,
},
"video_ids": [123, 456, 789],
}
example = <builtins.dict>
example['id'] = 123456790
example['profile'] = <builtins.dict>
example['profile']['name'] = 'John Doe'
example['profile']['birthdate'] = datetime.date(2000, 1, 1)
example['profile']['weight_kg'] = 86.18
example['video_ids'] = [123, 456, 789]
Requirements:
Yogger requires Python 3.9 or higher, is platform independent, and requires no outside dependencies.
Installing
Most stable version from PyPi:
pip install yogger
Development version from GitHub:
git clone git+https://github.com/yaphott/yogger.git
cd yogger
pip install .
Usage
Import packages and instantiate a logger:
import logging
import yogger
logger = logging.getLogger(__name__)
Install the logger class and configure with your package name:
Place at the start of the top-level function.
def _cli():
yogger.install()
yogger.configure(__name__)
# or
# yogger.configure("my_package")
Support for dumping stack and locals
Use the dump_on_exception
context manager:
with yogger.dump_on_exception():
raise SomeException
Example of output:
[ 2022-11-17 10:16:09.0918 CRITICAL yogger.base ]
Dumped stack and locals to '/tmp/my_package_stack_and_locals_hp0ngc90'
Copy and paste the following to view:
cat '/tmp/my_package_stack_and_locals_hp0ngc90'
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
SomeException
To quickly view the contents run the bash command from the dump message:
Example here is from the log message above:
cat '/tmp/my_package_stack_and_locals_hp0ngc90'
To handle without using the context manager, something like this would also suffice:
import inspect
try:
...
except Exception as e:
trace = inspect.trace()
if len(trace) > 1:
logfile_path = yogger.dump_stack_and_locals(trace[1:], e=e)
Library
yogger.install
Function to install the logger class and instantiate the global logger.
Function Signature |
---|
install() |
Parameters |
---|
Empty |
yogger.configure
Function to prepare for logging.
Function Signature |
---|
configure(package_name, *, verbosity=0, dump_locals=False, persist_log=False) |
Parameters | |
---|---|
package_name(str) | Name of the package to dump from trace stack. |
verbosity(int) | Level of verbosity (0-2). |
dump_locals(bool) | Dump locals to the logfile (in addition to stack) when log_level>=logging.WARNING. |
persist_log(bool) | Create the logfile in the current working directory instead of "/tmp". |
yogger.dump_on_exception
Context manager that dumps the stack and locals to a file if an exception is raised.
Function Signature |
---|
dump_on_exception() |
Parameters |
---|
Empty |
yogger.dump_stack_and_locals
Dump the stack and locals to a file.
Function Signature |
---|
dump_stack_and_locals(trace, *, e=None, logfile_path=None) |
Parameters | |
---|---|
trace(list[inspect.FrameInfo]) | Trace to dump |
e(Exception) | Exception that was raised. |
logfile_path(str) | Custom path to use for logfile. |
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 yogger-0.0.2.tar.gz
.
File metadata
- Download URL: yogger-0.0.2.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66391c5b7704ab3080b81c49b8683416c289f3a64f79f83796e0813c19978cbb |
|
MD5 | fc58aa9878429438eee9aeed3324a89c |
|
BLAKE2b-256 | 20b105955026a37b6ffa5be0aa9e024aea4c11ac2d8c96817d6d534f2c349704 |
File details
Details for the file yogger-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: yogger-0.0.2-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4bfb359d7b73da09d1fa1ebbff5437d54dcd37b6fc7fd722959d744339db790 |
|
MD5 | 8c43e33ae702bef4e302bd2dfecb38b8 |
|
BLAKE2b-256 | c0d8029639495bbf05ff4878ed0288987cba1a5aacede323a4c2c7b98ece4d8a |