Skip to main content

An API Logger for your Django Rest Framework project with Mongo DB.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongo_drf_endpoint_logger-0.1.9.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

mongo_drf_endpoint_logger-0.1.9-py3-none-any.whl (10.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page