Ticket #90: logconf.py

"""
Hook to configure logging when running nose.
"""
import logging
import os

from nose.plugins import Plugin


class LogConfig(Plugin):
    def __init__(self):
        self.enabled = True
        self.name = 'logconf'
        Plugin.__init__(self)
    def add_options(self, parser, env=os.environ):
        parser.add_option('--log-filename',
                          default=env.get('NOSE_LOGFILE'),
                          help='logging output file')
        parser.add_option('--log-config',
                          default=env.get('NOSE_LOGCONF'),
                          help='logging config file')
    def configure(self, options, config):
        if options.log_filename and options.log_config:
            raise ValueError('--log-filename and -log-config are mutually exclusive')
        if options.log_filename:
            logging.basicConfig(level=logging.DEBUG,
                    format='%(levelname)s:%(asctime)s:%(pathname)s:%(lineno)s>%(message)s',
                    filename=options.log_filename,
                    filemode='w')
        if options.log_config:
            logging.fileConfig(options.log_config)