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 -- http://mail.python.org/mailman/listinfo/python-list