Skip to content

Commit

Permalink
Allow customizing socket.error log level
Browse files Browse the repository at this point in the history
  • Loading branch information
icgood committed Nov 11, 2016
1 parent d7c5608 commit 33996f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 13 additions & 3 deletions slimta/logging/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# <https://docs.python.org/library/logging.html#logging-levels>`_.
SOCKET_ERROR_LOG_LEVEL = logging.ERROR


class SocketLogger(object):
"""Provides a limited set of log methods that :mod:`slimta` packages may
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/test_slimta_logging_socket.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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

0 comments on commit 33996f3

Please sign in to comment.