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 using the SysLog handler fails if locale is set
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Greg Price, misa, vinay.sajip
Priority: normal Keywords:

Created on 2006-07-17 19:33 by misa, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
logging-broken.py misa, 2006-07-17 19:33 Simple test script to show the problem. Call it after setting LANG=tr_TR.UTF-8
Messages (3)
msg29194 - (view) Author: Mihai Ibanescu (misa) Date: 2006-07-17 19:33
This affectes b2 and python 2.4 too.
Initially reported here:
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=198971

Looking at logging/handlers.py:

Line 635:
self.encodePriority(self.facility,                    
            string.lower(record.levelname)), msg)

Line 611 in encodePriority:
priority = self.priority_names[priority]

priority_names is declared on line 527:
    priority_names = {
        "alert":    LOG_ALERT,
        "crit":     LOG_CRIT,
        ...
        "info":     LOG_INFO,
        ...

Now, if one initializes the locale (i.e. 
locale.setlocale(locale.LC_ALL, "")

and then tries to use the logging module, it will fail
with an exception looking kinda like:

  File "/usr/lib64/python2.4/logging/handlers.py", line
627, in encodePriority
    priority = self.priority_names[priority]
KeyError: 'Info'

if they choose LANG=tr_TR.UTF-8

This happens because in that particular locale,
"INFO".lower() != "info"
msg29195 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2006-07-20 23:27
Logged In: YES 
user_id=308438

Fixed by using a dict to map logging level names to syslog
priority level names. This also facilitates usage when
custom logging levels are present.

Fix is checked into Subversion.
msg375577 - (view) Author: Greg Price (Greg Price) * Date: 2020-08-17 23:38
For the record because this issue is mentioned in a comment in logging/handlers.py and people are sometimes confused by it today:

> This happens because in that particular locale,
> "INFO".lower() != "info"

Since Python 3, this no longer happens: str.lower() and friends do not depend on the current locale.

Specifically, the lower() and similar methods on Unicode strings (now "str", previously "unicode") were always independent of the current locale.  The corresponding methods on byte strings (now "bytes", previously "str") did have this locale-dependent behavior, but that was replaced in commit 6ccd3f2dbcb98b33a71ffa6eae949deae797c09c, in 2007.

See also #37848, for a 2019 discussion of potentially adding an optional parameter to use an *explicit* locale.
History
Date User Action Args
2022-04-11 14:56:18adminsetgithub: 43683
2020-08-17 23:38:43Greg Pricesetnosy: + Greg Price
messages: + msg375577
2006-07-17 19:33:18misacreate