This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Logging cannot handle Unicode logger names
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: josephgordon, python-dev, vinay.sajip, vstinner, zephor
Priority: normal Keywords:

Created on 2015-11-19 05:36 by zephor, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (9)
msg254881 - (view) Author: Zephor Wu (zephor) Date: 2015-11-19 05:36
# coding=utf-8
import logging

logger = logging.getLogger(u'测试')
logger.error(u'测试')

# these code will get an unexpected error
# because getLogger encode the unicode to utf-8 while _log don't
# see line 474 in logging/__init__.py
# my suggestion is to keep the encoding in logging same with python2 with unicode
msg255298 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2015-11-24 22:49
I cannot reproduce, see below. Which exact version of Python are you using, and on what platform, and with what locale?

$ more logtest9.py
# coding=utf-8
import logging

logger = logging.getLogger(u'测试')
logger.error(u'测试')
$ python2.7 logtest9.py 
No handlers could be found for logger "测试"
msg255317 - (view) Author: Zephor Wu (zephor) Date: 2015-11-25 03:15
sorry, i forgot this:

logging.basicConfig(format='%(name)s %(message)s')

the error raised at the processing of the format while the name is 'utf-8' encoded and the message is an unicode
msg255319 - (view) Author: Zephor Wu (zephor) Date: 2015-11-25 03:28
here is the case, sorry again for just typing it before and not testing it myself :)


# coding=utf-8
import logging

logging.basicConfig(format='%(name)s %(message)s')

logger = logging.getLogger(u'测试')
logger.error(u'测试')


btw, i'm using python2.7.10, mac osx 10.11, but i think this can effect all python2
msg255323 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-11-25 07:52
$ cat x.py 
# coding=utf-8
import logging
logging.basicConfig(format='%(name)s %(message)s')
logger = logging.getLogger(u'测试')
logger.error(u'测试')

$ python2.7 x.py 
Traceback (most recent call last):
  File "/usr/lib64/python2.7/logging/__init__.py", line 859, in emit
    msg = self.format(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 732, in format
    return fmt.format(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 474, in format
    s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
Logged from file x.py, line 8
msg255855 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2015-12-04 10:37
The problem is that logger names which are Unicode are not handled correctly, leading to the error.
msg257027 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-12-26 12:22
New changeset 512a628c683e by Vinay Sajip in branch '2.7':
Closes #25664: handled logger names in Unicode.
https://hg.python.org/cpython/rev/512a628c683e
msg257036 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-12-26 15:01
> New changeset 512a628c683e by Vinay Sajip in branch '2.7':
> Closes #25664: handled logger names in Unicode.
> https://hg.python.org/cpython/rev/512a628c683e

.format() doesn't look to me like the best place to encode the name. Why not doing that in record constructor?

I don't want to add one try/except for each record field :-/
msg257040 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2015-12-26 16:07
> Why not doing that in record constructor?

You're right, that's probably better. Perhaps I was too hasty ...
History
Date User Action Args
2022-04-11 14:58:24adminsetgithub: 69850
2015-12-26 16:07:43vinay.sajipsetmessages: + msg257040
2015-12-26 15:01:24vstinnersetmessages: + msg257036
2015-12-26 12:22:54python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg257027

resolution: fixed
stage: resolved
2015-12-22 08:24:12josephgordonsetnosy: + josephgordon
2015-12-04 10:37:56vinay.sajipsetassignee: vinay.sajip
messages: + msg255855
title: Unexpected UnicodeDecodeError in logging module -> Logging cannot handle Unicode logger names
2015-11-25 07:52:27vstinnersetnosy: + vstinner
messages: + msg255323
2015-11-25 03:28:25zephorsetmessages: + msg255319
2015-11-25 03:15:16zephorsetmessages: + msg255317
2015-11-24 22:49:06vinay.sajipsetmessages: + msg255298
2015-11-19 10:35:04serhiy.storchakasetnosy: + vinay.sajip
2015-11-19 05:39:03zephorsettype: behavior
2015-11-19 05:36:22zephorcreate