New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not print utf-8(e.g. Chinese) #94
Comments
Is this Python 2.7? You need |
@hynek
|
OK I’ve looked further into this and it’s interesting! Python 2: >>> print(repr('哈哈哈'))
'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88'
>>> print(repr(u'哈哈哈'))
u'\u54c8\u54c8\u54c8' Python 3: >>> print(repr('哈哈哈'))
'哈哈哈' Python 3 has simply a much better Unicode support. I am not 100% sure what to make of it though. :( |
So I think I found a workaround by adding a new option to (KeyValue|Console)Renderer: >>> import structlog, sys, logging
>>> structlog.get_logger().info("哈哈哈", some_key="哈哈哈")
2016-12-09 12:52.52 哈哈哈 some_key=哈哈哈
>>> handler = logging.StreamHandler(sys.stdout)
>>> root_logger = logging.getLogger()
>>> root_logger.addHandler(handler)
>>> structlog.configure(
... processors=[
... structlog.processors.UnicodeEncoder(),
... structlog.processors.KeyValueRenderer(
... key_order=['event', 'request_id'], repr_native_str=False
... ),
... ],
... context_class=structlog.threadlocal.wrap_dict(dict),
... logger_factory=structlog.stdlib.LoggerFactory(),
... )
>>> structlog.get_logger().warning("哈哈哈")
event=哈哈哈 request_id=None It’s not on PyPI yet; would you mind testing it, if it works for you? |
OK, I'll test it, and will tell you the result ,tks~~ |
It took a bit longer than I hoped, but 17.1 is on PyPI! |
OK, thanks. |
@ianbrayoni you can actually supply those kwargs to the JSONRenderer class through the class JSONRenderer(object):
"""
Render the `event_dict` using ``serializer(event_dict, **json_kw)``.
....
"""
def __init__(self, serializer=json.dumps, **dumps_kw):
self._dumps_kw = dumps_kw
self._dumps = serializer
def __call__(self, logger, name, event_dict):
return self._dumps(event_dict, default=_json_fallback_handler,
**self._dumps_kw) |
I think you want |
What I wanted
What I did
What I get
u'\u54c8\u54c8\u54c8'
below is '哈哈哈' in ChineseWhat I found
__call__
instructlog/processors.py
the classKeyValueRenderer
repr
make thev
from '哈哈哈' tou'\u54c8\u54c8\u54c8'
repr
I get the info belowWhat the last
@hynek
The text was updated successfully, but these errors were encountered: