[Tutor] Logger object not passed between modules

Walter Prins wprins at gmail.com
Tue Jul 26 13:12:51 CEST 2011


Hi Luke,

On 26 July 2011 06:58, Luke Thomas Mergner <lmergner at gmail.com> wrote:

> The logger object is created within a function in the main app class.  Is
> this wrong? I thought that logger ensured the same object is used by both
> classes.  The logger.getLogger(__name__) doesn't set anything other than the
> name used to describe the source of the message, so how do I make sure I'm
> using the same log object?  I assume the problem is either scope (the log
> has to be in the module, not the class) or passing the object properly,
> neither of which I"m very comfortable with obviously.
>


With the caveat that I'm not exactly a logging expert in Python, let me
submit the following observations:
1) You should generally only create logger objects inside modules (whether
main or otherwise) for module specific logging.  Such a created logger
should not be shared with other modules. So yes your current approach at the
very least doesn't go by the the logging module's normal usage pattern(s).

2) If you don't care about module specific logging (and I suggest you do
not, at this stage of your learning), then you should ignore creating your
own logger objects and instead, simply directly log using the logging
*module* (which contains a share loggger object already.)

3) See the section entitled "Logging from multiple modules" here:
http://docs.python.org/howto/logging.html for an example of this.
It demonstrates how you can do some basic configuration of your log in the
main application module and also log from other modules, all without having
to explicitly create your own (module specific) logger objects.

HTH,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110726/18a45e8d/attachment.html>


More information about the Tutor mailing list