Skip to main content

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


Download files

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

Source Distribution

django_stream_logging-0.2.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

django_stream_logging-0.2.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file django_stream_logging-0.2.1.tar.gz.

File metadata

  • Download URL: django_stream_logging-0.2.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.5

File hashes

Hashes for django_stream_logging-0.2.1.tar.gz
Algorithm Hash digest
SHA256 37b19cc14c46ff453c09f8c4b44129c4b886df72305e1db473cf5bfe0acf7095
MD5 8c72f2f71b137b1c58bf9334a4ea9168
BLAKE2b-256 1f88fa9dd19626c4bbd0cb50e46b69aa809983c4a33b5ac35cd2b9501cbe1132

See more details on using hashes here.

File details

Details for the file django_stream_logging-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_stream_logging-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 584497771ab794ab063bae2ab4225f89391ac49f602a5058c379c0560a563b30
MD5 1cda05e976c4c98087e90609e3585de0
BLAKE2b-256 6f83a5916469eb3e46a058ac4a650d34a475fb7593a5d53570d07da9acaed96f

See more details on using hashes here.

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