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.config.fileConfig, NameError: name 'RotatingFileHandler' is not defined
Type: crash Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: Marc.Abramowitz, christian.heimes, gvanrossum, sebastian, vinay.sajip
Priority: normal Keywords:

Created on 2007-11-13 12:41 by sebastian, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
configurationFiles.zip sebastian, 2007-11-13 12:41
Messages (8)
msg57448 - (view) Author: sebastian (sebastian) Date: 2007-11-13 12:41
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=
---
msg57452 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-13 14:20
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.
msg57465 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-13 18:34
Somebody please propose a patch!
msg57467 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2007-11-13 18:46
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".
msg57702 - (view) Author: sebastian (sebastian) Date: 2007-11-20 13:21
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()?
msg57706 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2007-11-20 19:34
No, it doesn't. See this post:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/21be57fae7e9381a
msg161065 - (view) Author: Marc Abramowitz (Marc.Abramowitz) * Date: 2012-05-18 16:57
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.
msg161066 - (view) Author: Marc Abramowitz (Marc.Abramowitz) * Date: 2012-05-18 17:01
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"))
History
Date User Action Args
2022-04-11 14:56:28adminsetgithub: 45777
2012-05-18 17:01:45Marc.Abramowitzsetmessages: + msg161066
2012-05-18 16:57:13Marc.Abramowitzsetnosy: + Marc.Abramowitz
messages: + msg161065
2007-11-20 19:34:41vinay.sajipsetmessages: + msg57706
2007-11-20 13:21:55sebastiansetmessages: + msg57702
2007-11-13 18:48:48vinay.sajipsetstatus: open -> closed
resolution: not a bug
2007-11-13 18:46:35vinay.sajipsetmessages: + msg57467
2007-11-13 18:34:12gvanrossumsetnosy: + gvanrossum
messages: + msg57465
2007-11-13 14:35:26christian.heimessetpriority: normal
assignee: vinay.sajip
nosy: + vinay.sajip
2007-11-13 14:20:41christian.heimessetnosy: + christian.heimes
messages: + msg57452
versions: + Python 2.6, Python 3.0
2007-11-13 12:41:45sebastiancreate