# 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
```bash
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`:
```python
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.
```bash
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:
```bash
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:
```python
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.")
```
Raw data
{
"_id": null,
"home_page": "https://github.com/cristhianCEM/django-stream-logging",
"name": "django-stream-logging",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "Django, logging, stream, views, commands",
"author": "cristianCEM",
"author_email": "cristianbcer27@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/1f/88/fa9dd19626c4bbd0cb50e46b69aa809983c4a33b5ac35cd2b9501cbe1132/django_stream_logging-0.2.1.tar.gz",
"platform": null,
"description": "# Django Stream Logging\r\n\r\nA 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.\r\n\r\n\r\n## Installation\r\n\r\n```bash\r\npip install django-stream-logging\r\n```\r\n\r\n## Usage for commands\r\n\r\nTo 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.\r\n\r\nFor example, create a file: `myapp/management/commands/test-command.py`:\r\n\r\n```python\r\nfrom django_stream_logging import BaseLoggingCommand\r\n\r\nclass Command(BaseLoggingCommand):\r\n def handle(self, *args, **options):\r\n # Logging examples using different log levels\r\n self.logger.info('This is an info message')\r\n self.write_info('This is an info message (alias)')\r\n self.logger.error('This is an error message')\r\n self.write_error('This is an error message (alias)')\r\n self.logger.warning('This is a warning message')\r\n self.write_warning('This is a warning message (alias)')\r\n self.logger.debug('This is a debug message')\r\n self.write_debug('This is a debug message (alias)')\r\n self.logger.critical('This is a critical message')\r\n self.write_critical('This is a critical message (alias)')\r\n```\r\n\r\nNow you can run the command, and the logs will be displayed in the console in real-time.\r\n\r\n```bash\r\npython manage.py test-command\r\n```\r\n\r\nAdditionally, you can use the `--log-level` argument to set the desired log level for the command, allowing you to control which messages are shown:\r\n\r\n```bash\r\npython manage.py test-command --log-level=WARNING\r\n```\r\n\r\n## Usage for views\r\n\r\nTo 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.\r\n\r\nExample:\r\n\r\n```python\r\nfrom time import sleep\r\n\r\nclass ExampleEventStreamView(EventStreamView):\r\n \"\"\"\r\n Example view that inherits from EventStreamView and generates log messages\r\n of different levels to demonstrate its functionality.\r\n \"\"\"\r\n def event_stream(self):\r\n # Generating log messages with different severity levels\r\n self.logger.debug(\"Este es un mensaje de DEBUG.\")\r\n sleep(1)\r\n self.logger.info(\"Este es un mensaje de INFO.\")\r\n sleep(1)\r\n self.logger.warning(\"Este es un mensaje de WARNING.\")\r\n sleep(1)\r\n self.logger.error(\"Este es un mensaje de ERROR.\")\r\n sleep(1)\r\n self.logger.critical(\"Este es un mensaje de CRITICAL.\")\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django stream logging classes for views and commands.",
"version": "0.2.1",
"project_urls": {
"Homepage": "https://github.com/cristhianCEM/django-stream-logging"
},
"split_keywords": [
"django",
" logging",
" stream",
" views",
" commands"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6f83a5916469eb3e46a058ac4a650d34a475fb7593a5d53570d07da9acaed96f",
"md5": "1cda05e976c4c98087e90609e3585de0",
"sha256": "584497771ab794ab063bae2ab4225f89391ac49f602a5058c379c0560a563b30"
},
"downloads": -1,
"filename": "django_stream_logging-0.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1cda05e976c4c98087e90609e3585de0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 8233,
"upload_time": "2024-11-11T04:52:06",
"upload_time_iso_8601": "2024-11-11T04:52:06.320666Z",
"url": "https://files.pythonhosted.org/packages/6f/83/a5916469eb3e46a058ac4a650d34a475fb7593a5d53570d07da9acaed96f/django_stream_logging-0.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1f88fa9dd19626c4bbd0cb50e46b69aa809983c4a33b5ac35cd2b9501cbe1132",
"md5": "8c72f2f71b137b1c58bf9334a4ea9168",
"sha256": "37b19cc14c46ff453c09f8c4b44129c4b886df72305e1db473cf5bfe0acf7095"
},
"downloads": -1,
"filename": "django_stream_logging-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "8c72f2f71b137b1c58bf9334a4ea9168",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 7119,
"upload_time": "2024-11-11T04:52:07",
"upload_time_iso_8601": "2024-11-11T04:52:07.865217Z",
"url": "https://files.pythonhosted.org/packages/1f/88/fa9dd19626c4bbd0cb50e46b69aa809983c4a33b5ac35cd2b9501cbe1132/django_stream_logging-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-11 04:52:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cristhianCEM",
"github_project": "django-stream-logging",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "django-stream-logging"
}