Python logging module AttributeError: 'str' object has no attribute 'write'

Question:

I am using Tornado, and in my app I import logging so I can log some information about the server.

I put this:

logging.config.dictConfig(web_LOGGING)

right before:

tornado.options.parse_command_line()

but when I run the server and click any link I get the following error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 874, in emit
    stream.write(fs % msg)
AttributeError: 'str' object has no attribute 'write'
Logged from file web.py, line 1946

What is causing this?

I’ve already changed to another directory to steer clear of namespace conflicts.

Asked By: simonchou

||

Answers:

Psychic debugging says web_LOGGING has a key named stream with a str value (probably a file path); stream is only for already opened files, if you want to pass a file path, it’s passed as filename.

Answered By: ShadowRanger

The issue is that when loading from a text file like a yaml or JSON file, python will treat everything like a string unless specified. To do that in this case: ext://sys.stdout

In the yaml file:

...

handlers:
  console:
    class : logging.StreamHandler
    ...
    stream  : ext://sys.stdout
...

Here is the explanation.
Here is the example in the documentation.

Answered By: brendan lafferty
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.