Ticket #13766: logging.patch
File logging.patch, 4.1 KB (added by , 14 years ago) |
---|
-
django/core/servers/basehttp.py
14 14 import stat 15 15 import sys 16 16 import urllib 17 import logging 17 18 18 19 from django.core.management.color import color_style 19 20 from django.utils.http import http_date … … 26 27 sys_version = "Python/" + sys.version.split()[0] 27 28 software_version = server_version + ' ' + sys_version 28 29 30 st_handler = logging.StreamHandler(sys.stderr) 31 formatter = logging.Formatter("[%(asctime)s] - %(levelname)s - %(message)s") 32 st_handler.setFormatter(formatter) 33 34 log = logging.getLogger("WSGIServer") 35 log.addHandler(st_handler) 36 log.setLevel(logging.DEBUG) 37 admin_log = logging.getLogger("admin") 38 log.addHandler(st_handler) 39 log.setLevel(logging.DEBUG) 40 29 41 class WSGIServerException(Exception): 30 42 pass 31 43 … … 485 497 exc_info[0], exc_info[1], exc_info[2], 486 498 self.traceback_limit, stderr 487 499 ) 488 stderr.flush() 500 501 log.warn(stderr.getvalue()) 489 502 finally: 490 503 exc_info = None 491 504 … … 517 530 518 531 def get_stderr(self): 519 532 return self.stderr 520 533 521 534 def add_cgi_vars(self): 522 535 self.environ.update(self.base_env) 523 536 … … 558 571 # We set self.path to avoid crashes in log_message() on unsupported 559 572 # requests (like "OPTIONS"). 560 573 self.path = '' 561 self.style = color_style()574 #self.style = color_style() 562 575 BaseHTTPRequestHandler.__init__(self, *args, **kwargs) 563 576 564 577 def get_environ(self): … … 595 608 return env 596 609 597 610 def get_stderr(self): 598 return sys.stderr 611 from cStringIO import StringIO 612 fake_stderr = StringIO() 613 return fake_stderr 599 614 600 615 def handle(self): 601 616 """Handle a single HTTP request""" 602 617 self.raw_requestline = self.rfile.readline() 603 618 if not self.parse_request(): # An error code has been sent, just exit 604 619 return 605 handler = ServerHandler(self.rfile, self.wfile, self.get_stderr(), self.get_environ()) 620 log_buffer = self.get_stderr() 621 handler = ServerHandler(self.rfile, self.wfile, log_buffer, self.get_environ()) 606 622 handler.request_handler = self # backpointer for logging 607 623 handler.run(self.server.get_app()) 608 624 log_buffer.close() 625 609 626 def log_message(self, format, *args): 610 # Don't bother logging requests for admin images or the favicon. 611 if self.path.startswith(self.admin_media_prefix) or self.path == '/favicon.ico': 612 return 627 # Delay the decision of logging admin to logging config. 628 if self.path.startswith(self.admin_media_prefix): 629 int_logger = admin_log 630 else: 631 int_logger = log 632 633 msg = "%s\n" % (format % args) 613 634 614 msg = "[%s] %s\n" % (self.log_date_time_string(), format % args)615 616 # Utilize terminal colors, if available617 635 if args[1][0] == '2': 618 # Put 2XX first, since it should be the common case 619 msg = self.style.HTTP_SUCCESS(msg) 636 int_logger.info(msg) 620 637 elif args[1][0] == '1': 621 msg = self.style.HTTP_INFO(msg)638 int_logger.info(msg) 622 639 elif args[1] == '304': 623 msg = self.style.HTTP_NOT_MODIFIED(msg)640 int_logger.info(msg) 624 641 elif args[1][0] == '3': 625 msg = self.style.HTTP_REDIRECT(msg)642 int_logger.info(msg) 626 643 elif args[1] == '404': 627 msg = self.style.HTTP_NOT_FOUND(msg)644 int_logger.warn(msg) 628 645 elif args[1][0] == '4': 629 msg = self.style.HTTP_BAD_REQUEST(msg)646 int_logger.warn(msg) 630 647 else: 631 648 # Any 5XX, or any other response 632 msg = self.style.HTTP_SERVER_ERROR(msg) 633 634 sys.stderr.write(msg) 635 649 int_logger.critical(msg) 650 651 636 652 class AdminMediaHandler(object): 637 653 """ 638 654 WSGI middleware that intercepts calls to the admin media directory, as