Python logging.NullHandler() Examples

The following are 30 code examples of logging.NullHandler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logging , or try the search function .
Example #1
Source File: ext_logging.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
def _setup_console_log(self):
        """Add a console log handler."""
        namespace = self._meta.namespace
        to_console = self.app.config.get(self._meta.config_section,
                                         'to_console')
        if is_true(to_console):
            console_handler = logging.StreamHandler()
            format = self._get_console_format()
            formatter = self._get_console_formatter(format)
            console_handler.setFormatter(formatter)
            console_handler.setLevel(getattr(logging, self.get_level()))
        else:
            console_handler = NullHandler()

        # FIXME: self._clear_loggers() should be preventing this but its not!
        for i in logging.getLogger("cement:app:%s" % namespace).handlers:
            if isinstance(i, logging.StreamHandler):
                self.backend.removeHandler(i)

        self.backend.addHandler(console_handler) 
Example #2
Source File: logger.py    From python-sdk with Apache License 2.0 6 votes vote down vote up
def adapt_logger(logger):
    """
  Adapt our custom logger.BaseLogger object into a standard logging.Logger object.

  Adaptations are:
    - NoOpLogger turns into a logger with a single NullHandler.
    - SimpleLogger turns into a logger with a StreamHandler and level.

  Args:
    logger: Possibly a logger.BaseLogger, or a standard python logging.Logger.

  Returns: a standard python logging.Logger.

  """
    if isinstance(logger, logging.Logger):
        return logger

    # Use the standard python logger created by these classes.
    if isinstance(logger, (SimpleLogger, NoOpLogger)):
        return logger.logger

    # Otherwise, return whatever we were given because we can't adapt.
    return logger 
Example #3
Source File: test_logger.py    From python-sdk with Apache License 2.0 6 votes vote down vote up
def test_adapt_logger__noop(self):
        """Test that adapt_logger returns a standard python logger from a NoOpLogger."""
        noop_logger = _logger.NoOpLogger()
        standard_logger = _logger.adapt_logger(noop_logger)

        # adapt_logger knows about the loggers attached to this class.
        self.assertIs(noop_logger.logger, standard_logger)

        # Verify properties of the logger
        self.assertIsInstance(standard_logger, logging.Logger)
        self.assertEqual('optimizely.logger.NoOpLogger', standard_logger.name)
        self.assertEqual(logging.NOTSET, standard_logger.level)

        # Should have a single NullHandler (with a default formatter).
        self.assertEqual(1, len(standard_logger.handlers))
        handler = standard_logger.handlers[0]
        self.assertIsInstance(handler, logging.NullHandler)
        self.assertEqual(
            '%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s:%(message)s', handler.formatter._fmt,
        ) 
Example #4
Source File: guidedlda.py    From GuidedLDA with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self, n_topics, n_iter=2000, alpha=0.01, eta=0.01, random_state=None,
                 refresh=10):
        self.n_topics = n_topics
        self.n_iter = n_iter
        self.alpha = alpha
        self.eta = eta
        # if random_state is None, check_random_state(None) does nothing
        # other than return the current numpy RandomState
        self.random_state = random_state
        self.refresh = refresh

        if alpha <= 0 or eta <= 0:
            raise ValueError("alpha and eta must be greater than zero")

        # random numbers that are reused
        rng = guidedlda.utils.check_random_state(random_state)
        if random_state:
            random.seed(random_state)
        self._rands = rng.rand(1024**2 // 8)  # 1MiB of random variates

        # configure console logging if not already configured
        if len(logger.handlers) == 1 and isinstance(logger.handlers[0], logging.NullHandler):
            logging.basicConfig(level=logging.INFO) 
Example #5
Source File: Etrigan.py    From py-kms with The Unlicense 6 votes vote down vote up
def setup_files(self):
                self.pidfile = os.path.abspath(self.pidfile)
                
                if self.logfile is not None:                     
                        self.logdaemon = logging.getLogger('logdaemon')
                        self.logdaemon.setLevel(self.loglevel)
                        
                        filehandler = logging.FileHandler(self.logfile)
                        filehandler.setLevel(self.loglevel)
                        formatter = logging.Formatter(fmt = '[%(asctime)s] [%(levelname)8s] --- %(message)s',
                                                      datefmt = '%Y-%m-%d %H:%M:%S')
                        filehandler.setFormatter(formatter)
                        self.logdaemon.addHandler(filehandler)
                else:
                        nullhandler = logging.NullHandler()
                        self.logdaemon.addHandler(nullhandler) 
Example #6
Source File: __init__.py    From flask-restplus-server-example with MIT License 6 votes vote down vote up
def init_app(self, app):
        """
        Common Flask interface to initialize the logging according to the
        application configuration.
        """
        # We don't need the default Flask's loggers when using our invoke tasks
        # since we set up beautiful colorful loggers globally.
        for handler in list(app.logger.handlers):
            app.logger.removeHandler(handler)
        app.logger.propagate = True

        if app.debug:
            logging.getLogger('flask_oauthlib').setLevel(logging.DEBUG)
            app.logger.setLevel(logging.DEBUG)

        # We don't need the default SQLAlchemy loggers when using our invoke
        # tasks since we set up beautiful colorful loggers globally.
        # NOTE: This particular workaround is for the SQLALCHEMY_ECHO mode,
        # when all SQL commands get printed (without these lines, they will get
        # printed twice).
        sqla_logger = logging.getLogger('sqlalchemy.engine.base.Engine')
        for hdlr in list(sqla_logger.handlers):
            sqla_logger.removeHandler(hdlr)
        sqla_logger.addHandler(logging.NullHandler()) 
Example #7
Source File: process.py    From ms_deisotope with Apache License 2.0 6 votes vote down vote up
def _silence_loggers(self):
        nologs = ["deconvolution_scan_processor"]
        if not self.deconvolute:
            nologs.append("deconvolution")

        debug_mode = os.getenv("MS_DEISOTOPE_DEBUG")
        if debug_mode:
            handler = logging.FileHandler("ms-deisotope-deconvolution-debug-%s.log" % (os.getpid()), 'w')
            fmt = logging.Formatter(
                "%(asctime)s - %(name)s:%(filename)s:%(lineno)-4d - %(levelname)s - %(message)s",
                "%H:%M:%S")
            handler.setFormatter(fmt)
        for logname in nologs:
            logger_to_silence = logging.getLogger(logname)
            if debug_mode:
                logger_to_silence.setLevel("DEBUG")
                logger_to_silence.addHandler(handler)
            else:
                logger_to_silence.propagate = False
                logger_to_silence.setLevel("CRITICAL")
                logger_to_silence.addHandler(logging.NullHandler()) 
Example #8
Source File: engine.py    From LaSO with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, process_function):
        self._event_handlers = defaultdict(list)
        self._logger = logging.getLogger(__name__ + "." + self.__class__.__name__)
        self._logger.addHandler(logging.NullHandler())
        self._process_function = process_function
        self.should_terminate = False
        self.should_terminate_single_epoch = False
        self.state = None
        self._allowed_events = []

        self.register_events(*Events)

        if self._process_function is None:
            raise ValueError("Engine must be given a processing function in order to run.")

        self._check_signature(process_function, 'process_function', None) 
Example #9
Source File: log.py    From stem with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_is_tracing(self):
    logger = log.get_logger()
    original_handlers = logger.handlers
    logger.handlers = [log._NullHandler()]

    try:
      self.assertFalse(log.is_tracing())

      handler = logging.NullHandler()
      handler.setLevel(log.DEBUG)
      logger.addHandler(handler)

      self.assertFalse(log.is_tracing())

      handler = logging.NullHandler()
      handler.setLevel(log.TRACE)
      logger.addHandler(handler)

      self.assertTrue(log.is_tracing())
    finally:
      logger.handlers = original_handlers 
Example #10
Source File: early_stopping.py    From LaSO with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, patience, score_function, trainer):

        if not callable(score_function):
            raise TypeError("Argument score_function should be a function.")

        if patience < 1:
            raise ValueError("Argument patience should be positive integer.")

        if not isinstance(trainer, Engine):
            raise TypeError("Argument trainer should be an instance of Engine.")

        self.score_function = score_function
        self.patience = patience
        self.trainer = trainer
        self.counter = 0
        self.best_score = None
        self._logger = logging.getLogger(__name__ + "." + self.__class__.__name__)
        self._logger.addHandler(logging.NullHandler()) 
Example #11
Source File: test_jobq.py    From pykit with MIT License 6 votes vote down vote up
def test_exception(self):

        # Add a handler, or python complains "no handler assigned
        # to...."
        jl = logging.getLogger('pykit.jobq')
        jl.addHandler(logging.NullHandler())

        def err_on_even(args):
            if args % 2 == 0:
                raise Exception('even number')
            else:
                return args

        def collect(args):
            rst.append(args)

        rst = []
        jobq.run(range(10), [err_on_even, collect])
        self.assertEqual(list(range(1, 10, 2)), rst)

        # remove NullHandler
        jl.handlers = [] 
Example #12
Source File: log.py    From teleport with Apache License 2.0 6 votes vote down vote up
def format_ldap_message(message, prefix):
    if isinstance(message, LDAPMessage):
        try:  # pyasn1 prettyprint raises exception in version 0.4.3
            formatted = message.prettyPrint().split('\n')  # pyasn1 pretty print
        except Exception as e:
            formatted = ['pyasn1 exception', str(e)]
    else:
        formatted = pformat(message).split('\n')

    prefixed = ''
    for line in formatted:
        if line:
            if _hide_sensitive_data and line.strip().lower().startswith(_sensitive_lines):  # _sensitive_lines is a tuple. startswith() method checks each tuple element
                tag, _, data = line.partition('=')
                if data.startswith("b'") and data.endswith("'") or data.startswith('b"') and data.endswith('"'):
                    prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % (len(data) - 3, )
                else:
                    prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % len(data)
            else:
                prefixed += '\n' + prefix + line
    return prefixed

# sets a logger for the library with NullHandler. It can be used by the application with its own logging configuration 
Example #13
Source File: logger.py    From addon-check with GNU General Public License v3.0 6 votes vote down vote up
def create_logger(debug_filename, logger_name, enabled=False):
        """Creates a logger format for error logging
        :debug_filename: path and filename of the debug log
        :logger_name: name of the logger to create
        :enabled: enable to write the log to provided filename, otherwise uses a NullHandler
        """
        logger = logging.getLogger(logger_name)
        logger.setLevel(logging.DEBUG)
        logger.propagate = False
        formatter = logging.Formatter(fmt='%(asctime)s %(levelname)s:%(name)s:%(funcName)s: %(message)s',
                                      datefmt='%Y-%m-%d %H:%M:%S')

        if enabled:
            # DEBUG log to 'kodi-addon-checker.log'
            debug_log_handler = logging.handlers.RotatingFileHandler(debug_filename, encoding='utf-8', mode="w")
            debug_log_handler.setLevel(logging.DEBUG)
            debug_log_handler.setFormatter(formatter)
            logger.addHandler(debug_log_handler)
        else:
            logger.addHandler(logging.NullHandler())

        return logger 
Example #14
Source File: log.py    From teleport with Apache License 2.0 6 votes vote down vote up
def format_ldap_message(message, prefix):
    if isinstance(message, LDAPMessage):
        try:  # pyasn1 prettyprint raises exception in version 0.4.3
            formatted = message.prettyPrint().split('\n')  # pyasn1 pretty print
        except Exception as e:
            formatted = ['pyasn1 exception', str(e)]
    else:
        formatted = pformat(message).split('\n')

    prefixed = ''
    for line in formatted:
        if line:
            if _hide_sensitive_data and line.strip().lower().startswith(_sensitive_lines):  # _sensitive_lines is a tuple. startswith() method checks each tuple element
                tag, _, data = line.partition('=')
                if data.startswith("b'") and data.endswith("'") or data.startswith('b"') and data.endswith('"'):
                    prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % (len(data) - 3, )
                else:
                    prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % len(data)
            else:
                prefixed += '\n' + prefix + line
    return prefixed

# sets a logger for the library with NullHandler. It can be used by the application with its own logging configuration 
Example #15
Source File: utils.py    From python-zhmcclient with Apache License 2.0 6 votes vote down vote up
def setup_logger(log_comp, handler, level):
    """
    Setup the logger for the specified log component to add the specified
    handler (removing a possibly present NullHandler) and to set it to the
    specified log level. The handler is also set to the specified log level
    because the default level of a handler is 0 which causes it to process all
    levels.
    """

    name = LOGGER_NAMES[log_comp]
    logger = logging.getLogger(name)

    for h in logger.handlers:
        if isinstance(h, logging.NullHandler):
            logger.removeHandler(h)

    handler.setLevel(level)
    logger.addHandler(handler)
    logger.setLevel(level) 
Example #16
Source File: log.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _get_handler(settings):
    """ Return a log handler object according to settings """
    filename = settings.get('LOG_FILE')
    if filename:
        encoding = settings.get('LOG_ENCODING')
        handler = logging.FileHandler(filename, encoding=encoding)
    elif settings.getbool('LOG_ENABLED'):
        handler = logging.StreamHandler()
    else:
        handler = logging.NullHandler()

    formatter = logging.Formatter(
        fmt=settings.get('LOG_FORMAT'),
        datefmt=settings.get('LOG_DATEFORMAT')
    )
    handler.setFormatter(formatter)
    handler.setLevel(settings.get('LOG_LEVEL'))
    if settings.getbool('LOG_SHORT_NAMES'):
        handler.addFilter(TopLevelFormatter(['scrapy']))
    return handler 
Example #17
Source File: plugin.py    From testinfra with Apache License 2.0 6 votes vote down vote up
def pytest_configure(config):
    if config.option.verbose > 1:
        root = logging.getLogger()
        if not root.handlers:
            root.addHandler(logging.NullHandler())
        logging.getLogger("testinfra").setLevel(logging.DEBUG)
    if config.option.nagios:
        # disable & re-enable terminalreporter to write in a tempfile
        reporter = config.pluginmanager.getplugin('terminalreporter')
        if reporter:
            out = SpooledTemporaryFile(encoding=sys.stdout.encoding)
            config.pluginmanager.unregister(reporter)
            reporter = reporter.__class__(config, out)
            config.pluginmanager.register(reporter, 'terminalreporter')
            config.pluginmanager.register(NagiosReporter(out),
                                          'nagiosreporter') 
Example #18
Source File: log.py    From teleport with Apache License 2.0 6 votes vote down vote up
def format_ldap_message(message, prefix):
    if isinstance(message, LDAPMessage):
        try:  # pyasn1 prettyprint raises exception in version 0.4.3
            formatted = message.prettyPrint().split('\n')  # pyasn1 pretty print
        except Exception as e:
            formatted = ['pyasn1 exception', str(e)]
    else:
        formatted = pformat(message).split('\n')

    prefixed = ''
    for line in formatted:
        if line:
            if _hide_sensitive_data and line.strip().lower().startswith(_sensitive_lines):  # _sensitive_lines is a tuple. startswith() method checks each tuple element
                tag, _, data = line.partition('=')
                if data.startswith("b'") and data.endswith("'") or data.startswith('b"') and data.endswith('"'):
                    prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % (len(data) - 3, )
                else:
                    prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % len(data)
            else:
                prefixed += '\n' + prefix + line
    return prefixed

# sets a logger for the library with NullHandler. It can be used by the application with its own logging configuration 
Example #19
Source File: log.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _get_handler(settings):
    """ Return a log handler object according to settings """
    filename = settings.get('LOG_FILE')
    if filename:
        encoding = settings.get('LOG_ENCODING')
        handler = logging.FileHandler(filename, encoding=encoding)
    elif settings.getbool('LOG_ENABLED'):
        handler = logging.StreamHandler()
    else:
        handler = logging.NullHandler()

    formatter = logging.Formatter(
        fmt=settings.get('LOG_FORMAT'),
        datefmt=settings.get('LOG_DATEFORMAT')
    )
    handler.setFormatter(formatter)
    handler.setLevel(settings.get('LOG_LEVEL'))
    if settings.getbool('LOG_SHORT_NAMES'):
        handler.addFilter(TopLevelFormatter(['scrapy']))
    return handler 
Example #20
Source File: ebcLib.py    From SMBetray with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, client, clientRequestQueue, connectionInfo):
		logging.getLogger(__name__).addHandler(logging.NullHandler())
		self.logger = logging.getLogger(__name__)
		self.client = client
		self.clientRequestQueue = clientRequestQueue
		self.connInfo = connectionInfo
		super(ASyncReciever, self).__init__() 
Example #21
Source File: app.py    From python-dockerflow with Mozilla Public License 2.0 5 votes vote down vote up
def __init__(
        self,
        app=None,
        redis=None,
        silenced_checks=None,
        version_path=".",
        *args,
        **kwargs
    ):

        # The Dockerflow specific logger to be used by internals of this
        # extension.
        self.logger = logging.getLogger("dockerflow.sanic")
        self.logger.addHandler(logging.NullHandler())
        self.logger.setLevel(logging.INFO)

        # The request summary logger to be used by this extension
        # without pre-configuration. See docs for how to set it up.
        self.summary_logger = logging.getLogger("request.summary")

        # An ordered dictionary for storing custom Dockerflow checks in.
        self.checks = OrderedDict()

        # A list of IDs of custom Dockerflow checks to ignore in case they
        # show up.
        self.silenced_checks = silenced_checks or []

        # The path where to find the version JSON file. Defaults to the
        # parent directory of the app root path.
        self.version_path = version_path
        self._version_callback = version.get_version

        # Initialize the app if given.
        if app:
            self.init_app(app)
        # Initialize the built-in checks.
        if redis is not None:
            self.init_check(checks.check_redis_connected, redis) 
Example #22
Source File: ebcLib.py    From SMBetray with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, client, serverResponseQueue, connectionInfo):
		logging.getLogger(__name__).addHandler(logging.NullHandler())
		self.logger = logging.getLogger(__name__)
		self.client = client
		self.serverResponseQueue = serverResponseQueue
		self.connInfo = connectionInfo
		super(ASyncSender, self).__init__() 
Example #23
Source File: test_multiprocessing_logging.py    From multiprocessing-logging with GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.handler = logging.NullHandler()
        self.logger = logging.Logger('test-logger')
        self.logger.addHandler(self.handler) 
Example #24
Source File: run_tests_helper.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def SetLogLevel(verbose_count, add_handler=True):
  """Sets log level as |verbose_count|.

  Args:
    verbose_count: Verbosity level.
    add_handler: If true, adds a handler with |CustomFormatter|.
  """
  logging_common.InitializeLogging(
      _WrappedLoggingArgs(verbose_count),
      handler=None if add_handler else logging.NullHandler()) 
Example #25
Source File: logger.py    From levis with MIT License 5 votes vote down vote up
def __init__(self, config={}):
        super(PopulationLoggingGA, self).__init__(config)
        self.log_pop = self.config.setdefault("log_population", False)
        self.population_logger = logging.getLogger("levis.population")
        self.population_logger.setLevel(logging.INFO)
        self.population_logger.addHandler(logging.NullHandler())

        if "population_file" in self.config:
            self.log_pop = True
            fhpop = logging.FileHandler(self.config["population_file"])
            logging.getLogger("levis.population").addHandler(fhpop) 
Example #26
Source File: logger.py    From levis with MIT License 5 votes vote down vote up
def __init__(self, config={}):
        super(FitnessLoggingGA, self).__init__(config)

        self.stats_frequency = self.config.setdefault("stats_frequency", 1.0)

        self.log_fitness = self.config.setdefault("log_fitness", True)
        self.stats_logger = logging.getLogger("levis.stats")
        self.stats_logger.setLevel(logging.INFO)
        self.stats_logger.addHandler(logging.NullHandler())

        if "stats_file" in self.config:
            self.log_fitness = True
            fhstats = logging.FileHandler(self.config["stats_file"])
            logging.getLogger("levis.stats").addHandler(fhstats) 
Example #27
Source File: logger.py    From char-rnn-text-generation with MIT License 5 votes vote down vote up
def get_logger(name, log_path=os.path.join(os.path.dirname(__file__), "main.log"), console=False):
    """
    Simple logging wrapper that returns logger
    configured to log into file and console.

    Args:
        name (str): name of logger
        log_path (str): path of log file
        console (bool): whether to log on console

    Returns:
        logging.Logger: configured logger
    """
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    # ensure that logging handlers are not duplicated
    for handler in list(logger.handlers):
        logger.removeHandler(handler)

    # rotating file handler
    if log_path:
        fh = RotatingFileHandler(log_path,
                                 maxBytes=2 ** 20,  # 1 MB
                                 backupCount=1)  # 1 backup
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    # console handler
    if console:
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        ch.setFormatter(formatter)
        logger.addHandler(ch)

    if len(logger.handlers) == 0:
        logger.addHandler(logging.NullHandler())

    return logger 
Example #28
Source File: finder.py    From influxgraph with Apache License 2.0 5 votes vote down vote up
def _setup_logger(self, level, log_file):
        """Setup log level and log file if set"""
        if not level:
            return
        if logger.handlers:
            return
        if hasattr(logging, 'NullHandler'):
            logger.addHandler(logging.NullHandler())
        formatter = logging.Formatter(
            '[%(levelname)s] %(asctime)s - %(module)s.%(funcName)s() '
            '- %(message)s')
        level = getattr(logging, level.upper())
        logger.setLevel(level)
        handler = logging.StreamHandler()
        logger.addHandler(handler)
        handler.setFormatter(formatter)
        if not log_file:
            return
        try:
            _handler = WatchedFileHandler(log_file)
        except IOError:
            logger.error("Could not write to %s, falling back to stdout",
                         log_file)
        else:
            logger.addHandler(_handler)
            _handler.setFormatter(formatter) 
Example #29
Source File: ext_logging.py    From deepWordBug with Apache License 2.0 5 votes vote down vote up
def _setup_file_log(self):
        """Add a file log handler."""

        namespace = self._meta.namespace
        file_path = self.app.config.get(self._meta.config_section, 'file')
        rotate = self.app.config.get(self._meta.config_section, 'rotate')
        max_bytes = self.app.config.get(self._meta.config_section,
                                        'max_bytes')
        max_files = self.app.config.get(self._meta.config_section,
                                        'max_files')
        if file_path:
            file_path = fs.abspath(file_path)
            log_dir = os.path.dirname(file_path)
            if not os.path.exists(log_dir):
                os.makedirs(log_dir)

            if rotate:
                from logging.handlers import RotatingFileHandler
                file_handler = RotatingFileHandler(
                    file_path,
                    maxBytes=int(max_bytes),
                    backupCount=int(max_files),
                )
            else:
                from logging import FileHandler
                file_handler = FileHandler(file_path)

            format = self._get_file_format()
            formatter = self._get_file_formatter(format)
            file_handler.setFormatter(formatter)
            file_handler.setLevel(getattr(logging, self.get_level()))
        else:
            file_handler = NullHandler()

        # FIXME: self._clear_loggers() should be preventing this but its not!
        for i in logging.getLogger("cement:app:%s" % namespace).handlers:
            if isinstance(i, file_handler.__class__):   # pragma: nocover
                self.backend.removeHandler(i)           # pragma: nocover

        self.backend.addHandler(file_handler) 
Example #30
Source File: logger.py    From phonemizer with GNU General Public License v3.0 5 votes vote down vote up
def get_logger(verbosity='normal'):
    """Returns a configured logging.Logger instance

    The logger is configured to output messages on the standard error stream
    (stderr).

    Parameters
    ----------
    verbosity (str) : The level of verbosity, must be 'verbose' (displays
      debug/info and warning messages), 'normal' (warnings only) or 'quiet' (do
      not display anything).

    Raises
    ------
    RuntimeError if `verbosity` is not 'normal', 'verbose', or 'quiet'.

    """
    # make sure the verbosity argument is valid
    valid_verbosity = ['normal', 'verbose', 'quiet']
    if verbosity not in valid_verbosity:
        raise RuntimeError(
            f'verbosity is {verbosity} but must be in '
            f'{", ".join(valid_verbosity)}')

    logger = logging.getLogger()

    # setup output to stderr
    logger.handlers = []
    handler = logging.StreamHandler(sys.stderr)

    # setup verbosity level
    logger.setLevel(logging.WARNING)
    if verbosity == 'verbose':
        logger.setLevel(logging.DEBUG)
    elif verbosity == 'quiet':
        handler = logging.NullHandler()

    # setup messages format
    handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
    logger.addHandler(handler)
    return logger