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.config.fileConfig, NameError: name 'RotatingFileHandler' is not defined #45777

Closed
Sebastian mannequin opened this issue Nov 13, 2007 · 8 comments
Closed

logging.config.fileConfig, NameError: name 'RotatingFileHandler' is not defined #45777

Sebastian mannequin opened this issue Nov 13, 2007 · 8 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@Sebastian
Copy link
Mannequin

Sebastian mannequin commented Nov 13, 2007

BPO 1436
Nosy @gvanrossum, @vsajip, @tiran, @msabramo
Files
  • configurationFiles.zip
  • 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 2007-11-13.18:48:48.309>
    created_at = <Date 2007-11-13.12:41:45.989>
    labels = ['invalid', 'library', 'type-crash']
    title = "logging.config.fileConfig, NameError: name 'RotatingFileHandler' is not defined"
    updated_at = <Date 2012-05-18.17:01:45.904>
    user = 'https://bugs.python.org/sebastian'

    bugs.python.org fields:

    activity = <Date 2012-05-18.17:01:45.904>
    actor = 'Marc.Abramowitz'
    assignee = 'vinay.sajip'
    closed = True
    closed_date = <Date 2007-11-13.18:48:48.309>
    closer = 'vinay.sajip'
    components = ['Library (Lib)']
    creation = <Date 2007-11-13.12:41:45.989>
    creator = 'sebastian'
    dependencies = []
    files = ['8740']
    hgrepos = []
    issue_num = 1436
    keywords = []
    message_count = 8.0
    messages = ['57448', '57452', '57465', '57467', '57702', '57706', '161065', '161066']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'vinay.sajip', 'christian.heimes', 'sebastian', 'Marc.Abramowitz']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue1436'
    versions = ['Python 2.6', 'Python 2.5', 'Python 3.0']

    @Sebastian
    Copy link
    Mannequin Author

    Sebastian mannequin commented Nov 13, 2007

    fileConfig crashes with a NameError when trying to configure a
    RotatingFileHandler (I assume the same holds for other handlers defined
    in logging.handlers). Using StreamHandler (from the logging package)
    works fine. Most likely, I am missing something here, but if not, this
    is a really bad joke...

    RotatingFileHandler is available on my system, a qualified name must be
    used:

    Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) 
    [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import logging
    >>> import logging.handlers
    >>> RotatingFileHandler("test.log", "a", 5000, 5)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'RotatingFileHandler' is not defined
    >>> logging.handlers.RotatingFileHandler("test.log", "a", 5000, 5)
    <logging.handlers.RotatingFileHandler instance at 0x7940d0>

    fileConfig crashes, with or without qualified names:

    >>> import logging.config
    >>> logging.config.fileConfig("test.ini")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File
    "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/logging/config.py",
    line 84, in fileConfig
        handlers = _install_handlers(cp, formatters)
      File
    "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/logging/config.py",
    line 149, in _install_handlers
        klass = eval(klass, vars(logging))
      File "<string>", line 1, in <module>
    NameError: name 'RotatingFileHandler' is not defined
    >>> logging.config.fileConfig("test.qualified_name.ini")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File
    "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/logging/config.py",
    line 84, in fileConfig
        handlers = _install_handlers(cp, formatters)
      File
    "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/logging/config.py",
    line 149, in _install_handlers
        klass = eval(klass, vars(logging))
      File "<string>", line 1, in <module>
    NameError: name 'logging' is not defined

    test.ini (in configurationFiles.zip):
    ---
    [loggers]
    keys=root

    [handlers]
    keys=fileHandler

    [formatters]
    keys=simpleFormatter

    [logger_root]
    level=DEBUG
    handlers=fileHandler

    [handler_fileHandler]
    class=RotatingFileHandler
    level=DEBUG
    formatter=simpleFormatter
    args=('test.log', 'a', 5000000, 5)

    [formatter_simpleFormatter]
    format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    datefmt=
    ---

    test.qualified_name.ini (in configurationFiles.zip):
    ---
    [loggers]
    keys=root

    [handlers]
    keys=fileHandler

    [formatters]
    keys=simpleFormatter

    [logger_root]
    level=DEBUG
    handlers=fileHandler

    [handler_fileHandler]
    class=logging.handlers.RotatingFileHandler
    level=DEBUG
    formatter=simpleFormatter
    args=('test.log', 'a', 5000000, 5)

    [formatter_simpleFormatter]
    format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    datefmt=
    ---

    @Sebastian Sebastian mannequin added stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump labels Nov 13, 2007
    @tiran
    Copy link
    Member

    tiran commented Nov 13, 2007

    confirmed

    The problem is in logging.config._install_handlers(cp, formatters). The
    code is usin klass = eval(klass, vars(logging)) args = eval(args,
    vars(logging)) to get the logger class from the logging module.

    @gvanrossum
    Copy link
    Member

    Somebody please propose a patch!

    @vsajip
    Copy link
    Member

    vsajip commented Nov 13, 2007

    This is not a bug: I think you are missing something. In the first
    example (interactive usage), the lines "import logging" and "import
    logging.handlers" do not magically introduce RotatingFileHandler into
    your interactive session's globals. To do this, you would have to say
    "from logging.handlers import RotatingFileHandler". An analogous example
    unrelated to logging:

    ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on
    Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import httplib
    >>> HTTP
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    NameError: name 'HTTP' is not defined
    >>> httplib.HTTP
    <class httplib.HTTP at 0x00A759F0>
    >>>

    In the second example (using fileConfig), the documentation in

    http://docs.python.org/lib/logging-config-fileformat.html

    clearly states that the class and arguments are evaluated using the
    logging package's namespace. This means that for a handler defined in
    the logging package itself (such as StreamHandler) no qualification is
    required, but for a handler defined in the handlers subpackage, you
    should specify "handlers.RotatingFileHandler" in the configuration file
    rather than "RotatingFileHandler" or "logging.handlers.RotatingFileHandler".

    @vsajip vsajip closed this as completed Nov 13, 2007
    @vsajip vsajip added the invalid label Nov 13, 2007
    @Sebastian
    Copy link
    Mannequin Author

    Sebastian mannequin commented Nov 20, 2007

    thank you very much...

    but: [...] the class and arguments are evaluated using the
    logging package's namespace. [...]

    does this mean, it is not possible to use user-defined handlers,
    naturally residing outside python's class library (and logging package),
    when applying fileConfig()?

    @vsajip
    Copy link
    Member

    vsajip commented Nov 20, 2007

    @msabramo
    Copy link
    Mannequin

    msabramo mannequin commented May 18, 2012

    I just ran into this issue with Python 2.5 (doesn't seem to be an issue in >= 2.6?) and for the benefit of anyone else, I'm copying the answer from Vinay's Google Group post <http://groups.google.com/group/comp.lang.python/browse_thread/thread/21be57fae7e9381a> into this bug, in case the Google group goes away or the URL changes.

    The values in the config file are interpreted in the context of the
    logging module's namespace. Hence, one way of achieving what you 
    want is putting any custom handlers in a module of your own, and
    providing a binding in the logging module's namespace. For example: 
    assuming your DBHandler is defined in a module customhandlers, you 
    could do this somewhere in your code, before loading the 
    configuration:
    
            import logging
            import customhandlers # Use your own module name here
    
            logging.custhandlers = customhandlers # Bind your module to "custhandlers" in logging
    and then your logging configuration can refer to 
    "custhandlers.DBHandler". Of course I merely used "custhandlers" and 
    "customhandlers" to show how you can bind to an arbitrary name.
    

    @msabramo
    Copy link
    Mannequin

    msabramo mannequin commented May 18, 2012

    Or for a practical example, here's how I used the above technique to solve this problem in web2py:

    diff --git a/gluon/main.py b/gluon/main.py
    index 57bf647..2f69c6b 100644
    --- a/gluon/main.py
    +++ b/gluon/main.py
    @@ -68,6 +68,13 @@ create_missing_folders()
     # set up logging for subsequent imports
     import logging
     import logging.config
    +
    +# This needed to prevent exception on Python 2.5:
    +# NameError: name 'gluon' is not defined
    +# See http://bugs.python.org/issue1436
    +import gluon.messageboxhandler
    +logging.gluon = gluon
    +
     logpath = abspath("logging.conf")
     if os.path.exists(logpath):
         logging.config.fileConfig(abspath("logging.conf"))

    @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-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants