Conditional based on whether or not a module is being used

Pete Emerson pemerson at gmail.com
Sat Mar 6 18:13:33 EST 2010


On Mar 6, 2:38 pm, Vinay Sajip <vinay_sa... at yahoo.co.uk> wrote:
> On Mar 5, 9:29 pm, Pete Emerson <pemer... at gmail.com> wrote:
>
>
>
> > I have written my first module called "logger" that logs to syslog via
> > the syslog module but also allows forloggingto STDOUT in debug mode
> > at multiple levels (to increase verbosity depending on one's need), or
> > both. I've looked at theloggingmodule and while it might suit my
> > needs, it's overkill for me right now (I'm still *very* much a python
> > newbie).
>
> Overkill in what sense? You just need to write a few lines of code to
> be able to use the logging package which comes with Python:
>
> import logging, logging.handlers, sys
> logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
> logging.getLogger().addHandler(logging.handlers.SysLogHandler())
> # default logs to syslog at (localhost, 514) with facility LOG_USER
> # you can change the default to use e.g. Unix domain sockets and a
> different facility
>
> So you're experienced enough and have time enough to write your own
> logger module, but too much of a newbie to use a module which is part
> of Python's included batteries? If you're writing something like
> logging to learn about it and what the issues are, that's fair enough.
> But I can't see what you mean by overkill, exactly. The three lines
> above (or thereabouts) will, I believe, let you log to syslog and to
> stdout...which is what you say you want to do.
>
> > I want to write other modules, and my thinking is that it makes sense
> > for those modules to use the "logger" module to do thelogging, if and
> > only if the parent using the other modules is also using the logger
> > module.
>
> > In other words, I don't want to force someone to use the "logger"
> > module just so they can use my other modules, even if the "logger"
> > module is installed ... but I also want to take advantage of it if I'm
> > using it.
>
> > Now that I've written that, I'm not sure that makes a whole lot of
> > sense. It seems like I could say, "hey, this person has the 'logger'
> > module available, let's use it!".
>
> > Thoughts?
>
> Well, the logging package is available in Python and ready for use and
> pretty much battle tested, so why not use that? Are you planning to
> use third-party libraries in your Python work, or write everything
> yourself? If you are planning to use third party libraries, how would
> their logging be hooked into your logger module? And if not, is it
> good to have two logging systems in parallel?
>
> Of course as the maintainer of Python's logging package, you'd expect
> me to be biased in favour of it. You maybe shouldn't let that sway
> you ;-)
>
> Regards,
>
> Vinay Sajip

Thanks for your insights, Vinay, and thank you also for writing
packages such as logging. The word 'overkill' was a poor choice on my
part! I should have said, "I don't quite understand the logging module
yet, but I am comfortable with the syslog module's two functions,
openlog and syslog".

I wrote my own logger module *partly* to gain the experience, and
partly to do the following:

1) In debug mode, send what would have gone to syslog to STDOUT or
STDERR
2) In non-debug mode, use /dev/log or localhost:514 depending on what
is set
3) Allow for multiple levels of logging beyond INFO, WARNING, CRIT ...
essentially allow multiple levels of INFO depending on how much detail
is desired.  A high level of messaging when programs are running
poorly is desired, but when programs are running smoothly, I don't
need to send as much to syslog.

I started in with your logging package, but I think I simply got ahead
of myself. I definitely agree that writing my own wrappers around
syslog to do what I want might be a duplication of effort. At this
point I think I'm ready to go back to your logging package and see
what I can do; if you have words of advice regarding 1-3 above, I'd
certainly appreciate it.

Now I'll go to your example above and see what it does. Thank you!

Pete



More information about the Python-list mailing list