[PYTHON] How to deal with the problem that Japanese characters are garbled when outputting logs using JSON log formatter

I used JSON log formatter because I want to get the log in JSON, but for some reason Japanese (double-byte characters) is garbled.

environment

Windows10 Home 10.0.18362 Build 18362 Python 3.8.3 JSON-log-formatter 0.3.0

Failure example

Execute the following program.

json_log_test.py


import logging
import json_log_formatter

formatter = json_log_formatter.JSONFormatter()

json_handler = logging.FileHandler(filename='my-log.json', encoding='utf-8')#UTF-Output at 8
json_handler.setFormatter(formatter)

logger = logging.getLogger('my_json')
logger.addHandler(json_handler)
logger.setLevel(logging.INFO)

logger.info('This string is garbled', extra={'extra': 'This string is also garbled'})

The output log file "my-log.json" is as follows. (Shaped for easy viewing)

my-log.json


{
    "extra": "\u3053\u306e\u6587\u5b57\u5217\u3082\u6587\u5b57\u5316\u3051\u3059\u308b",
    "message": "\u3053\u306e\u6587\u5b57\u5217\u304c\u6587\u5b57\u5316\u3051\u3059\u308b",
    "time": "2020-10-21T02:29:53.275011"
}

The extra and message are garbled.

solution

Rewrite the "to_json" method of "\ _ \ _ init__.py" in the "json_log_formatter" directory as follows.

__init__.py


    def to_json(self, record):
        """Converts record dict to a JSON string.

        It makes best effort to serialize a record (represents an object as a string)
        instead of raising TypeError if json library supports default argument.
        Note, ujson doesn't support it.

        Override this method to change the way dict is converted to JSON.

        """
        try:
            #return self.json_lib.dumps(record, default=_json_serializable)#Before rewriting
            return self.json_lib.dumps(record, ensure_ascii=False, default=_json_serializable)#After rewriting
        # ujson doesn't support default argument and raises TypeError.
        except TypeError:
            #return self.json_lib.dumps(record)#Before rewriting
            return self.json_lib.dumps(record, ensure_ascii=False)#After rewriting

Add ensure_ascii = False to the argument of the dumps method that converts the dictionary to JSON.

When I execute "json_log_test.py" again and check the output file, the characters are not garbled this time.

my-log.json


{
    "extra": "This string is also garbled",
    "message": "This string is garbled",
    "time": "2020-10-21T02:31:16.704940"
}

Reference site

Memo when Japanese characters are garbled with json.dumps () of Python --Tile memorandum -pygo https://cortyuming.hateblo.jp/entry/20140920/p2 JSON-log-formatter · PyPI https://pypi.org/project/JSON-log-formatter/

Recommended Posts

How to deal with the problem that Japanese characters are garbled when outputting logs using JSON log formatter
[GNOME] How to avoid the problem that conversion candidate characters do not disappear when converting to Japanese
How to deal with the problem that the current directory moves when Python is executed from Atom
How to deal with garbled characters in json of Django REST Framework
How to deal with SessionNotCreatedException when using Selenium
How to deal with the problem that build fails when CI / CD of Python Function with AWS Amplify
[systemd] How to deal with the problem that fancontrol does not work after suspending
A story about how to deal with the CORS problem
How to deal with the terminal getting into the pipenv environment without permission when using pipenv with vscode
[VLC] How to deal with the problem that it is not in the foreground during playback
How to deal with the error "Failed to load module" canberra-gtk-module "that appears when you run OpenCV
[Note] Japanese characters are garbled with atom-runner
How to deal with OAuth2 error when using Google APIs from Python
[AWS] Wordpress How to deal with "The response is not a correct JSON response"
How to deal with errors when hitting pip ②
[Python] How to handle Japanese characters with openCV
How to deal with problems that occur in future prediction using the SARIMA model (seasonal autoregressive integrated moving average model)
How to deal with "Type Error: No matching signature found" error when using pandas fillna
How to disable the reference feature when outputting PyYaml
The story that Kivy's Japanese input characters are displayed
[Linux] How to monitor logs that are constantly added
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
How to deal with the error that Docker's MySQL container fails to start on Docker Toolbox
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python
How to not load images when using PhantomJS with Selenium
How to resolve CSRF Protection when using AngularJS with Django
Solve the Japanese problem when using the CSV module in Python.
How to deal with UnicodeDecodeError when executing google image download
A story that didn't work when I tried to log in with the Python requests module
Python Django How to deal with cases where Japanese carrier emails are played by Email Validator
Workaround for the problem that? Is displayed when checking the version when using non-standard perl | python with pkg version
Half-width katakana characters are not garbled when using python + selenium execute_script
How to deal with errors when installing whitenoise and deploying to Heroku
How to deal with errors when installing Python and pip with choco
[Web development with Python] Measures against garbled characters when outputting html
Memorandum Regular expression When there are multiple characters in the character string that you want to separate
When using the zap library in your own Logger with Golang, how to put the hierarchy above Logger in Caller
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib