Using logging module to log either to screen or a file

Discussion in 'Python' started by RedBaron, Dec 7, 2010.

  1. RedBaron

    RedBaron Guest

    Hi,
    I am beginner to python and i am writing a program that does a lot of
    things. One of the requirements is that the program shud generate a
    log file. I came across python loggging module and found it very
    useful. But I have a few problems
    Suppose by giving option '-v' along with the program the user can turn
    off logging to a file and instead display log on the screen. Since I
    am using a config file for logging, how do I accomplish this.
    I tried to define two handlers (fil and screen) and added it to my
    logger. But that logs data to both screen and the file. I need to log
    it to only one. How do I dynamically remove one of the handler from
    the logger based on user option. As a precursor how do i reference the
    handlers defined in config file in the code??
     
    RedBaron, Dec 7, 2010
    #1
    1. Advertising

  2. RedBaron wrote:
    > Hi,
    > I am beginner to python and i am writing a program that does a lot of
    > things. One of the requirements is that the program shud generate a
    > log file. I came across python loggging module and found it very
    > useful. But I have a few problems
    > Suppose by giving option '-v' along with the program the user can turn
    > off logging to a file and instead display log on the screen. Since I
    > am using a config file for logging, how do I accomplish this.
    > I tried to define two handlers (fil and screen) and added it to my
    > logger. But that logs data to both screen and the file. I need to log
    > it to only one. How do I dynamically remove one of the handler from
    > the logger based on user option. As a precursor how do i reference the
    > handlers defined in config file in the code??
    >

    your logger has a public 'handlers' attribute.

    consoleHandlers = [h for h in logger.handlers if h.__class__ is
    logging.StreamHandler] # the list of handlers logging to the console
    (assuming they are instances of the StreamHandler class)

    if consoleHandlers:
    h1 = consoleHandlers[0]
    h1.filter = lambda x:True # enable the handler
    h1.filter = lambda x:False # disable the handler


    JM
     
    Jean-Michel Pichavant, Dec 7, 2010
    #2
    1. Advertising

  3. RedBaron

    RedBaron Guest

    On Dec 7, 7:33 pm, Jean-Michel Pichavant <>
    wrote:
    > RedBaron wrote:
    > > Hi,
    > > I am beginner to python and i am writing a program that does a lot of
    > > things. One of the requirements is that the program shud generate a
    > > log file. I came across python loggging module and found it very
    > > useful. But I have a few problems
    > > Suppose by giving option '-v' along with the program the user can turn
    > > off logging to a file and instead display log on the screen. Since I
    > > am using a config file for logging, how do I accomplish this.
    > > I tried to define two handlers (fil and screen) and added it to my
    > > logger. But that logs data to both screen and the file. I need to log
    > > it to only one. How do I dynamically remove one of the handler from
    > > the logger based on user option. As a precursor how do i reference the
    > > handlers defined in config file in the code??

    >
    > your logger has a public 'handlers' attribute.
    >
    > consoleHandlers = [h for h in logger.handlers if h.__class__ is
    > logging.StreamHandler] # the list of handlers logging to the console
    > (assuming they are instances of the StreamHandler class)
    >
    > if consoleHandlers:
    >     h1 = consoleHandlers[0]
    >     h1.filter = lambda x:True # enable the handler
    >     h1.filter = lambda x:False # disable the handler
    >
    > JM


    Thanks JM,
    This works like charm. I had also though on similar lines bt I was
    using isinstance(). I have two handlers - logging.RotatingFIleHandler
    and StreamHandler. isinstance() was weird in the sense that no matter
    which handle I checked for being 'StreamHandler' I always got true.
    Also instead of setting filter to false, I was popping from the
    handlers list...Silly me
    Thanks a ton
     
    RedBaron, Dec 8, 2010
    #3
    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. rh0dium
    Replies:
    2
    Views:
    356
    rh0dium
    Aug 4, 2005
  2. fuzzylollipop
    Replies:
    1
    Views:
    392
  3. =?iso-8859-1?q?=C1lvaro_Nieto?=

    logging module: log file with datetime

    =?iso-8859-1?q?=C1lvaro_Nieto?=, Jun 1, 2007, in forum: Python
    Replies:
    1
    Views:
    313
    Vinay Sajip
    Jun 1, 2007
  4. Royan
    Replies:
    1
    Views:
    1,512
    Andrea Francia
    Apr 1, 2008
  5. kretel
    Replies:
    6
    Views:
    344
    Jorgen Grahn
    Jun 10, 2009
Loading...

Share This Page