From cacefba4c2071a33d07d69fa6997d2f7b0c3ac63 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 16 Jun 2016 08:02:56 +0200 Subject: [PATCH] fix traceback logging level logic --- tests/test_logging.py | 30 +++++++++++++++++++++++++++--- wakatime/logger.py | 10 +++++++--- wakatime/offlinequeue.py | 9 ++++----- wakatime/stats.py | 2 +- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/tests/test_logging.py b/tests/test_logging.py index db41fe7..b78fee8 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -119,7 +119,32 @@ class LoggingTestCase(utils.TestCase): self.assertIn('response_code', output[4]) @log_capture() - def test_exception_traceback(self, logs): + def test_exception_traceback_logged_in_debug_mode(self, logs): + logging.disable(logging.NOTSET) + + response = Response() + response.status_code = 0 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + now = u(int(time.time())) + entity = 'tests/samples/codefiles/python.py' + config = 'tests/samples/configs/good_config.cfg' + args = ['--file', entity, '--config', config, '--time', now, '--verbose'] + + with utils.mock.patch('wakatime.stats.open') as mock_open: + mock_open.side_effect = Exception('FooBar') + + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') + + output = u("\n").join([u(' ').join(x) for x in logs.actual()]) + self.assertIn(u('WakaTime DEBUG Traceback (most recent call last):'), output) + self.assertIn(u('Exception: FooBar'), output) + + @log_capture() + def test_exception_traceback_not_logged_normally(self, logs): logging.disable(logging.NOTSET) response = Response() @@ -140,5 +165,4 @@ class LoggingTestCase(utils.TestCase): self.assertEquals(sys.stderr.getvalue(), '') output = u("\n").join([u(' ').join(x) for x in logs.actual()]) - self.assertIn(u('WakaTime ERROR Traceback (most recent call last):'), output) - self.assertIn(u('Exception: FooBar'), output) + self.assertEquals(u(''), output) diff --git a/wakatime/logger.py b/wakatime/logger.py index 3d3f166..c3d7635 100644 --- a/wakatime/logger.py +++ b/wakatime/logger.py @@ -73,11 +73,15 @@ class JsonFormatter(logging.Formatter): def traceback_formatter(*args, **kwargs): - if 'level' in kwargs and (kwargs['level'].lower() == 'warn' or kwargs['level'].lower() == 'warning'): + try: + level = kwargs.get('level', args[0]).lower() + except: + level = None + if level == 'warn' or level == 'warning': logging.getLogger('WakaTime').warning(traceback.format_exc()) - elif 'level' in kwargs and kwargs['level'].lower() == 'info': + elif level == 'info': logging.getLogger('WakaTime').info(traceback.format_exc()) - elif 'level' in kwargs and kwargs['level'].lower() == 'debug': + elif level == 'debug': logging.getLogger('WakaTime').debug(traceback.format_exc()) else: logging.getLogger('WakaTime').error(traceback.format_exc()) diff --git a/wakatime/offlinequeue.py b/wakatime/offlinequeue.py index 9455e2b..dcf9b13 100644 --- a/wakatime/offlinequeue.py +++ b/wakatime/offlinequeue.py @@ -12,7 +12,6 @@ import logging import os -import traceback from time import sleep try: @@ -70,7 +69,7 @@ class Queue(object): conn.commit() conn.close() except sqlite3.Error: - log.error(traceback.format_exc()) + log.traceback() def pop(self): if not HAS_SQL: # pragma: nocover @@ -81,7 +80,7 @@ class Queue(object): try: conn, c = self.connect() except sqlite3.Error: - log.debug(traceback.format_exc()) + log.traceback('debug') return None loop = True while loop and tries > -1: @@ -119,11 +118,11 @@ class Queue(object): } loop = False except sqlite3.Error: # pragma: nocover - log.debug(traceback.format_exc()) + log.traceback('debug') sleep(wait) tries -= 1 try: conn.close() except sqlite3.Error: # pragma: nocover - log.debug(traceback.format_exc()) + log.traceback('debug') return heartbeat diff --git a/wakatime/stats.py b/wakatime/stats.py index 57941fc..935f9b4 100644 --- a/wakatime/stats.py +++ b/wakatime/stats.py @@ -231,7 +231,7 @@ def get_file_head(file_name): try: with open(file_name, 'r', encoding='utf-8') as fh: text = fh.read(512000) - except: # pragma: nocover + except: try: with open(file_name, 'r', encoding=sys.getfilesystemencoding()) as fh: text = fh.read(512000)