Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
A simple Django app to log requests/responses in various formats, such as JSON.
.. role:: python(code) :language: python
A Django library that logs request, response and exception details in a JSON document. It uses the python rotation mechanism to rotate the file logs, but the rotation files will be gziped and it has support for sending the log messages to Elasticsearch.
.. image:: https://badge.fury.io/py/django-logging-json.svg :target: https://badge.fury.io/py/django-logging-json
.. code-block:: python
pip install git+https://github.com/cipriantarta/django-logging
or
.. code-block:: python
pip install django-logging-json
.. code-block:: python
INSTALLED_APPS = (
...
'django_logging',
)
2. Include the DjangoLoggingMiddleware middleware in your MIDDLEWARE_CLASSES like this:
.. code-block:: python
MIDDLEWARE_CLASSES = (
'django_logging.middleware.DjangoLoggingMiddleware',
...
)
This handle will log request/response info to LOG_PATH/app.log
. It will also log request/exception, for unhandled exceptions, in the same file.
Log format:
Request and Response
.. code-block:: javascript
{
"INFO":
{
"timestamp":
{
"request": {
... request info ...
},
"response": {
... response info ...
}
}
}
}
Request and Exception
.. code-block:: javascript
{
"ERROR":
{
"timestamp":
{
"request": {
... request info ...
},
"exception": {
"message": "Exception message",
"traceback": [
...
]
}
}
}
}
This handler will log all queries to LOG_PATH/sql.log
.
In a production environment you should set LOG_LEVEL = Error
or SQL_LOG = False
to avoid performance issues.
The queries will also be logged to the console if CONSOLE_LOG
is set to True
This handler will log debug messages to LOG_PATH/debug.log
. This handler is only used when settings.DEBUG
is set to True
.
Log format:
.. code-block:: python
[%(levelname)s - %(created)s], file:%(module)s.py, func:%(funcName)s, ln:%(lineno)s: %(message)s
To log debug messages:
.. code-block:: python
from django_logging import log
log.debug('debug message')
To log handled exceptions:
.. code-block:: python
from django_logging import log, ErrorLogObject
log.error(ErrorLogObject(request, exception, duration))
Inspired by Django Rest Framework, Django Logging settings are grouped in a single dictionary.
To override Django Logging settings, add a dictionary in your project's settings file
.. code-block:: python
DJANGO_LOGGING = {
"CONSOLE_LOG": False
}
:python:CONSOLE_LOG = True
- Log to console.
:python:SQL_LOG = True
- Log SQL queries.
:python:SQL_THRESHOLD = 0.5
- Log slow queries only.
:python:LOG_LEVEL = 'debug'
- If settings.DEBUG is set to True, otherwise LOG_LEVEL is set to 'info'
:python:DISABLE_EXISTING_LOGGERS = True
- Set this to False if you want to combine with multiple loggers.
:python:LOG_PATH = '{}/logs'.format(settings.BASE_DIR)
- If the logs folder does not exist, it will be created.
:python:IGNORED_PATHS = ['/admin', '/static', '/favicon.ico']
- List of URL endpoints to ignore.
:python:RESPONSE_FIELDS = ('status', 'reason', 'charset', 'headers', 'content')
- List of response fields to log.
:python:CONTENT_JSON_ONLY = True
- Log response content only if its a JSON document.
:python:ROTATE_MB = 100
- Maximum size in MB that the log file can have before it gets rotated.
:python:ROTATE_COUNT = 10
- Maximum number of rotated log files.
:python:INDENT_CONSOLE_LOG = 2
- Indent console log by "n" spaces.
:python:ELASTICSEARCH_ENABLED = False
- Set to yes to enable elasticsearch support.
:python:ELASTICSEARCH_HOSTS = ["localhost"]
- Elasticsearch hosts
:python:ELASTICSEARCH_INDEX = "django-logging-json"
- Elasticsearch index name
:python:ELASTICSEARCH_SSL = False
- Elasticsearch connection via SSL (:443)
:python:ELASTICSEARCH_AUTH = ('user', 'password)
- Elasticsearch authorization credentials (user, password). Defaults to None
django >= 1.10
.. image:: kibana_sample.png
FAQs
A simple Django app to log requests/responses in various formats, such as JSON.
We found that django-logging-json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.