(beginner) logging config not working
Hi This is a beginner question. Thanks for the hand. I've been asked to maintain some poorly constructed python code. Logging is weak. Getting it to work with python logging programmatically was easy. However, I'd like to refactor all the logging code into configuration since I see the need for other handlers in the future (syslog and possibly email). For now I just want to get console and log file working, but I'm having problems with the log file. I'm trying some test code. Here's my test script (logging_example.py): import logging import logging.config def main(): logging.config.fileConfig("logging.conf") logging.debug("debug check") logging.info("info check") logging.warn("warn check") logging.error("err check") logging.critical("crit check") if __name__ == "__main__": main() Here's my config (logging.conf): [loggers] keys=root,file [handlers] keys=console,file [formatters] keys=simple,detailed [logger_root] level=NOTSET handlers=console [logger_file] level=DEBUG handlers=file qualname=mylogger [formatter_simple] class=logging.Formatter format=%(asctime)s - %(name)s [%(levelname)s] - %(message)s [formatter_detailed] class=logging.Formatter format=%(asctime)s - %(name)s [%(levelname)s] - %(message)s [handler_console] class=logging.StreamHandler formatter=simple args=(sys.stdout,) [handler_file] class=FileHandler level=DEBUG formatter=detailed args=('logging_example.log', 'w') Output: $ python logging_example.py 2011-04-29 17:07:01,923 - root [DEBUG] - debug check 2011-04-29 17:07:01,986 - root [INFO] - info check 2011-04-29 17:07:01,986 - root [WARNING] - warn check 2011-04-29 17:07:01,986 - root [ERROR] - err check 2011-04-29 17:07:02,000 - root [CRITICAL] - crit check The logging_example.log is created, but no entries are written to it. Based on this configuration I'd expect all the logging entries to be written to the file as well. Any ideas where I'm going awry? -- http://mail.python.org/mailman/listinfo/python-list
Re: (beginner) logging config not working
On Apr 29, 10:09 pm, Peter Otten <__pete...@web.de> wrote: > Unknown Moss wrote: > > Hi > > > This is a beginner question. Thanks for the hand. > > > I've been asked to maintain some poorly constructed python code. > > Logging is weak. Getting it to work with python logging > > programmatically was easy. > > > However, I'd like to refactor all the logging code into configuration > > since I see the need for other handlers in the future (syslog and > > possibly email). > > > For now I just want to get console and log file working, but I'm > > having problems with the log file. I'm trying some test code. Here's > > my test script (logging_example.py): > > > import logging > > import logging.config > > > def main(): > > logging.config.fileConfig("logging.conf") > > logging.debug("debug check") > > The above is a shortcut for > > root = logging.getLogger("") > root.debug("debug check") > > i. e. you are logging to the root logger. According to your config file > messages sent to the root logger are only handled by the console handler: > > > [logger_root] > > level=NOTSET > > handlers=console > > You can either change that by adding the file handler to the list of > handlers for the root logger > > handlers=console,file > > in the config file or by directing your logging messages to "mylogger" with > > mylogger = logging.getLogger("mylogger") > mylogger.debug("debug check") > > Note that loggers are organized in a tree; messages sent to mylogger will be > propagated upwords to the root logger by default. Thanks you Peter. That worked perfectly. It points out that I'm not really understanding the python logging concepts yet. I'll search for more details. I need pictures. :-) Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
Re: (beginner) logging config not working
On Apr 29, 10:09 pm, Peter Otten <__pete...@web.de> wrote: > Unknown Moss wrote: > > Hi > > > This is a beginner question. Thanks for the hand. > > > I've been asked to maintain some poorly constructed python code. > > Logging is weak. Getting it to work with python logging > > programmatically was easy. > > > However, I'd like to refactor all the logging code into configuration > > since I see the need for other handlers in the future (syslog and > > possibly email). > > > For now I just want to get console and log file working, but I'm > > having problems with the log file. I'm trying some test code. Here's > > my test script (logging_example.py): > > > import logging > > import logging.config > > > def main(): > > logging.config.fileConfig("logging.conf") > > logging.debug("debug check") > > The above is a shortcut for > > root = logging.getLogger("") > root.debug("debug check") > > i. e. you are logging to the root logger. According to your config file > messages sent to the root logger are only handled by the console handler: > > > [logger_root] > > level=NOTSET > > handlers=console > > You can either change that by adding the file handler to the list of > handlers for the root logger > > handlers=console,file > > in the config file or by directing your logging messages to "mylogger" with > > mylogger = logging.getLogger("mylogger") > mylogger.debug("debug check") > > Note that loggers are organized in a tree; messages sent to mylogger will be > propagated upwords to the root logger by default. Thanks you Peter. That worked perfectly. It points out that I'm not really understanding the python logging concepts yet. I'll search for more details. I need pictures. :-) Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
(beginner question) ConfigParser nuances
Hi - Beginner question here. I'm working with ConfigParser. I'd like to take a multiline variable and convert it directly to an array. Seems like a common problem, but I don't see how I can do it without doing a little parsing in my own code. Here's what I'm doing ... >>> import ConfigParser >>> import io >>> sample = """ ... [Example] ... fruit = apple ... orange ... pear ... """ >>> config = ConfigParser.RawConfigParser() >>> config.readfp(io.BytesIO(sample)) >>> config.get("Example", "fruit") 'apple\norange\npear' >>> temp = config.get("Example", "fruit") >>> temp.split() ['apple', 'orange', 'pear'] I'm thinking there's a way to avoid this intermediate temp.split() step. Is there not a way to move a multiline value straight into an array using ConfigParser? Thanks for the help. -- http://mail.python.org/mailman/listinfo/python-list
Re: (beginner question) ConfigParser nuances
On May 2, 3:25 pm, Chris Rebert wrote: > On Mon, May 2, 2011 at 3:04 PM, Unknown Moss wrote: > > Hi -Beginnerquestionhere. I'm working with ConfigParser. I'd like > > to take a multiline variable and convert it directly to an array. > > Seems like a common problem, but I don't see how I can do it without > > doing a little parsing in my own code. Here's what I'm doing ... > > >>>> import ConfigParser > >>>> import io > >>>> sample = """ > > ... [Example] > > ... fruit = apple > > ... orange > > ... pear > > ... """ > >>>> config = ConfigParser.RawConfigParser() > >>>> config.readfp(io.BytesIO(sample)) > >>>> config.get("Example", "fruit") > > 'apple\norange\npear' > >>>> temp = config.get("Example", "fruit") > >>>> temp.split() > > ['apple', 'orange', 'pear'] > > > I'm thinking there's a way to avoid this intermediate temp.split() > > step. Is there not a way to move a multiline value straight into an > > array using ConfigParser? > > Nope, there is not. I think some might instead use several numbered > options to similar effect: > > # config file > [Example] > fruit1: apple > fruit2: orange > fruit3: pear > > # Python > from itertools import count > fruits = [] > names = ("fruit" + str(i) for i in count(1)) > for name in names: > if not config.has_option("Example", name): > break > fruits.append(config.get("Example", name)) > > Cheers, > Chris > --http://rebertia.com Ok, thanks Chris. Maybe I'm getting too lazy in my old age. :-) -- http://mail.python.org/mailman/listinfo/python-list