简体   繁体   中英

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. However, the extraction isn't working when unicode strings are involved - the u is being prepended to unicode strings, breaking the parser.

Is it possible to configure the KeyValueRenderer to exclude the 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.

This is what structlog.processors.UnicodeEncoder is for. It takes unicode strings and encodes them to byte strings.

Once you use Python 3, you want structlog.processors.UnicodeDecoder to prevent b prefixes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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