A Django app for logging custom application errors
Project description
exceptions_and_logging
A Django app for logging custom application errors on the server side and also spits out JSON error info on the client side. You basically subclass our 'ApplicationErrors' class and the rest is done for you.
Dependencies
- Django Rest Framework
- PyYaml
Usage
- Without logging config.
# your_app.exceptions.py (or wherever your app exceptions are)
from exceptions_and_logging.exceptions import ApplicationErrors
class MyCustomException(ApplicationErrors):
default_message = "This item: '%(item_name)s' isn't supposed to be null"
def __init__(self, item_name):
message = self.default_message % {"item_name": item_name}
super().__init__(message)
When this error is raised:
-
Generated response on the client side (via Rest Framework):
-
{ "error":{ "type":"MyCustomException", "message":"This item: 'SomeItem' isn't supposed to be null", "code":"error" } }
-
An HTTP 500 Application Error response is returned.
-
-
Generated log on the server console (stdout):
-
02/15/2021 04:30:34 PM - APPLICATION ERROR - ERROR Error Type: MyCustomException Error Message: This item: 'SomeItem' isn't supposed to be null Traceback (most recent call last): # Bunch of traceback info ... ... raise NullError("SomeItem") your_app.exceptions.MyCustomException: This item: 'SomeItem' isn't supposed to be null
-
-
With Logging Config
# your_app.exceptions.py (or wherever your app exceptions are) from exceptions_and_logging.exceptions import ApplicationErrors # Path to your logging config file module_dir = os.path.dirname(__file__) config_file = os.path.join(module_dir, "myconfig.yaml") class MyCustomException(ApplicationErrors): default_message = "This item: '%(item_name)s' isn't supposed to be null" log_config = config_file # Can also be a dict config # If no logger name is passed, it will use the default logger name. def __init__(self, item_name, logger_name=None): message = self.default_message % {"item_name": item_name} super().__init__(message, logger_name=logger_name)
-
The
ApplicationError
class provides hooks for adding extra format strings to the logging message and also extra key, value pairs for the error dict.-
# Assuming your formatter has extra values like user_id and user_email msg_format = '%(asctime)s - APPLICATION ERROR - %(levelname)s\n' \ 'User Email: %(user_email)s\n' \ 'User ID: %(user_id)s\n'\ 'Error Message: %(message)s\n' class MyCustomException(ApplicationErrors): def __init__(self, user_id, user_email, **err_dict_kwargs): self.user_id, self.user_email = user_id, user_email super().__init__(**err_dict_kwargs) # Override this method to return extra kwargs for logging def extra_kwargs(self) -> dict: dict_ = { "user_id": self.user_id, "user_email": self.user_email } return dict_
# in views.py from rest_framework.views import APIView from .exceptions import MyCustomException class TestException(APIView): def get(self, request): raise MyCustomException( "id4", "name@gmail.com", foo="bar", hot="soup" )
-
Generated response on the client side via Rest Framework:
{ "error":{ "type":"MyCustomException", "message":"An application error occurred.", "code":"error", "foo":"bar", "hot":"soup" } }
-
-
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
Hashes for exceptions_and_logging-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35fb120a370741b0280c5f6a1aaaebe109d52af2615e54620fde5541434f0297 |
|
MD5 | 97dc997b019637116cae5fe7825521ff |
|
BLAKE2b-256 | c66d2dfb980a508f1908601b8303cbcf24a3a84fd16f4e5367addf886023976d |
Hashes for exceptions_and_logging-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16aaf460ba3427ebfba2462247764286f31069c0b1b122cae391461205dee030 |
|
MD5 | 0c46a2035900cab2ad7b1ba081027bfe |
|
BLAKE2b-256 | d92ff48212345a368ef6852f449762244db0efaeeb64f147c5bda5232c979c1f |