Giter VIP home page Giter VIP logo

Comments (10)

nathandouglas avatar nathandouglas commented on May 20, 2024 2

@ianbrayoni you can actually supply those kwargs to the JSONRenderer class through the dumps_kw argument. I ran into the same issue, but noticed this is the source code (snippet below):

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)

from structlog.

hynek avatar hynek commented on May 20, 2024 1

I think you want structlog.processors.UnicodeEncoder.

from structlog.

hynek avatar hynek commented on May 20, 2024

Is this Python 2.7? You need UnicodeEncoder instead of UnicodeDecoder on legacy Python versions.

from structlog.

kongp3 avatar kongp3 commented on May 20, 2024

@hynek
Thanks for your commentary first ^_^
and then
I had tried both, and I had debugged it with the debug tool in Pycharm, and I tried it onece again when I saw your commentary. It was no helped. Here is my demo main code. Thank you for caring me again ^_^

import structlog
import tornado.options
from tornado import autoreload
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.wsgi import WSGIContainer
from flask import Flask

app = Flask(__name__)



if __name__ == '__main__':
    structlog.configure(
        processors=[
            structlog.processors.UnicodeEncoder(),
            structlog.processors.KeyValueRenderer(
                key_order=['event', 'request_id'],
            ),
        ],
        context_class=structlog.threadlocal.wrap_dict(dict),
        logger_factory=structlog.stdlib.LoggerFactory(),
    )
    tornado.options.parse_command_line()
    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(9998)
    instance = IOLoop.instance()
    autoreload.start(instance)
    instance.start()

from structlog.

hynek avatar hynek commented on May 20, 2024

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. :(

from structlog.

hynek avatar hynek commented on May 20, 2024

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?

from structlog.

kongp3 avatar kongp3 commented on May 20, 2024

OK, I'll test it, and will tell you the result ,tks~~

from structlog.

kongp3 avatar kongp3 commented on May 20, 2024

It is worked!
And I am so exciting!!
I pip install form github, and the result is in the picture below
Thanks very much for solving this problem ~_~
If you push it to the PyPI, please kindly @ me here, thank you.

pip install git+https://github.com/hynek/structlog.git
image

from structlog.

hynek avatar hynek commented on May 20, 2024

It took a bit longer than I hoped, but 17.1 is on PyPI!

from structlog.

kongp3 avatar kongp3 commented on May 20, 2024

OK, thanks.

from structlog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.