diff --git a/asyncssh/logging.py b/asyncssh/logging.py index bdf87ca..09da38b 100644 --- a/asyncssh/logging.py +++ b/asyncssh/logging.py @@ -61,6 +61,9 @@ def get_child(self, child: str = '', context: str = '') -> 'SSHLogger': def log(self, level: int, msg: object, *args, **kwargs) -> None: """Log a message to the underlying logger""" + if not self.isEnabledFor(level): + return + def _item_text(item: _LogArg) -> str: """Convert a list item to text""" diff --git a/tests/test_logging.py b/tests/test_logging.py index 459d632..01f614e 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -20,9 +20,14 @@ """Unit tests for AsyncSSH logging API""" +import logging +import unittest + +from unittest.mock import MagicMock + import asyncssh -from asyncssh.logging import logger +from asyncssh.logging import SSHLogger, logger from asyncssh.session import SSHClientSession from asyncssh.sftp import SFTPServer @@ -49,6 +54,41 @@ def stat(self, path): return super().stat(path) +class _TestSSHLogger(unittest.TestCase): + """Unit tests for log argument conversion""" + + def test_disabled_logging(self): + """Disabled messages should not convert their arguments""" + + for level, disabled in ((logging.WARNING, False), (logging.INFO, True)): + with self.subTest(level=level, disabled=disabled): + parent = logging.Logger('test', level) + parent.disabled = disabled + ssh_logger = SSHLogger(parent) + item = MagicMock() + + ssh_logger.info('Test %s', [item]) + + self.assertEqual(item.mock_calls, []) + + def test_enabled_logging(self): + """Enabled messages should retain argument conversion and context""" + + parent = logging.Logger('test', logging.INFO) + ssh_logger = SSHLogger(parent, context='test context') + item = MagicMock() + item.__str__.return_value = 'item' + + with self.assertLogs(parent, level='INFO') as log: + ssh_logger.info('Test %s %s %s', [b'bytes', item], + ('localhost', 22), b'line\n') + + self.assertEqual(len(item.mock_calls), 1) + self.assertEqual(log.records[0].getMessage(), + '[test context] Test bytes,item localhost, port 22 ' + 'line\\n') + + class _TestLogging(ServerTestCase): """Unit tests for AsyncSSH logging API"""