P
Peter
on python 2.6 the following code raises an AttributeError:
#!/usr/bin/env python
import os.path as p
import logging, logging.config
class Logger(object):
def load_config(self):
logging.config.fileConfig(p.join(p.dirname(__file__),'logging.cfg'))
logger = Logger()
logger.load_config()
======================================
Traceback (most recent call last):
File "/p/python/exp/of_logging/logging_bug.py", line 11, in <module>
logger.load_config()
File "/p/python/exp/of_logging/logging_bug.py", line 8, in load_config
logging.config.fileConfig(p.join(p.dirname(__file__),'logging.cfg'))
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line 76,
in fileConfig
formatters = _create_formatters(cp)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
130, in _create_formatters
c = _resolve(class_name)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
100, in _resolve
__import__(used)
File "/p/python/of/logger.py", line 144, in <module>
of_logger.load_config(p.join(p.dirname(__file__),'logging.cfg'))
File "/p/python/of/logger.py", line 109, in load_config
logging.config.fileConfig(conffile)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line 76,
in fileConfig
formatters = _create_formatters(cp)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
130, in _create_formatters
c = _resolve(class_name)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
101, in _resolve
found = getattr(found, n)
AttributeError: 'module' object has no attribute 'logger'
From what I understand in the source, the _resolve functions does an
import of the containing module during the parsing of the config file.
In the imported module, the module variable 'logger' is not defined at
that moment and the function fails.
However, I can not see why my code should not work. I want to have a
poor-man's Singleton Logger class ( see Python Cookbook 2nd edition
p.275), so I rebind 'logger' to the only instance of its class.
What's the problem ?
Thanks
Peter
#!/usr/bin/env python
import os.path as p
import logging, logging.config
class Logger(object):
def load_config(self):
logging.config.fileConfig(p.join(p.dirname(__file__),'logging.cfg'))
logger = Logger()
logger.load_config()
======================================
Traceback (most recent call last):
File "/p/python/exp/of_logging/logging_bug.py", line 11, in <module>
logger.load_config()
File "/p/python/exp/of_logging/logging_bug.py", line 8, in load_config
logging.config.fileConfig(p.join(p.dirname(__file__),'logging.cfg'))
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line 76,
in fileConfig
formatters = _create_formatters(cp)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
130, in _create_formatters
c = _resolve(class_name)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
100, in _resolve
__import__(used)
File "/p/python/of/logger.py", line 144, in <module>
of_logger.load_config(p.join(p.dirname(__file__),'logging.cfg'))
File "/p/python/of/logger.py", line 109, in load_config
logging.config.fileConfig(conffile)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line 76,
in fileConfig
formatters = _create_formatters(cp)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
130, in _create_formatters
c = _resolve(class_name)
File "/usr/local/python2.6/lib/python2.6/logging/config.py", line
101, in _resolve
found = getattr(found, n)
AttributeError: 'module' object has no attribute 'logger'
From what I understand in the source, the _resolve functions does an
import of the containing module during the parsing of the config file.
In the imported module, the module variable 'logger' is not defined at
that moment and the function fails.
However, I can not see why my code should not work. I want to have a
poor-man's Singleton Logger class ( see Python Cookbook 2nd edition
p.275), so I rebind 'logger' to the only instance of its class.
What's the problem ?
Thanks
Peter