Skip to main content

Mongo DRF Endpoint Logger [MDRFEL]

https://pypi.org/project/mongo-drf-endpoint-logger/ https://github.com/inanpy/mongo_drf_endpoint_logger

version Open Source Downloads Downloads Downloads

  • For your Django Rest Framework project, an API logger with MongoDb.

  • It won't slow down your API's response time, because the logger runs on a different thread.

  • Yes, we know that the log is not kept in the database, but there are times when we need to keep very critical data in the database.

  • We encrypt data such as password, token, access, refresh etc. (Please read the documentation if you want to add the keys to be encrypted.)

  • We do not log incoming urls for the admin panel.

What data does it record?

Url
Body 
Method
Ip
Response
Status Code
Execution Time
Created Date

Installation

Use the package manager pip to install mongo_drf_endpoint_logger.

pip install mongo_drf_endpoint_logger

Configuration

  1. Include EndpointLoggerMiddleware to MIDDLEWARE
MIDDLEWARE = [
    # ....................................
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'mongo_drf_endpoint_logger.middleware.endpoint_logger_middleware.EndpointLoggerMiddleware'
]
  1. Add MongoDb config to settings.py
import mongoengine
mongoengine.connect(
    db="logging", # MongoDb DB name
    host="localhost", # MongoDb Host
    username="***", # MongoDb Username
    password="***", # MongoDb Password
)

Config for MDRFEL

1. Open the MDRFEL system:

# Default: False, Description: Open The MDRFEL system:)
MONGO_DRF_ENDPOINT_LOGGER_LOG_TO_DB = True

2. Path type

# Default: "ABSLOLUTE", Description: Url path type information.
# ABSOLUTE_URI, FULL_PATH_URI
# RAW_URI just for Django<4.0 – https://docs.djangoproject.com/en/4.0/releases/4.0/
MONGO_DRF_ENDPOINT_LOGGER_PATH_TYPE = "ABSOLUTE_URI"

3. Skip URL to logging.

# Default: [], Description: By using the API's url name, you can avoid logging any API.
# Example: router.register(r'detail', DetailView, basename='ApiDetail')
# You could use basename-request_type
MONGO_DRF_ENDPOINT_LOGGER_SKIP_URL_NAME = ['ApiDetail-list', 'ApiDetail-create']

4. Skip namespace to logging.

# Default: []
# Description: By specifying the app's namespace as list
# you can avoid logging the entire app into the database.
MONGO_DRF_ENDPOINT_LOGGER_SKIP_NAMESPACE = ['name_space_1', 'name_space_2']

5. Specific methods to logging.

# Default: [] # Log all request.
# Description: By specifying request methods you can log only specific methods.
MONGO_DRF_ENDPOINT_LOGGER_METHODS = ['GET', 'POST', 'DELETE', 'PUT', 'PATCH'] 

6. Specific Status Code to logging.

# Default: [] # Log all request.
# Description: By specifying status codes you can log only specific status codes.
MONGO_DRF_ENDPOINT_LOGGER_STATUS_CODES = ['500', '400', '422']

7. Hide private information from logging.

# Default: ['password', 'token', 'access', 'refresh']
# Description: You could hide sensitive information from being exposed in the logs.
MONGO_DRF_ENDPOINT_LOGGER_STATUS_CODES = ['email', 'password']

Manual Log Insert

from mongo_drf_endpoint_logger.helpers import async_insert_log, sync_insert_log

"""
sync_insert_log : Create a log with SYNC.
async_insert_log: Create a log with THREAD, so it will be ASYNC.

    url: CharField: Required
    headers: DictField: Required
    body: DictField: Required
    method: CharField: Required
    response: DictField: Required
    status_code: IntegerFiled: Optional, Default: 200
    execution_time: string, Optional, Default: "0"
    ip: CharField: Optional, Default: "0.0.0.0"
    created_date: DateTimeField: Optional, Default: datetime.now()
"""

sync_insert_log(
    url="testUrl",
    headers={"hkey": "hvalue"},
    body={"bkey": "bvalue"},
    ip="127.0.0.1",
    response={"rkey": "rvalue"},
    status_code=200,
    execution_time="0.2",
    method="POST",
    created_date="2022-07-11 15:19:41.184786",
)

# OR

async_insert_log(
    url="testUrl",
    headers={"hkey": "hvalue"},
    body={"bkey": "bvalue"},
    ip="127.0.0.1",
    response={"rkey": "rvalue"},
    status_code=200,
    execution_time="0.2",
    method="POST",
    created_date="2022-07-11 15:19:41.184786",
)

TODO

  1. Django Admin Integration
  2. Interval Information for Readme
  3. Unit Tests

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Metadata

Release files for mongo-drf-endpoint-logger 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongo-drf-endpoint-logger 0.1.9
File Size Uploaded
mongo_drf_endpoint_logger-0.1.9.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mongo-drf-endpoint-logger 0.1.9
File Interpreter ABI Platform
mongo_drf_endpoint_logger-0.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / mongo_drf_endpoint_logger-0.1.9.tar.gz

Download URL mongo_drf_endpoint_logger-0.1.9.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f68173c35c47ddce51ab1fb8cc93ea737a9f444fbd53f8361f8d03e2961bc338
BLAKE2b-256 checksum
How to use checksums
860cdf97e351149151d49510212423494ad1f04aacdeaf3565a66a4f44369587
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.9

Release files / mongo_drf_endpoint_logger-0.1.9-py3-none-any.whl

Download URL mongo_drf_endpoint_logger-0.1.9-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77392caea77b6389319c911f044460d468851b55d044fc5f8498c3a921907efc
BLAKE2b-256 checksum
How to use checksums
7485565416526fdeee87bb1803636310145e1f45db01b5dcd1b03e0e6f8c083d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.9

Release history Release notifications | RSS feed

This release

0.1.9 This release

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page