简体   繁体   English

Python Structlog-从Unicode字符串隐藏u''

[英]Python Structlog - hide u'' from unicode strings

I have a Python2 application which logs via the structlog library, and downstream the logs are captured an extracted using key/value syntax. 我有一个Python2应用程序,该应用程序通过structlog库进行日志记录,并使用键/值语法提取提取的下游日志。 However, the extraction isn't working when unicode strings are involved - the u is being prepended to unicode strings, breaking the parser. 但是,当涉及到unicode字符串时,提取将不起作用-u被前缀为unicode字符串,从而破坏了解析器。

Is it possible to configure the KeyValueRenderer to exclude the u''? 是否可以将KeyValueRenderer配置为排除u''?

import structlog
structlog.configure(processors=[structlog.processors.KeyValueRenderer()])
l = structlog.get_logger()
l.error('I am ASCII')
l.error(u'I am Unicode')

Result: 结果:

event='I am ASCII'
event=u'I am Unicode'

Desired: 期望的:

event='I am ASCII'
event='I am Unicode'

I know there are questions to change Python's global printing behavior for Unicode strings - but I'm just looking to change the behavior in structlog's approach to printing them. 我知道有一些问题需要更改Python对Unicode字符串的全局打印行为-但我只是想在structlog的打印方法中更改行为。

This is what structlog.processors.UnicodeEncoder is for. 这就是structlog.processors.UnicodeEncoder的用途。 It takes unicode strings and encodes them to byte strings. 它采用unicode字符串并将其编码为字节字符串。

Once you use Python 3, you want structlog.processors.UnicodeDecoder to prevent b prefixes. 使用Python 3后,您希望structlog.processors.UnicodeDecoder阻止b前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM