logger: Log the JID during a parsing error.

This commit is contained in:
Emmanuel Gil Peyrot 2019-02-07 02:31:15 +01:00
parent 42b87268b3
commit e5e0533b6e
2 changed files with 9 additions and 9 deletions

View file

@ -56,14 +56,14 @@ class LogMessage(LogItem):
self.nick = nick self.nick = nick
def parse_log_line(msg: str) -> Optional[LogItem]: def parse_log_line(msg: str, jid: str) -> Optional[LogItem]:
match = re.match(MESSAGE_LOG_RE, msg) match = re.match(MESSAGE_LOG_RE, msg)
if match: if match:
return LogMessage(*match.groups()) return LogMessage(*match.groups())
match = re.match(INFO_LOG_RE, msg) match = re.match(INFO_LOG_RE, msg)
if match: if match:
return LogInfo(*match.groups()) return LogInfo(*match.groups())
log.debug('Error while parsing "%s"', msg) log.debug('Error while parsing %ss logs: “%s', jid, msg)
return None return None
@ -176,7 +176,7 @@ class Logger:
filename, filename,
exc_info=True) exc_info=True)
return None return None
return parse_log_lines(lines) return parse_log_lines(lines, jid)
def log_message(self, def log_message(self,
jid: str, jid: str,
@ -306,7 +306,7 @@ def _get_lines_from_fd(fd: IO[Any], nb: int = 10) -> List[str]:
return lines return lines
def parse_log_lines(lines: List[str]) -> List[Dict[str, Any]]: def parse_log_lines(lines: List[str], jid: str) -> List[Dict[str, Any]]:
""" """
Parse raw log lines into poezio log objects Parse raw log lines into poezio log objects
""" """
@ -320,7 +320,7 @@ def parse_log_lines(lines: List[str]) -> List[Dict[str, Any]]:
idx += 1 idx += 1
log.debug('fail?') log.debug('fail?')
continue continue
log_item = parse_log_line(lines[idx]) log_item = parse_log_line(lines[idx], jid)
idx += 1 idx += 1
if not isinstance(log_item, LogItem): if not isinstance(log_item, LogItem):
log.debug('wrong log format? %s', log_item) log.debug('wrong log format? %s', log_item)

View file

@ -7,13 +7,13 @@ from poezio.common import get_utc_time, get_local_time
def test_parse_message(): def test_parse_message():
line = 'MR 20170909T09:09:09Z 000 <nick>  body' line = 'MR 20170909T09:09:09Z 000 <nick>  body'
assert vars(parse_log_line(line)) == vars(LogMessage('2017', '09', '09', '09', '09', '09', '0', 'nick', 'body')) assert vars(parse_log_line(line, 'user@domain')) == vars(LogMessage('2017', '09', '09', '09', '09', '09', '0', 'nick', 'body'))
line = '<>' line = '<>'
assert parse_log_line(line) is None assert parse_log_line(line, 'user@domain') is None
line = 'MR 20170908T07:05:04Z 003 <nick>  ' line = 'MR 20170908T07:05:04Z 003 <nick>  '
assert vars(parse_log_line(line)) == vars(LogMessage('2017', '09', '08', '07', '05', '04', '003', 'nick', '')) assert vars(parse_log_line(line, 'user@domain')) == vars(LogMessage('2017', '09', '08', '07', '05', '04', '003', 'nick', ''))
def test_log_and_parse_messages(): def test_log_and_parse_messages():
@ -27,7 +27,7 @@ def test_log_and_parse_messages():
msg2_utc = get_utc_time(msg2['date']) msg2_utc = get_utc_time(msg2['date'])
assert built_msg2 == 'MR %s 001 <toto>  coucou\n coucou\n' % (msg2_utc.strftime('%Y%m%dT%H:%M:%SZ')) assert built_msg2 == 'MR %s 001 <toto>  coucou\n coucou\n' % (msg2_utc.strftime('%Y%m%dT%H:%M:%SZ'))
assert parse_log_lines((built_msg1 + built_msg2).split('\n')) == [ assert parse_log_lines((built_msg1 + built_msg2).split('\n'), 'user@domain') == [
{'time': msg1['date'], 'history': True, 'txt': '\x195,-1}coucou', 'nickname': 'toto'}, {'time': msg1['date'], 'history': True, 'txt': '\x195,-1}coucou', 'nickname': 'toto'},
{'time': msg2['date'], 'history': True, 'txt': '\x195,-1}coucou\ncoucou', 'nickname': 'toto'}, {'time': msg2['date'], 'history': True, 'txt': '\x195,-1}coucou\ncoucou', 'nickname': 'toto'},
] ]