Skip to content

logging using the SysLog handler fails if locale is set #43683

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

Closed
misa mannequin opened this issue Jul 17, 2006 · 3 comments
Closed

logging using the SysLog handler fails if locale is set #43683

misa mannequin opened this issue Jul 17, 2006 · 3 comments
Labels
stdlib Python modules in the Lib dir

Comments

@misa
Copy link
Mannequin

misa mannequin commented Jul 17, 2006

BPO 1524081
Nosy @vsajip, @gnprice
Files
  • logging-broken.py: Simple test script to show the problem. Call it after setting LANG=tr_TR.UTF-8
  • 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 = None
    closed_at = <Date 2020-08-17.23:38:43.754>
    created_at = <Date 2006-07-17.19:33:18.000>
    labels = ['library']
    title = 'logging using the SysLog handler fails if locale is set'
    updated_at = <Date 2020-08-17.23:38:43.754>
    user = 'https://bugs.python.org/misa'

    bugs.python.org fields:

    activity = <Date 2020-08-17.23:38:43.754>
    actor = 'Greg Price'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2006-07-17.19:33:18.000>
    creator = 'misa'
    dependencies = []
    files = ['2071']
    hgrepos = []
    issue_num = 1524081
    keywords = []
    message_count = 3.0
    messages = ['29194', '29195', '375577']
    nosy_count = 3.0
    nosy_names = ['vinay.sajip', 'misa', 'Greg Price']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1524081'
    versions = ['Python 2.5']

    @misa
    Copy link
    Mannequin Author

    misa mannequin commented Jul 17, 2006

    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"

    @misa misa mannequin closed this as completed Jul 17, 2006
    @misa misa mannequin added the stdlib Python modules in the Lib dir label Jul 17, 2006
    @misa misa mannequin closed this as completed Jul 17, 2006
    @misa misa mannequin added the stdlib Python modules in the Lib dir label Jul 17, 2006
    @vsajip
    Copy link
    Member

    vsajip commented Jul 20, 2006

    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.

    @gnprice
    Copy link
    Contributor

    gnprice commented Aug 17, 2020

    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 6ccd3f2, in 2007.

    See also bpo-37848, for a 2019 discussion of potentially adding an optional parameter to use an *explicit* locale.

    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
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants