dulwich-users team mailing list archive
-
dulwich-users team
-
Mailing list archive
-
Message #00075
[PATCH 4/5] Add logging utilities and server logging.
From: Dave Borowitz <dborowitz@xxxxxxxxxx>
The logging utilities, including default setup, live in log_utils.py.
Callers do not need to import the logging module if all they need is
getLogger.
Change-Id: I5364ae2a2524397b0d13cc727e3f031e3f5adecf
---
NEWS | 2 +
bin/dul-daemon | 2 +
dulwich/log_utils.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++
dulwich/server.py | 18 ++++++++++++-
4 files changed, 88 insertions(+), 1 deletions(-)
create mode 100644 dulwich/log_utils.py
diff --git a/NEWS b/NEWS
index 716af74..b034699 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,8 @@
* Move named file initilization to BaseRepo. (Dave Borowitz)
+ * Add logging utilities and server logging. (Dave Borowitz)
+
TESTS
* Add tests for sorted_tree_items and C implementation. (Dave Borowitz)
diff --git a/bin/dul-daemon b/bin/dul-daemon
index 26067d6..dbc23ea 100755
--- a/bin/dul-daemon
+++ b/bin/dul-daemon
@@ -18,6 +18,7 @@
# MA 02110-1301, USA.
import sys
+from dulwich.log_utils import default_logging_config
from dulwich.repo import Repo
from dulwich.server import DictBackend, TCPGitServer
@@ -27,6 +28,7 @@ if __name__ == "__main__":
else:
gitdir = "."
+ default_logging_config()
backend = DictBackend({"/": Repo(gitdir)})
server = TCPGitServer(backend, 'localhost')
server.serve_forever()
diff --git a/dulwich/log_utils.py b/dulwich/log_utils.py
new file mode 100644
index 0000000..d6e7d62
--- /dev/null
+++ b/dulwich/log_utils.py
@@ -0,0 +1,67 @@
+# log_utils.py -- Logging utilities for Dulwich
+# Copyright (C) 2010 Google, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+
+"""Logging utilities for Dulwich.
+
+Any module that uses logging needs to do compile-time initialization to set up
+the logging environment. Since Dulwich is also used as a library, clients may
+not want to see any logging output. In that case, we need to use a special
+handler to suppress spurious warnings like "No handlers could be found for
+logger dulwich.foo".
+
+For details on the _NullHandler approach, see:
+http://docs.python.org/library/logging.html#configuring-logging-for-a-library
+
+For many modules, the only function from the logging module they need is
+getLogger; this module exports that function for convenience. If a calling
+module needs something else, it can import the standard logging module directly.
+"""
+
+import logging
+import sys
+
+getLogger = logging.getLogger
+
+
+class _NullHandler(logging.Handler):
+ """No-op logging handler to avoid unexpected logging warnings."""
+
+ def emit(self, record):
+ pass
+
+
+_NULL_HANDLER = _NullHandler()
+_DULWICH_LOGGER = getLogger('dulwich')
+_DULWICH_LOGGER.addHandler(_NULL_HANDLER)
+
+
+def default_logging_config():
+ """Set up the default Dulwich loggers."""
+ remove_null_handler()
+ logging.basicConfig(level=logging.INFO, stream=sys.stderr,
+ format='%(asctime)s %(levelname)s: %(message)s')
+
+
+def remove_null_handler():
+ """Remove the null handler from the Dulwich loggers.
+
+ If a caller wants to set up logging using something other than
+ default_logging_config, calling this function first is a minor optimization
+ to avoid the overhead of using the _NullHandler.
+ """
+ _DULWICH_LOGGER.removeHandler(_NULL_HANDLER)
diff --git a/dulwich/server.py b/dulwich/server.py
index 171eb41..14f78b9 100644
--- a/dulwich/server.py
+++ b/dulwich/server.py
@@ -28,8 +28,9 @@ Documentation/technical directory in the cgit distribution, and in particular:
import collections
import socket
-import zlib
import SocketServer
+import sys
+import zlib
from dulwich.errors import (
ApplyDeltaError,
@@ -37,6 +38,7 @@ from dulwich.errors import (
GitProtocolError,
ObjectFormatException,
)
+from dulwich import log_utils
from dulwich.objects import (
hex_to_sha,
)
@@ -58,6 +60,8 @@ from dulwich.protocol import (
)
+logger = log_utils.getLogger(__name__)
+
class Backend(object):
"""A backend for the Git smart server implementation."""
@@ -141,6 +145,7 @@ class DictBackend(Backend):
self.repos = repos
def open_repository(self, path):
+ logger.debug('Opening repository at %s', path)
# FIXME: What to do in case there is no repo ?
return self.repos[path]
@@ -178,6 +183,7 @@ class Handler(object):
raise GitProtocolError('Client does not support required '
'capability %s.' % cap)
self._client_capabilities = set(caps)
+ logger.info('Client capabilities: %s', caps)
def has_capability(self, cap):
if self._client_capabilities is None:
@@ -671,6 +677,7 @@ class TCPGitRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
proto = ReceivableProtocol(self.connection.recv, self.wfile.write)
command, args = proto.read_cmd()
+ logger.info('Handling %s request, args=%s', command, args)
cls = self.handlers.get(command, None)
if not callable(cls):
@@ -690,5 +697,14 @@ class TCPGitServer(SocketServer.TCPServer):
def __init__(self, backend, listen_addr, port=TCP_GIT_PORT, handlers=None):
self.backend = backend
self.handlers = handlers
+ logger.info('Listening for TCP connections on %s:%d', listen_addr, port)
SocketServer.TCPServer.__init__(self, (listen_addr, port),
self._make_handler)
+
+ def verify_request(self, request, client_address):
+ logger.info('Handling request from %s', client_address)
+ return True
+
+ def handle_error(self, request, client_address):
+ logger.exception('Exception happened during processing of request '
+ 'from %s', client_address)
--
1.7.0.4
References