[Tutor] "alias" instance of logger

Kent Johnson kent37 at tds.net
Sat Oct 6 06:24:36 CEST 2007

Robert Jackson wrote:

> log = logging.basicConfig(level=logging.DEBUG,filename="/home/richard/templog",filemode='w')

logging.basicConfig() does not return a logger, it returns None.

> Later in my program I do:
> log.info("finished step 4.")
> Python spits out this error:
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'NoneType' object has no attribute 'info'

Because log is None and None does not have an 'info' attribute.
> I CAN, however, do:
> logging.info("finished step 4.")

Sure, logging is the logging module and it has an info function. This is 
normal usage.

> What confuses me about this is that I can do something like this:
> # call another function and pass it the logger instance:
> foo(logging)

You are not passing a logger, you are passing the logging module itself.

> And, if I define foo() like this:
> def foo(log):
>    # this works fine!
>    log.info("finished step 4.")
> The log.info works fine inside of foo().

Sure, because inside of foo, the name 'log' is bound to the actual 
parameter passed in which is the logging module.

> Why is it that I can pass logging as an instance into a function, and
> use whatever instance name I wants inside of foo(),

That is true of every function parameter, you can give them whatever 
name you want.

> but I can't assign
> an "alias" for the logging instance inside of main() (by doing instancealias = logging.basic())?

Because you are not aliasing logging here. Just assign
log = logging

though I don't see the point, just use logging.info() etc.


More information about the Tutor mailing list