fix traceback logging level logic

This commit is contained in:
Alan Hamlett 2016-06-16 08:02:56 +02:00
parent 4ce1419cc8
commit cacefba4c2
4 changed files with 39 additions and 12 deletions

View file

@ -119,7 +119,32 @@ class LoggingTestCase(utils.TestCase):
self.assertIn('response_code', output[4]) self.assertIn('response_code', output[4])
@log_capture() @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) logging.disable(logging.NOTSET)
response = Response() response = Response()
@ -140,5 +165,4 @@ class LoggingTestCase(utils.TestCase):
self.assertEquals(sys.stderr.getvalue(), '') self.assertEquals(sys.stderr.getvalue(), '')
output = u("\n").join([u(' ').join(x) for x in logs.actual()]) output = u("\n").join([u(' ').join(x) for x in logs.actual()])
self.assertIn(u('WakaTime ERROR Traceback (most recent call last):'), output) self.assertEquals(u(''), output)
self.assertIn(u('Exception: FooBar'), output)

View file

@ -73,11 +73,15 @@ class JsonFormatter(logging.Formatter):
def traceback_formatter(*args, **kwargs): 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()) 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()) 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()) logging.getLogger('WakaTime').debug(traceback.format_exc())
else: else:
logging.getLogger('WakaTime').error(traceback.format_exc()) logging.getLogger('WakaTime').error(traceback.format_exc())

View file

@ -12,7 +12,6 @@
import logging import logging
import os import os
import traceback
from time import sleep from time import sleep
try: try:
@ -70,7 +69,7 @@ class Queue(object):
conn.commit() conn.commit()
conn.close() conn.close()
except sqlite3.Error: except sqlite3.Error:
log.error(traceback.format_exc()) log.traceback()
def pop(self): def pop(self):
if not HAS_SQL: # pragma: nocover if not HAS_SQL: # pragma: nocover
@ -81,7 +80,7 @@ class Queue(object):
try: try:
conn, c = self.connect() conn, c = self.connect()
except sqlite3.Error: except sqlite3.Error:
log.debug(traceback.format_exc()) log.traceback('debug')
return None return None
loop = True loop = True
while loop and tries > -1: while loop and tries > -1:
@ -119,11 +118,11 @@ class Queue(object):
} }
loop = False loop = False
except sqlite3.Error: # pragma: nocover except sqlite3.Error: # pragma: nocover
log.debug(traceback.format_exc()) log.traceback('debug')
sleep(wait) sleep(wait)
tries -= 1 tries -= 1
try: try:
conn.close() conn.close()
except sqlite3.Error: # pragma: nocover except sqlite3.Error: # pragma: nocover
log.debug(traceback.format_exc()) log.traceback('debug')
return heartbeat return heartbeat

View file

@ -231,7 +231,7 @@ def get_file_head(file_name):
try: try:
with open(file_name, 'r', encoding='utf-8') as fh: with open(file_name, 'r', encoding='utf-8') as fh:
text = fh.read(512000) text = fh.read(512000)
except: # pragma: nocover except:
try: try:
with open(file_name, 'r', encoding=sys.getfilesystemencoding()) as fh: with open(file_name, 'r', encoding=sys.getfilesystemencoding()) as fh:
text = fh.read(512000) text = fh.read(512000)