diff --git a/swh/core/logger.py b/swh/core/logger.py --- a/swh/core/logger.py +++ b/swh/core/logger.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015 The Software Heritage developers +# Copyright (C) 2015-2016 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -14,7 +14,7 @@ current_task = None from psycopg2.extras import Json - +from swh.core import serializers EXTRA_LOGDATA_PREFIX = 'swh_' @@ -27,6 +27,11 @@ return logging.getLevelName(lvl).lower() +class SWHJson(Json): + def dumps(self, obj): + return serializers.SWHJSONEncoder().encode(obj) + + class PostgresHandler(logging.Handler): """log handler that store messages in a Postgres DB @@ -90,7 +95,7 @@ } log_entry = (db_level_of_py_level(log_data['levelno']), msg, - Json(extra_data), log_data['name'], self.fqdn, + SWHJson(extra_data), log_data['name'], self.fqdn, os.getpid()) db = self._connect() with db.cursor() as cur: diff --git a/swh/core/tests/test_logger.py b/swh/core/tests/test_logger.py --- a/swh/core/tests/test_logger.py +++ b/swh/core/tests/test_logger.py @@ -1,8 +1,9 @@ -# Copyright (C) 2015 The Software Heritage developers +# Copyright (C) 2015-2016 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import base64 import logging import os import unittest @@ -39,6 +40,14 @@ self.logger.info('notice', extra={'swh_type': 'test entry', 'swh_data': 42}) self.logger.warn('warning') + # Deal with bytes + data_as_bytes = b'some-data' + self.logger.error('error', extra={ + 'swh_type': 'dict', + 'swh_data': { + 'something': [data_as_bytes] + } + }) with self.conn.cursor() as cur: cur.execute('SELECT level, message, data, src_module FROM log') @@ -48,3 +57,14 @@ self.modname), db_log_entries) self.assertIn(('warning', 'warning', {}, self.modname), db_log_entries) + + expected_data = base64.b85encode(data_as_bytes).decode('ascii') + self.assertIn( + ('error', 'error', { + 'type': 'dict', + 'data': {'something': [{ + 'd': expected_data, + 'swhtype': 'bytes', + }]}, + }, self.modname), + db_log_entries)