Skip to content
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

Logging cannot handle Unicode logger names #69850

Closed
zephor mannequin opened this issue Nov 19, 2015 · 9 comments
Closed

Logging cannot handle Unicode logger names #69850

zephor mannequin opened this issue Nov 19, 2015 · 9 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@zephor
Copy link
Mannequin

zephor mannequin commented Nov 19, 2015

BPO 25664
Nosy @vsajip, @vstinner

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/vsajip'
closed_at = <Date 2015-12-26.12:22:54.730>
created_at = <Date 2015-11-19.05:36:22.216>
labels = ['type-bug', 'library']
title = 'Logging cannot handle Unicode logger names'
updated_at = <Date 2015-12-26.16:07:43.378>
user = 'https://bugs.python.org/zephor'

bugs.python.org fields:

activity = <Date 2015-12-26.16:07:43.378>
actor = 'vinay.sajip'
assignee = 'vinay.sajip'
closed = True
closed_date = <Date 2015-12-26.12:22:54.730>
closer = 'python-dev'
components = ['Library (Lib)']
creation = <Date 2015-11-19.05:36:22.216>
creator = 'zephor'
dependencies = []
files = []
hgrepos = []
issue_num = 25664
keywords = []
message_count = 9.0
messages = ['254881', '255298', '255317', '255319', '255323', '255855', '257027', '257036', '257040']
nosy_count = 5.0
nosy_names = ['vinay.sajip', 'vstinner', 'python-dev', 'josephgordon', 'zephor']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue25664'
versions = ['Python 2.7']

@zephor
Copy link
Mannequin Author

zephor mannequin commented Nov 19, 2015

# 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

@zephor zephor mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 19, 2015
@vsajip
Copy link
Member

vsajip commented Nov 24, 2015

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 "测试"

@zephor
Copy link
Mannequin Author

zephor mannequin commented Nov 25, 2015

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

@zephor
Copy link
Mannequin Author

zephor mannequin commented Nov 25, 2015

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

@vstinner
Copy link
Member

$ 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

@vsajip
Copy link
Member

vsajip commented Dec 4, 2015

The problem is that logger names which are Unicode are not handled correctly, leading to the error.

@vsajip vsajip changed the title Unexpected UnicodeDecodeError in logging module Logging cannot handle Unicode logger names Dec 4, 2015
@vsajip vsajip self-assigned this Dec 4, 2015
@python-dev
Copy link
Mannequin

python-dev mannequin commented Dec 26, 2015

New changeset 512a628c683e by Vinay Sajip in branch '2.7':
Closes bpo-25664: handled logger names in Unicode.
https://hg.python.org/cpython/rev/512a628c683e

@python-dev python-dev mannequin closed this as completed Dec 26, 2015
@vstinner
Copy link
Member

New changeset 512a628c683e by Vinay Sajip in branch '2.7':
Closes bpo-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 :-/

@vsajip
Copy link
Member

vsajip commented Dec 26, 2015

Why not doing that in record constructor?

You're right, that's probably better. Perhaps I was too hasty ...

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants