AttributeError: logging module bug ?

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
 
N

Neil Cerutti

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'))

__file__ is undefined in your example code, so I'm not getting
the same exception as you.
 
P

Peter Otten

Peter said:
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()
What's the problem ?

Please provide the config file "logging.cfg" to ease debugging.

Peter
 
N

Neil Cerutti

__file__ is undefined in your example code, so I'm not getting
the same exception as you.

Never mind. I was running it in interactive mode, and of course
it doesn't work.
 
P

Peter Otten

Peter said:
Here it is, thanks for having a look
Peter

Unfortunately I still can't reproduce your problem. With a minimal file

../of/logger.py

from logging import Formatter

class RootFormatter(Formatter):
pass
class ModuleFormatter(Formatter):
pass
class ClassFormatter(Formatter):
pass
class DataFormatter(Formatter):
pass

(and an empty ./of/__init__.py) your initial script runs without error. If
you want external help please

(1) make sure that you provide all necessary files needed to reproduce the
error

(2) remove as much of the code as possible that does not contribute to the
problem. (This is also an effective debugging technique)

Peter
 
P

Peter

./of/logger.py

from logging import Formatter

class RootFormatter(Formatter):
pass
class ModuleFormatter(Formatter):
pass
class ClassFormatter(Formatter):
pass
class DataFormatter(Formatter):
pass

(and an empty ./of/__init__.py) your initial script runs without error. If
you want external help please

(1) make sure that you provide all necessary files needed to reproduce the
error

(2) remove as much of the code as possible that does not contribute to the
problem. (This is also an effective debugging technique)

Peter
(1), (2): That is sometimes easier said than done, especially if you
still lack experience in a given language. You are right, though, my
request lacked precision, sorry for the noise !
Your answer helped me to find the problem:

given the minimal files above, in my logging.cfg file , I specified:

[formatter_root]
format=%(levelname)s %(name)s %(message)s
datefmt=%S
# does not work:
class = of.logger.RootFormatter

based on my misunderstanding, that the path to the RootFormatter could
be specfied as a normal python class defined in a package.module .

It seems that I have to say instead:
.....
# works:
class = logger.RootFormatter

This was somehow unexpected for me, since in a module using logger.py, I
could use either import:

from mylogger import logger # without package name

or

from of.mylogger import logger # with package name

but this does not seem to work for the class specification in the config
file (only the former works).

Again, thanks a lot !
Peter
 
G

Gabriel Genellina

This was somehow unexpected for me, since in a module using logger.py, I
could use either import:

from mylogger import logger # without package name

or

from of.mylogger import logger # with package name

but this does not seem to work for the class specification in the config
file (only the former works).

Then you have a big problem with the Python search path (sys.path): you
should *not* have two different (absolute) ways to refer to the same
module, ever.
If "of" is a package, it should not be listed in sys.path
 
P

Peter

Then you have a big problem with the Python search path (sys.path): you
should *not* have two different (absolute) ways to refer to the same
module, ever.
If "of" is a package, it should not be listed in sys.path
Thanks a lot for this helpful advice, in fact the package/module system
is for me ( coming from compiled languages with libaries ) the most
unintuitive thing in python.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top