diff --git a/slimta/logging/socket.py b/slimta/logging/socket.py index aca70c92..393f331d 100644 --- a/slimta/logging/socket.py +++ b/slimta/logging/socket.py @@ -23,12 +23,18 @@ from __future__ import absolute_import +import logging from functools import partial from gevent.socket import SHUT_WR, SHUT_RD __all__ = ['SocketLogger'] +#: The log level for logging :py:exc:`socket.error` exceptions. The default log +#: level is `logging.ERROR +# `_. +SOCKET_ERROR_LOG_LEVEL = logging.ERROR + class SocketLogger(object): """Provides a limited set of log methods that :mod:`slimta` packages may @@ -39,10 +45,14 @@ class SocketLogger(object): """ - def __init__(self, log): + def __init__(self, logger): from slimta.logging import logline - self.log = partial(logline, log.debug, 'fd') - self.log_error = partial(logline, log.error, 'fd') + self.logger = logger + self.log = partial(logline, logger.debug, 'fd') + self.log_error = partial(logline, self._log_error, 'fd') + + def _log_error(self, *args, **kwargs): + return self.logger.log(SOCKET_ERROR_LOG_LEVEL, *args, **kwargs) def send(self, socket, data): """Logs a socket :meth:`~socket.socket.send()` operation along with the diff --git a/test/test_slimta_logging_socket.py b/test/test_slimta_logging_socket.py index 97d4d249..0f02f73b 100644 --- a/test/test_slimta_logging_socket.py +++ b/test/test_slimta_logging_socket.py @@ -1,8 +1,11 @@ import unittest2 as unittest +import errno +import logging import socket from testfixtures import log_capture +import slimta.logging.socket from slimta.logging import getSocketLogger @@ -77,5 +80,15 @@ def test_close(self, l): self.log.close(sock) l.check(('test', 'DEBUG', 'fd:771:close')) + @log_capture() + def test_error(self, l): + sock = FakeSocket(680) + exc = OSError(errno.EPIPE, 'Broken pipe') + self.log.error(sock, exc, 'testaddress') + slimta.logging.socket.SOCKET_ERROR_LOG_LEVEL = logging.WARNING + self.log.error(sock, exc) + l.check(('test', 'ERROR', 'fd:680:error address=\'testaddress\' args=(32, \'Broken pipe\') message=\'[Errno 32] Broken pipe\''), + ('test', 'WARNING', 'fd:680:error args=(32, \'Broken pipe\') message=\'[Errno 32] Broken pipe\'')) + # vim:et:fdm=marker:sts=4:sw=4:ts=4