Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

logging: AttributeError: 'module' object has no attribute 'getLogger'

7,258 views
Skip to first unread message

Frank GOENNINGER

unread,
May 23, 2010, 9:46:48 AM5/23/10
to

Hi all:

Being completely new to Python still (just about a week into it now) I
tried to follow the Python 2.6.5 version documemtation aiming at setting
up a logger as follows:

<code>

import logging

global gPIBLogger

class PIBLogger(object):
'''
TODO: classdocs
'''

def __init__(self, logFileName):
'''
Constructor
'''
self.logFileName = logFileName
self.logger = logging.getLogger('PIBLogger')
self.logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(self.logFileName,
maxBytes=1000000,
backupCount=9)
self.logger.addHandler(handler)
gPIBLogger = self.logger


def main():
mylogger = PIBLogger('/tmp/pib.log')
gPIBLogger.debug(' Hi ')

if __name__ == "__main__":
main()

</code>

When trying to execute main() I get:

Traceback (most recent call last):
File "/.../src/pib/logging.py", line 37, in <module>
main()
File "/.../src/pib/logging.py", line 33, in main
mylogger = PIBLogger('/tmp/pib.log')
File "/...src/pib/logging.py", line 23, in __init__
self.logger = logging.getLogger('PIBLogger')
AttributeError: 'module' object has no attribute 'getLogger'

I double checked and yes, getLogger is there. Why is the interpreter
asking for an "attribute" here ? Any hints on what I am doing wrong ?

TIA!

Regards

Frank

Simon Brunning

unread,
May 23, 2010, 10:04:01 AM5/23/10
to Frank GOENNINGER, python-list
On 23 May 2010 14:46, Frank GOENNINGER <dg1...@googlemail.com> wrote:
> Traceback (most recent call last):
>  File "/.../src/pib/logging.py", line 37, in <module>
>    main()

Here's a clue - looks like your own module is called logging. That's
what's getting imported by your import. Try naming your module
something else, and you should be golden.

--
Cheers,
Simon B.

Duncan Booth

unread,
May 23, 2010, 10:06:22 AM5/23/10
to
Frank GOENNINGER <dg1...@googlemail.com> wrote:

>
> When trying to execute main() I get:
>
> Traceback (most recent call last):
> File "/.../src/pib/logging.py", line 37, in <module>
> main()
> File "/.../src/pib/logging.py", line 33, in main
> mylogger = PIBLogger('/tmp/pib.log')
> File "/...src/pib/logging.py", line 23, in __init__
> self.logger = logging.getLogger('PIBLogger')
> AttributeError: 'module' object has no attribute 'getLogger'
>
> I double checked and yes, getLogger is there. Why is the interpreter
> asking for an "attribute" here ? Any hints on what I am doing wrong ?
>

You're source file appears to be called logging.py, so when you do 'import
logging' it just imports itself. The system logging.py has a getLogger
function, but *your* logging.py doesn't.

Philip Semanchuk

unread,
May 23, 2010, 10:07:12 AM5/23/10
to python-list (General)


Short answer: Change the name of src/pib/logging.py to something else.

Long answer: When Python hits the line "import logging", it first
looks in the current directory and imports logging.py, which in this
case is the file it's already executing. It never finds the standard
library's logging module.

One way you could have figured this out would be to add this as the
first line of main():
print dir(logging)

That would have told you what Python thought the logging module looked
like, and would have perhaps recognized it as your own.

Cheers
Philip

Frank GOENNINGER

unread,
May 24, 2010, 5:55:46 AM5/24/10
to
Simon Brunning <si...@brunningonline.net> writes:

Yep. That was it. Thanks !!

Cheers
Frank

Frank GOENNINGER

unread,
May 24, 2010, 6:00:46 AM5/24/10
to
Philip Semanchuk <phi...@semanchuk.com> writes:

> On May 23, 2010, at 9:46 AM, Frank GOENNINGER wrote:
>>
>> I double checked and yes, getLogger is there. Why is the interpreter
>> asking for an "attribute" here ? Any hints on what I am doing wrong ?
>
>
> Short answer: Change the name of src/pib/logging.py to something else.

Done.

> Long answer: When Python hits the line "import logging", it first
> looks in the current directory and imports logging.py, which in this
> case is the file it's already executing. It never finds the standard
> library's logging module.
>
> One way you could have figured this out would be to add this as the
> first line of main():
> print dir(logging)
>
> That would have told you what Python thought the logging module looked
> like, and would have perhaps recognized it as your own.

Thanks - learned a lot from your post.

>
> Cheers
> Philip

Cheers
Frank

0 new messages