Django stream logging classes for views and commands.
Project description
Django Stream Logging
A Django logging handler that allows streaming log messages to the console, browser, or other output destinations in real-time, making it easier to monitor what is happening within your Django application.
Installation
pip install django-stream-logging
Usage for commands
To stream log messages from custom management commands to the console, use the BaseLoggingCommand
class as a base for your command. This will enable easy and configurable logging for various log levels.
For example, create a file: myapp/management/commands/test-command.py
:
from django_stream_logging import BaseLoggingCommand
class Command(BaseLoggingCommand):
def handle(self, *args, **options):
# Logging examples using different log levels
self.logger.info('This is an info message')
self.write_info('This is an info message (alias)')
self.logger.error('This is an error message')
self.write_error('This is an error message (alias)')
self.logger.warning('This is a warning message')
self.write_warning('This is a warning message (alias)')
self.logger.debug('This is a debug message')
self.write_debug('This is a debug message (alias)')
self.logger.critical('This is a critical message')
self.write_critical('This is a critical message (alias)')
Now you can run the command, and the logs will be displayed in the console in real-time.
python manage.py test-command
Additionally, you can use the --log-level
argument to set the desired log level for the command, allowing you to control which messages are shown:
python manage.py test-command --log-level=WARNING
Usage for views
To stream log messages directly to the browser from a view, inherit from the EventStreamView
class. This is useful for scenarios where real-time feedback is needed in the browser, such as monitoring tasks or processes.
Example:
from time import sleep
class ExampleEventStreamView(EventStreamView):
"""
Example view that inherits from EventStreamView and generates log messages
of different levels to demonstrate its functionality.
"""
def event_stream(self):
# Generating log messages with different severity levels
self.logger.debug("Este es un mensaje de DEBUG.")
sleep(1)
self.logger.info("Este es un mensaje de INFO.")
sleep(1)
self.logger.warning("Este es un mensaje de WARNING.")
sleep(1)
self.logger.error("Este es un mensaje de ERROR.")
sleep(1)
self.logger.critical("Este es un mensaje de CRITICAL.")
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 django_stream_logging-0.2.0.tar.gz
.
File metadata
- Download URL: django_stream_logging-0.2.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1993dbc4031d3eda93b68c8433a586e538899c8b49fb2cb51320b95eefa02d8d |
|
MD5 | cc1e13060975b1264c9585a203013011 |
|
BLAKE2b-256 | 6719c44f685aeec3543710ebafe9c730f9846925123addfff6eb61c275082d03 |
File details
Details for the file django_stream_logging-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: django_stream_logging-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64b7de7505b79a9c84dd5011860e6d975a7cb50ebeb7dba21ffca229248b159c |
|
MD5 | f9222ea42881e550e916bde077e3bd99 |
|
BLAKE2b-256 | 810d9b9d548a0843f8389f30911e00179e1f465026cb99cfe7d0784d946306fd |