From 4cb515136336864de0012595963341f81f262dc7 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 22 Aug 2015 23:35:48 -0700 Subject: [PATCH] Improved handling of logging --- socketio/server.py | 12 +++++++----- tests/test_server.py | 5 +++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/socketio/server.py b/socketio/server.py index 1b57b5a..e4b1aa5 100644 --- a/socketio/server.py +++ b/socketio/server.py @@ -85,12 +85,14 @@ class Server(object): if not isinstance(logger, bool): self.logger = logger else: - logging.basicConfig() self.logger = logging.getLogger('socketio') - if logger: - self.logger.setLevel(logging.INFO) - else: - self.logger.setLevel(logging.ERROR) + if not logging.root.handlers and \ + self.logger.level == logging.NOTSET: + if logger: + self.logger.setLevel(logging.INFO) + else: + self.logger.setLevel(logging.ERROR) + self.logger.addHandler(logging.StreamHandler()) def on(self, event, handler=None, namespace=None): """Register an event handler. diff --git a/tests/test_server.py b/tests/test_server.py index 34ac61a..31c3324 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -361,8 +361,13 @@ class TestServer(unittest.TestCase): def test_logger(self, eio): s = server.Server(logger=False) self.assertEqual(s.logger.getEffectiveLevel(), logging.ERROR) + s.logger.setLevel(logging.NOTSET) s = server.Server(logger=True) self.assertEqual(s.logger.getEffectiveLevel(), logging.INFO) + s.logger.setLevel(logging.WARNING) + s = server.Server(logger=True) + self.assertEqual(s.logger.getEffectiveLevel(), logging.WARNING) + s.logger.setLevel(logging.NOTSET) s = server.Server(logger='foo') self.assertEqual(s.logger, 'foo')