K
K
kondratev-ad2022-02-08 14:42:22
Python
kondratev-ad, 2022-02-08 14:42:22

Python logging, how to enable support for Russian letters?

I write logs using logging.config in Python. The problem is that I format the logs in JSON and if there are Russian letters, then it logs like this: {\\n \"errors\": [\\n \"\u0414\u043e\u043a\u0443\u043c\u0435 \u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\"\\n ]\\n}'
I can't set it up, please help

logging.ini
[loggers]
keys=root

[handlers]
keys=fileHandler

[formatters]
keys=json

[logger_root]
level=INFO
handlers=fileHandler

[handler_fileHandler]
class=FileHandler
level=INFO
formatter=json
args=('%(logfilename)s',)

[formatter_json]
class=pythonjsonlogger.jsonlogger.JsonFormatter
format=%(asctime)s - %(levelname)s - %(module)s - %(funcName)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

In code
logger = logging.getLogger(__name__)
logging.config.fileConfig(
    'logging.ini',
    defaults={'logfilename': 'test.log'},
    disable_existing_loggers=False
)
logger.warning(messange)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Nesterov, 2022-02-08
@AlexNest

json.loads(переменная/строка с json)
или 
json.load(файл)

V
Vindicar, 2022-02-08
@Vindicar

The sources say that the constructor of the JsonFormatter class has a corresponding parameter.
How to transfer it through the config, I fz.
Try just adding json_ensure_ascii=False"formatter_json" to the section.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question