Using logging module to log either to screen or a file

R

RedBaron

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??
 
J

Jean-Michel Pichavant

RedBaron said:
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
 
R

RedBaron

RedBaron said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top