mirror of
https://github.com/django/daphne.git
synced 2025-07-01 10:23:35 +03:00
Added logging
This commit is contained in:
parent
9222676bf7
commit
57f21d71e2
16
channels/log.py
Normal file
16
channels/log.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logger(name, verbosity=1):
|
||||||
|
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s')
|
||||||
|
|
||||||
|
handler = logging.StreamHandler()
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
logger = logging.getLogger(name)
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
if verbosity > 1:
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
logger.propagate = False
|
||||||
|
return logger
|
|
@ -1,15 +1,21 @@
|
||||||
import django
|
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from django.core.management.commands.runserver import Command as RunserverCommand
|
from django.core.management.commands.runserver import Command as RunserverCommand
|
||||||
from django.core.management import CommandError
|
|
||||||
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
|
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
|
||||||
from channels.worker import Worker
|
|
||||||
from channels.adapters import UrlConsumer
|
from channels.adapters import UrlConsumer
|
||||||
from channels.interfaces.wsgi import WSGIInterface
|
from channels.interfaces.wsgi import WSGIInterface
|
||||||
|
from channels.log import setup_logger
|
||||||
|
from channels.worker import Worker
|
||||||
|
|
||||||
|
|
||||||
class Command(RunserverCommand):
|
class Command(RunserverCommand):
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
self.verbosity = options.get("verbosity", 1)
|
||||||
|
self.logger = setup_logger('django.channels', self.verbosity)
|
||||||
|
super(Command, self).handle(*args, **options)
|
||||||
|
|
||||||
def get_handler(self, *args, **options):
|
def get_handler(self, *args, **options):
|
||||||
"""
|
"""
|
||||||
Returns the default WSGI handler for the runner.
|
Returns the default WSGI handler for the runner.
|
||||||
|
@ -27,9 +33,9 @@ class Command(RunserverCommand):
|
||||||
# Register the default one
|
# Register the default one
|
||||||
self.channel_backend.registry.add_consumer(UrlConsumer(), ["http.request"])
|
self.channel_backend.registry.add_consumer(UrlConsumer(), ["http.request"])
|
||||||
# Note that this is the right one on the console
|
# Note that this is the right one on the console
|
||||||
self.stdout.write("Worker thread running, channels enabled")
|
self.logger.info("Worker thread running, channels enabled")
|
||||||
if self.channel_backend.local_only:
|
if self.channel_backend.local_only:
|
||||||
self.stdout.write("Local channel backend detected, no remote channels support")
|
self.logger.info("Local channel backend detected, no remote channels support")
|
||||||
# Launch a worker thread
|
# Launch a worker thread
|
||||||
worker = WorkerThread(self.channel_backend)
|
worker = WorkerThread(self.channel_backend)
|
||||||
worker.daemon = True
|
worker.daemon = True
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import time
|
|
||||||
from wsgiref.simple_server import BaseHTTPRequestHandler
|
|
||||||
from django.core.management import BaseCommand, CommandError
|
from django.core.management import BaseCommand, CommandError
|
||||||
|
|
||||||
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
|
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
|
||||||
|
from channels.log import setup_logger
|
||||||
from channels.worker import Worker
|
from channels.worker import Worker
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
# Get the backend to use
|
# Get the backend to use
|
||||||
|
self.verbosity = options.get("verbosity", 1)
|
||||||
|
self.logger = setup_logger('django.channels', self.verbosity)
|
||||||
channel_backend = channel_backends[DEFAULT_CHANNEL_BACKEND]
|
channel_backend = channel_backends[DEFAULT_CHANNEL_BACKEND]
|
||||||
if channel_backend.local_only:
|
if channel_backend.local_only:
|
||||||
raise CommandError(
|
raise CommandError(
|
||||||
|
@ -16,10 +18,10 @@ class Command(BaseCommand):
|
||||||
"Configure a network-based backend in CHANNEL_BACKENDS to use this command."
|
"Configure a network-based backend in CHANNEL_BACKENDS to use this command."
|
||||||
)
|
)
|
||||||
# Launch a worker
|
# Launch a worker
|
||||||
self.stdout.write("Running worker against backend %s" % channel_backend)
|
self.logger.info("Running worker against backend %s", channel_backend)
|
||||||
# Optionally provide an output callback
|
# Optionally provide an output callback
|
||||||
callback = None
|
callback = None
|
||||||
if options.get("verbosity", 1) > 1:
|
if self.verbosity > 1:
|
||||||
callback = self.consumer_called
|
callback = self.consumer_called
|
||||||
# Run the worker
|
# Run the worker
|
||||||
try:
|
try:
|
||||||
|
@ -28,12 +30,4 @@ class Command(BaseCommand):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def consumer_called(self, channel, message):
|
def consumer_called(self, channel, message):
|
||||||
self.stdout.write("[%s] %s" % (self.log_date_time_string(), channel))
|
self.logger.debug("%s", channel)
|
||||||
|
|
||||||
def log_date_time_string(self):
|
|
||||||
"""Return the current time formatted for logging."""
|
|
||||||
now = time.time()
|
|
||||||
year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
|
|
||||||
s = "%02d/%3s/%04d %02d:%02d:%02d" % (
|
|
||||||
day, BaseHTTPRequestHandler.monthname[month], year, hh, mm, ss)
|
|
||||||
return s
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import time
|
|
||||||
from django.core.management import BaseCommand, CommandError
|
from django.core.management import BaseCommand, CommandError
|
||||||
|
|
||||||
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
|
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
|
||||||
|
from channels.log import setup_logger
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
|
@ -10,6 +11,8 @@ class Command(BaseCommand):
|
||||||
help='Optional port number')
|
help='Optional port number')
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
|
self.verbosity = options.get("verbosity", 1)
|
||||||
|
self.logger = setup_logger('django.channels', self.verbosity)
|
||||||
# Get the backend to use
|
# Get the backend to use
|
||||||
channel_backend = channel_backends[DEFAULT_CHANNEL_BACKEND]
|
channel_backend = channel_backends[DEFAULT_CHANNEL_BACKEND]
|
||||||
if channel_backend.local_only:
|
if channel_backend.local_only:
|
||||||
|
@ -23,13 +26,13 @@ class Command(BaseCommand):
|
||||||
import asyncio
|
import asyncio
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from channels.interfaces.websocket_twisted import WebsocketTwistedInterface
|
from channels.interfaces.websocket_twisted import WebsocketTwistedInterface
|
||||||
self.stdout.write("Running Twisted/Autobahn WebSocket interface server")
|
self.logger.info("Running Twisted/Autobahn WebSocket interface server")
|
||||||
self.stdout.write(" Channel backend: %s" % channel_backend)
|
self.logger.info(" Channel backend: %s", channel_backend)
|
||||||
self.stdout.write(" Listening on: ws://0.0.0.0:%i" % port)
|
self.logger.info(" Listening on: ws://0.0.0.0:%i" % port)
|
||||||
WebsocketTwistedInterface(channel_backend=channel_backend, port=port).run()
|
WebsocketTwistedInterface(channel_backend=channel_backend, port=port).run()
|
||||||
else:
|
else:
|
||||||
from channels.interfaces.websocket_asyncio import WebsocketAsyncioInterface
|
from channels.interfaces.websocket_asyncio import WebsocketAsyncioInterface
|
||||||
self.stdout.write("Running asyncio/Autobahn WebSocket interface server")
|
self.logger.info("Running asyncio/Autobahn WebSocket interface server")
|
||||||
self.stdout.write(" Channel backend: %s" % channel_backend)
|
self.logger.info(" Channel backend: %s", channel_backend)
|
||||||
self.stdout.write(" Listening on: ws://0.0.0.0:%i" % port)
|
self.logger.info(" Listening on: ws://0.0.0.0:%i", port)
|
||||||
WebsocketAsyncioInterface(channel_backend=channel_backend, port=port).run()
|
WebsocketAsyncioInterface(channel_backend=channel_backend, port=port).run()
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
import traceback
|
import logging
|
||||||
|
|
||||||
from .message import Message
|
from .message import Message
|
||||||
from .utils import name_that_thing
|
from .utils import name_that_thing
|
||||||
|
|
||||||
|
logger = logging.getLogger('django.channels')
|
||||||
|
|
||||||
|
|
||||||
class Worker(object):
|
class Worker(object):
|
||||||
"""
|
"""
|
||||||
|
@ -35,5 +38,4 @@ class Worker(object):
|
||||||
except Message.Requeue:
|
except Message.Requeue:
|
||||||
self.channel_backend.send(channel, content)
|
self.channel_backend.send(channel, content)
|
||||||
except:
|
except:
|
||||||
print("Error processing message with consumer {}:".format(name_that_thing(consumer)))
|
logger.exception("Error processing message with consumer %s:", name_that_thing(consumer))
|
||||||
traceback.print_exc()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user