dulwich-users team mailing list archive
-
dulwich-users team
-
Mailing list archive
-
Message #00076
[PATCH 5/5] Add simple logging to HTTP server.
From: Dave Borowitz <dborowitz@xxxxxxxxxx>
Change-Id: I696bf5dcc05c86e115a85828be5b88d4ed3f2b21
---
NEWS | 2 +-
bin/dul-web | 37 +++++++++++++++++++++++++++++++++----
dulwich/web.py | 13 +++++++++++++
3 files changed, 47 insertions(+), 5 deletions(-)
diff --git a/NEWS b/NEWS
index b034699..8bd018a 100644
--- a/NEWS
+++ b/NEWS
@@ -10,7 +10,7 @@
* Move named file initilization to BaseRepo. (Dave Borowitz)
- * Add logging utilities and server logging. (Dave Borowitz)
+ * Add logging utilities and git/HTTP server logging. (Dave Borowitz)
TESTS
diff --git a/bin/dul-web b/bin/dul-web
index df2cf8d..15b1a0e 100644
--- a/bin/dul-web
+++ b/bin/dul-web
@@ -19,10 +19,37 @@
import os
import sys
+from dulwich.log_utils import default_logging_config
from dulwich.repo import Repo
from dulwich.server import DictBackend
-from dulwich.web import HTTPGitApplication
-from wsgiref.simple_server import make_server
+from dulwich.web import (
+ logger,
+ HTTPGitApplication,
+ )
+from wsgiref.simple_server import (
+ WSGIRequestHandler,
+ make_server,
+ )
+
+
+class HTTPGitRequestHandler(WSGIRequestHandler):
+ """Handler that uses dulwich's logger for logging exceptions."""
+
+ def log_exception(self, exc_info):
+ logger.exception('Exception happened during processing of request',
+ exc_info=exc_info)
+
+ def log_message(self, format, *args):
+ logger.info(format, *args)
+
+ def log_error(self, *args):
+ logger.error(*args)
+
+
+# TODO: allow serving on other addresses/ports via command-line flag
+LISTEN_ADDR=''
+PORT = 8000
+
if __name__ == "__main__":
if len(sys.argv) > 1:
@@ -30,8 +57,10 @@ if __name__ == "__main__":
else:
gitdir = os.getcwd()
+ default_logging_config()
backend = DictBackend({"/": Repo(gitdir)})
app = HTTPGitApplication(backend)
- # TODO: allow serving on other ports via command-line flag
- server = make_server('', 8000, app)
+ server = make_server(LISTEN_ADDR, PORT, app,
+ handler_class=HTTPGitRequestHandler)
+ logger.info('Listening for HTTP connections on %s:%d', LISTEN_ADDR, PORT)
server.serve_forever()
diff --git a/dulwich/web.py b/dulwich/web.py
index a9776ca..e25ec4b 100644
--- a/dulwich/web.py
+++ b/dulwich/web.py
@@ -27,6 +27,7 @@ try:
from urlparse import parse_qs
except ImportError:
from dulwich.misc import parse_qs
+from dulwich import log_utils
from dulwich.protocol import (
ReceivableProtocol,
)
@@ -37,6 +38,9 @@ from dulwich.server import (
)
+logger = log_utils.getLogger(__name__)
+
+
# HTTP error strings
HTTP_OK = '200 OK'
HTTP_NOT_FOUND = '404 Not Found'
@@ -106,12 +110,14 @@ def _url_to_path(url):
def get_text_file(req, backend, mat):
req.nocache()
path = _url_to_path(mat.group())
+ logger.info('Sending plain text file %s', path)
return send_file(req, get_repo(backend, mat).get_named_file(path),
'text/plain')
def get_loose_object(req, backend, mat):
sha = mat.group(1) + mat.group(2)
+ logger.info('Sending loose object %s', sha)
object_store = get_repo(backend, mat).object_store
if not object_store.contains_loose(sha):
yield req.not_found('Object not found')
@@ -128,6 +134,7 @@ def get_loose_object(req, backend, mat):
def get_pack_file(req, backend, mat):
req.cache_forever()
path = _url_to_path(mat.group())
+ logger.info('Sending pack file %s', path)
return send_file(req, get_repo(backend, mat).get_named_file(path),
'application/x-git-packed-objects')
@@ -135,6 +142,7 @@ def get_pack_file(req, backend, mat):
def get_idx_file(req, backend, mat):
req.cache_forever()
path = _url_to_path(mat.group())
+ logger.info('Sending pack file %s', path)
return send_file(req, get_repo(backend, mat).get_named_file(path),
'application/x-git-packed-objects-toc')
@@ -162,6 +170,7 @@ def get_info_refs(req, backend, mat):
# TODO: select_getanyfile() (see http-backend.c)
req.nocache()
req.respond(HTTP_OK, 'text/plain')
+ logger.info('Emulating dumb info/refs')
repo = get_repo(backend, mat)
refs = repo.get_refs()
for name in sorted(refs.iterkeys()):
@@ -182,6 +191,7 @@ def get_info_refs(req, backend, mat):
def get_info_packs(req, backend, mat):
req.nocache()
req.respond(HTTP_OK, 'text/plain')
+ logger.info('Emulating dumb info/packs')
for pack in get_repo(backend, mat).object_store.packs:
yield 'P pack-%s.pack\n' % pack.name()
@@ -211,6 +221,7 @@ class _LengthLimitedFile(object):
def handle_service_request(req, backend, mat):
service = mat.group().lstrip('/')
+ logger.info('Handling service request for %s', service)
handler_cls = req.handlers.get(service, None)
if handler_cls is None:
yield req.forbidden('Unsupported service %s' % service)
@@ -263,12 +274,14 @@ class HTTPGitRequest(object):
def not_found(self, message):
"""Begin a HTTP 404 response and return the text of a message."""
self._cache_headers = []
+ logger.info('Not found: %s', message)
self.respond(HTTP_NOT_FOUND, 'text/plain')
return message
def forbidden(self, message):
"""Begin a HTTP 403 response and return the text of a message."""
self._cache_headers = []
+ logger.info('Forbidden: %s', message)
self.respond(HTTP_FORBIDDEN, 'text/plain')
return message
--
1.7.0.4
References