From 1cd03d82de14dfed0b2fa0fcae97249b38457644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9B=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D1=81=D1=82=D0=BE=D0=B2?= Date: Tue, 18 Jul 2017 15:52:31 +0500 Subject: [PATCH] Add Formatter parameter 'ensure_ascii' that can fix cyrillic (and other non-ASCII) utf8-representation troubles on some specific ELK settings. --- docs/config.rst | 5 +++++ logstash_async/formatter.py | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index e1e2721..5427e68 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -98,6 +98,11 @@ extra items passed in the logging call. (default: None) +ensure_ascii + By default ('True') non_ASCII symbols in JSON escaped with \uXXXX sequence. + But on some specific settings of Elastic Stack + this sequences won't be transformed back to UTF8 represintation. + For this specific cases try to set parameter to 'False'. Options for the asynchronous processing (in module logstash_async.constants) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/logstash_async/formatter.py b/logstash_async/formatter.py index 4e48ba7..64dccb5 100644 --- a/logstash_async/formatter.py +++ b/logstash_async/formatter.py @@ -33,12 +33,14 @@ class LogstashFormatter(logging.Formatter): # ---------------------------------------------------------------------- - def __init__(self, message_type='python-logstash', tags=None, fqdn=False, extra_prefix='extra', extra=None): + def __init__(self, message_type='python-logstash', tags=None, fqdn=False, extra_prefix='extra', extra=None, + ensure_ascii=True): super(LogstashFormatter, self).__init__() self._message_type = message_type self._tags = tags if tags is not None else [] self._extra_prefix = extra_prefix self._extra = extra + self._ensure_ascii = ensure_ascii self._interpreter = None self._interpreter_version = None @@ -201,7 +203,7 @@ def _serialize(self, message): if sys.version_info < (3, 0): return json.dumps(message) else: - return bytes(json.dumps(message), 'utf-8') + return bytes(json.dumps(message, ensure_ascii=self._ensure_ascii), 'utf-8') class DjangoLogstashFormatter(LogstashFormatter):