Re: improvements sought re. logging across modules

Discussion in 'Python' started by Fábio Santos, Apr 24, 2013.

  1. Maybe import mylogger.mylogger as gLog? I don't know what you mean by
    "missing a trick". Your example seems pretty pythonic to me, except for the
    fact that you use a singleton where you could have a couple of functions
    and use the module as the namespace.
    On 24 Apr 2013 17:58, "The Night Tripper" <> wrote:

    > Hi all
    > I have a small suite of python modules, say
    >
    > A.py
    > B.py
    > C.py
    >
    > which can be invoked in a variety of ways. eg.
    >
    > 1) A.py is invoked directly; this imports and uses B.py and C.py
    >
    > 2) B.py is invoked; this imports and uses A.py and C.py
    >
    > I use the logging module in all of these python modules, and I want to be
    > able to use a single logger across the entire suite of whichever set of
    > scripts is running.
    >
    > The way I do this at the moment is to have a separate module mylogger.py:
    >
    > == mylogger.py ==
    >
    > import logging
    >
    > class MyLogger: #using python 2.4 ;-o
    > def __init__(self):
    > self.log = logging.getLogger(MY_APP_NAME)
    > def setupLogging(self):
    > self.log.setlevel(logging.DEBUG)
    > # ...
    >
    > # our singleton logging object
    > mylogger = Mylogger()
    > # EOF
    >
    > and then in my other modules A.py, B.py etc. I have something like:
    >
    > == A.py ==
    >
    > import mylogger
    > gLog = mylogger.mylogger
    >
    > if __name__ == "__main__":
    > gLog.setupLogging()
    > gLog.info("Module A running as main")
    > main()
    > #EOF
    >
    > == B.py ==
    >
    > import mylogger
    > gLog = mylogger.mylogger
    >
    > if __name__ == "__main__":
    > gLog.setupLogging()
    > gLog.info("Module B running as main")
    > main()
    > # EOF
    >
    > This works, but I can't help thinking I'm missing a trick here. Any
    > suggestions?
    >
    > Thanks
    > j^n
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Fábio Santos, Apr 24, 2013
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rotem
    Replies:
    23
    Views:
    660
    Trent Mick
    Sep 15, 2005
  2. Matthew Wilson
    Replies:
    2
    Views:
    283
    Martin v. Loewis
    May 11, 2010
  3. Jason Friedman
    Replies:
    0
    Views:
    176
    Jason Friedman
    Feb 3, 2012
  4. Chris “Kwpolska†Warrick

    Re: improvements sought re. logging across modules

    Chris “Kwpolska†Warrick, Apr 24, 2013, in forum: Python
    Replies:
    0
    Views:
    88
    Chris “Kwpolska†Warrick
    Apr 24, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    101
    Dave Angel
    Apr 24, 2013
Loading...

Share This Page