Python logging.CRITICAL() Examples

The following are code examples for showing how to use logging.CRITICAL(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pygreynoise   Author: GreyNoise-Intelligence   File: util.py    MIT License 7 votes vote down vote up
def configure_logging():
    """Configure logging."""
    logging.basicConfig(stream=sys.stderr, format="%(message)s", level=logging.CRITICAL)
    logging.getLogger("greynoise").setLevel(logging.WARNING)
    structlog.configure(
        processors=[
            structlog.stdlib.add_logger_name,
            structlog.stdlib.add_log_level,
            structlog.stdlib.PositionalArgumentsFormatter(),
            structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M.%S"),
            structlog.processors.StackInfoRenderer(),
            structlog.processors.format_exc_info,
            structlog.dev.ConsoleRenderer(),
        ],
        context_class=dict,
        logger_factory=structlog.stdlib.LoggerFactory(),
        wrapper_class=structlog.stdlib.BoundLogger,
        cache_logger_on_first_use=True,
    ) 
Example 2
Project: pyblish-win   Author: pyblish   File: test_logging.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_nested_explicit(self):
        # Logging levels in a nested namespace, all explicitly set.
        m = self.next_message

        INF = logging.getLogger("INF")
        INF.setLevel(logging.INFO)
        INF_ERR  = logging.getLogger("INF.ERR")
        INF_ERR.setLevel(logging.ERROR)

        # These should log.
        INF_ERR.log(logging.CRITICAL, m())
        INF_ERR.error(m())

        # These should not log.
        INF_ERR.warn(m())
        INF_ERR.info(m())
        INF_ERR.debug(m())

        self.assert_log_lines([
            ('INF.ERR', 'CRITICAL', '1'),
            ('INF.ERR', 'ERROR', '2'),
        ]) 
Example 3
Project: Tyr   Author: hudl   File: server.py    The Unlicense 6 votes vote down vote up
def establish_logger(self):

        try:
            return self.log
        except:
            pass

        log = logging.getLogger('Tyr.{c}'
                                .format(c=self.__class__.__name__))
        log.setLevel(logging.DEBUG)
        self.log = log

        if not log.handlers:
            # Configure a root logger
            logging.basicConfig(level=logging.INFO,
                                format='%(asctime)s [%(name)s]'
                                ' %(levelname)s: %(message)s',
                                datefmt='%H:%M:%S')
            # Reduce boto logging
            logging.getLogger('boto').setLevel(logging.CRITICAL) 
Example 4
Project: web-scraper   Author: keeper-of-data   File: log.py    MIT License 6 votes vote down vote up
def setup_custom_logger(name, log_file):

    # Create logger
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    # Create file handler which logs error messages
    fh = logging.FileHandler(log_file)
    fh.setLevel(logging.ERROR)
    # Create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL)
    # Create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # Add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)

    return logger 
Example 5
Project: jawfish   Author: war-and-code   File: handlers.py    MIT License 6 votes vote down vote up
def __init__(self, appname, dllname=None, logtype="Application"):
        logging.Handler.__init__(self)
        try:
            import win32evtlogutil, win32evtlog
            self.appname = appname
            self._welu = win32evtlogutil
            if not dllname:
                dllname = os.path.split(self._welu.__file__)
                dllname = os.path.split(dllname[0])
                dllname = os.path.join(dllname[0], r'win32service.pyd')
            self.dllname = dllname
            self.logtype = logtype
            self._welu.AddSourceToRegistry(appname, dllname, logtype)
            self.deftype = win32evtlog.EVENTLOG_ERROR_TYPE
            self.typemap = {
                logging.DEBUG   : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.INFO    : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.WARNING : win32evtlog.EVENTLOG_WARNING_TYPE,
                logging.ERROR   : win32evtlog.EVENTLOG_ERROR_TYPE,
                logging.CRITICAL: win32evtlog.EVENTLOG_ERROR_TYPE,
         }
        except ImportError:
            print("The Python Win32 extensions for NT (service, event "\
                        "logging) appear not to be available.")
            self._welu = None 
Example 6
Project: dataflow   Author: tensorpack   File: logger.py    Apache License 2.0 6 votes vote down vote up
def format(self, record):
        date = colored('[%(asctime)s @%(filename)s:%(lineno)d]', 'green')
        msg = '%(message)s'
        if record.levelno == logging.WARNING:
            fmt = date + ' ' + colored('WRN', 'red', attrs=['blink']) + ' ' + msg
        elif record.levelno == logging.ERROR or record.levelno == logging.CRITICAL:
            fmt = date + ' ' + colored('ERR', 'red', attrs=['blink', 'underline']) + ' ' + msg
        elif record.levelno == logging.DEBUG:
            fmt = date + ' ' + colored('DBG', 'yellow', attrs=['blink']) + ' ' + msg
        else:
            fmt = date + ' ' + msg
        if hasattr(self, '_style'):
            # Python3 compatibility
            self._style._fmt = fmt
        self._fmt = fmt
        return super(_MyFormatter, self).format(record) 
Example 7
Project: NiujiaoDebugger   Author: MrSrc   File: handlers.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, appname, dllname=None, logtype="Application"):
        logging.Handler.__init__(self)
        try:
            import win32evtlogutil, win32evtlog
            self.appname = appname
            self._welu = win32evtlogutil
            if not dllname:
                dllname = os.path.split(self._welu.__file__)
                dllname = os.path.split(dllname[0])
                dllname = os.path.join(dllname[0], r'win32service.pyd')
            self.dllname = dllname
            self.logtype = logtype
            self._welu.AddSourceToRegistry(appname, dllname, logtype)
            self.deftype = win32evtlog.EVENTLOG_ERROR_TYPE
            self.typemap = {
                logging.DEBUG   : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.INFO    : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.WARNING : win32evtlog.EVENTLOG_WARNING_TYPE,
                logging.ERROR   : win32evtlog.EVENTLOG_ERROR_TYPE,
                logging.CRITICAL: win32evtlog.EVENTLOG_ERROR_TYPE,
         }
        except ImportError:
            print("The Python Win32 extensions for NT (service, event "\
                        "logging) appear not to be available.")
            self._welu = None 
Example 8
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 6 votes vote down vote up
def test_nested_explicit(self):
        # Logging levels in a nested namespace, all explicitly set.
        m = self.next_message

        INF = logging.getLogger("INF")
        INF.setLevel(logging.INFO)
        INF_ERR  = logging.getLogger("INF.ERR")
        INF_ERR.setLevel(logging.ERROR)

        # These should log.
        INF_ERR.log(logging.CRITICAL, m())
        INF_ERR.error(m())

        # These should not log.
        INF_ERR.warning(m())
        INF_ERR.info(m())
        INF_ERR.debug(m())

        self.assert_log_lines([
            ('INF.ERR', 'CRITICAL', '1'),
            ('INF.ERR', 'ERROR', '2'),
        ]) 
Example 9
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 6 votes vote down vote up
def test_nested_with_virtual_parent(self):
        # Logging levels when some parent does not exist yet.
        m = self.next_message

        INF = logging.getLogger("INF")
        GRANDCHILD = logging.getLogger("INF.BADPARENT.UNDEF")
        CHILD = logging.getLogger("INF.BADPARENT")
        INF.setLevel(logging.INFO)

        # These should log.
        GRANDCHILD.log(logging.FATAL, m())
        GRANDCHILD.info(m())
        CHILD.log(logging.FATAL, m())
        CHILD.info(m())

        # These should not log.
        GRANDCHILD.debug(m())
        CHILD.debug(m())

        self.assert_log_lines([
            ('INF.BADPARENT.UNDEF', 'CRITICAL', '1'),
            ('INF.BADPARENT.UNDEF', 'INFO', '2'),
            ('INF.BADPARENT', 'CRITICAL', '3'),
            ('INF.BADPARENT', 'INFO', '4'),
        ]) 
Example 10
Project: modelforge   Author: src-d   File: slogging.py    Apache License 2.0 6 votes vote down vote up
def formatMessage(self, record: logging.LogRecord) -> str:
        """Convert the already filled log record to a string."""
        level_color = "0"
        text_color = "0"
        fmt = ""
        if record.levelno <= logging.DEBUG:
            fmt = "\033[0;37m" + logging.BASIC_FORMAT + "\033[0m"
        elif record.levelno <= logging.INFO:
            level_color = "1;36"
            lmsg = record.message.lower()
            if self.GREEN_RE.search(lmsg):
                text_color = "1;32"
        elif record.levelno <= logging.WARNING:
            level_color = "1;33"
        elif record.levelno <= logging.CRITICAL:
            level_color = "1;31"
        if not fmt:
            fmt = "\033[" + level_color + \
                  "m%(levelname)s\033[0m:%(rthread)s:%(name)s:\033[" + text_color + \
                  "m%(message)s\033[0m"
        fmt = _fest + fmt
        record.rthread = reduce_thread_id(record.thread)
        return fmt % record.__dict__ 
Example 11
Project: trove-dashboard   Author: openstack   File: tests.py    Apache License 2.0 6 votes vote down vote up
def test_launch_instance_exception_on_flavors(self):
        trove_exception = self.exceptions.nova
        self.mock_flavor_list.side_effect = trove_exception

        toSuppress = ["trove_dashboard.content.databases."
                      "workflows.create_instance",
                      "horizon.workflows.base"]

        # Suppress expected log messages in the test output
        loggers = []
        for cls in toSuppress:
            logger = logging.getLogger(cls)
            loggers.append((logger, logger.getEffectiveLevel()))
            logger.setLevel(logging.CRITICAL)

        try:
            with self.assertRaises(exceptions.Http302):
                self.client.get(LAUNCH_URL)
                self.mock_datastore_flavors.assert_called_once_with(
                    test.IsHttpRequest(), mock.ANY, mock.ANY)

        finally:
            # Restore the previous log levels
            for (log, level) in loggers:
                log.setLevel(level) 
Example 12
Project: wp1   Author: openzim   File: project.py    GNU General Public License v2.0 6 votes vote down vote up
def update_project_by_name(project_name):
  wp10db = wp10_connect()
  wikidb = wiki_connect()

  logging.basicConfig(level=logging.INFO)
  logging.getLogger('mwclient').setLevel(logging.CRITICAL)
  logging.getLogger('urllib3').setLevel(logging.CRITICAL)
  logging.getLogger('requests_oauthlib').setLevel(logging.CRITICAL)
  logging.getLogger('oauthlib').setLevel(logging.CRITICAL)

  try:
    project = get_project_by_name(wp10db, project_name)
    if not project:
      logger.error('No project with name: %s', project_name)
      return
    update_project(wikidb, wp10db, project)
  finally:
    wp10db.close()
    wikidb.close() 
Example 13
Project: knack   Author: microsoft   File: log.py    MIT License 6 votes vote down vote up
def get_color_wrapper(cls, level):
        if not cls.COLOR_MAP:
            import colorama

            def _color_wrapper(color_marker):
                def wrap_msg_with_color(msg):
                    return '{}{}{}'.format(color_marker, msg, colorama.Style.RESET_ALL)
                return wrap_msg_with_color

            cls.COLOR_MAP = {
                logging.CRITICAL: _color_wrapper(colorama.Fore.LIGHTRED_EX),
                logging.ERROR: _color_wrapper(colorama.Fore.LIGHTRED_EX),
                logging.WARNING: _color_wrapper(colorama.Fore.YELLOW),
                logging.INFO: _color_wrapper(colorama.Fore.GREEN),
                logging.DEBUG: _color_wrapper(colorama.Fore.CYAN)
            }

        return cls.COLOR_MAP.get(level, None) 
Example 14
Project: knack   Author: microsoft   File: log.py    MIT License 6 votes vote down vote up
def _get_console_log_configs():
        return [
            # (default)
            {
                CLI_LOGGER_NAME: logging.WARNING,
                'root': logging.CRITICAL,
            },
            # --verbose
            {
                CLI_LOGGER_NAME: logging.INFO,
                'root': logging.CRITICAL,
            },
            # --debug
            {
                CLI_LOGGER_NAME: logging.DEBUG,
                'root': logging.DEBUG,
            }] 
Example 15
Project: cct   Author: awacha   File: mainwindow.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def writelogline(self, message: str, record: logging.LogRecord):
        assert hasattr(record, 'message')
        if record.levelno >= logging.CRITICAL:
            tag = self._logtags.lookup('critical')
        elif record.levelno >= logging.ERROR:
            tag = self._logtags.lookup('error')
        elif record.levelno >= logging.WARNING:
            tag = self._logtags.lookup('warning')
        else:
            tag = self._logtags.lookup('normal')
        enditer = self._logbuffer.get_end_iter()
        self._logbuffer.insert_with_tags(enditer, message + '\n', tag)
        self._logview.scroll_to_mark(
            self._logbuffer.get_mark('log_end'), 0.1, False, 0, 0)
        if record.levelno >= logging.INFO:
            self.builder.get_object('statusbar').pop(0)
            self.builder.get_object('statusbar').push(0, record.message.split('\n')[0])
        self._logview2.add_logentry(record)
        return False 
Example 16
Project: cct   Author: awacha   File: logviewer_text.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def emit(self, record: logging.LogRecord):
        msg = self.format(record) + '\n'
        cursor = self.plainTextEdit.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)

        if record.levelno < logging.INFO:
            cursor.insertText(msg, self.debugformat)
        elif record.levelno < logging.WARNING:
            cursor.insertText(msg, self.infoformat)
        elif record.levelno < logging.ERROR:
            cursor.insertText(msg, self.warningformat)
        elif record.levelno < logging.CRITICAL:
            cursor.insertText(msg, self.errorformat)
        else:
            cursor.insertText(msg, self.criticalformat)
        cursor.movePosition(QtGui.QTextCursor.End)
        self.plainTextEdit.setTextCursor(cursor)
        self.plainTextEdit.ensureCursorVisible() 
Example 17
Project: rizza   Author: JacobCallahan   File: logger.py    GNU General Public License v3.0 6 votes vote down vote up
def setup_logzero(path, level):
    Path(path).parent.mkdir(parents=True, exist_ok=True)
    log_fmt = '%(color)s[%(levelname)s %(asctime)s]%(end_color)s %(message)s'
    if level == 'debug':
        level = logging.DEBUG
        log_fmt = (
            '%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]'
            '%(end_color)s %(message)s')
    elif level == 'info':
        level = logging.INFO
    elif level == 'warning':
        level = logging.WARNING
    elif level == 'error':
        level = logging.ERROR
    elif level == 'critical':
        level = logging.CRITICAL

    formatter = logzero.LogFormatter(fmt=log_fmt)
    logzero.setup_default_logger(formatter=formatter)
    logzero.loglevel(level)
    logzero.logfile(
        path, loglevel=level, maxBytes=1e9, backupCount=3, formatter=formatter) 
Example 18
Project: codimension   Author: SergeySatskiy   File: cdmpluginbase.py    GNU General Public License v3.0 6 votes vote down vote up
def sendLogMessage(self, level, msg, *args):
        """Sends a log message asynchronously.

        The method could be used safely from a non-GUI thread.

        level => integer, one of those found in logging:
                          logging.CRITICAL
                          logging.ERROR
                          logging.WARNING
                          logging.INFO
                          logging.DEBUG
        msg => message
        args => message arguments to be substituted (mgs % args)
        """
        try:
            self.__parent.pluginLogMessage.emit(level, msg % args)
        except Exception as exc:
            self.__parent.pluginLogMessage.emit(
                logging.ERROR,
                "Error sending a plugin log message. Error: " + str(exc)) 
Example 19
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, appname, dllname=None, logtype="Application"):
        logging.Handler.__init__(self)
        try:
            import win32evtlogutil, win32evtlog
            self.appname = appname
            self._welu = win32evtlogutil
            if not dllname:
                dllname = os.path.split(self._welu.__file__)
                dllname = os.path.split(dllname[0])
                dllname = os.path.join(dllname[0], r'win32service.pyd')
            self.dllname = dllname
            self.logtype = logtype
            self._welu.AddSourceToRegistry(appname, dllname, logtype)
            self.deftype = win32evtlog.EVENTLOG_ERROR_TYPE
            self.typemap = {
                logging.DEBUG   : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.INFO    : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.WARNING : win32evtlog.EVENTLOG_WARNING_TYPE,
                logging.ERROR   : win32evtlog.EVENTLOG_ERROR_TYPE,
                logging.CRITICAL: win32evtlog.EVENTLOG_ERROR_TYPE,
         }
        except ImportError:
            print("The Python Win32 extensions for NT (service, event "\
                        "logging) appear not to be available.")
            self._welu = None 
Example 20
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def getEventType(self, record):
        """
        Return the event type for the record.

        Override this if you want to specify your own types. This version does
        a mapping using the handler's typemap attribute, which is set up in
        __init__() to a dictionary which contains mappings for DEBUG, INFO,
        WARNING, ERROR and CRITICAL. If you are using your own levels you will
        either need to override this method or place a suitable dictionary in
        the handler's typemap attribute.
        """
        return self.typemap.get(record.levelno, self.deftype) 
Example 21
Project: pyblish-win   Author: pyblish   File: test_logging.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nested_inherited(self):
        #Logging levels in a nested namespace, inherited from parent loggers.
        m = self.next_message

        INF = logging.getLogger("INF")
        INF.setLevel(logging.INFO)
        INF_ERR  = logging.getLogger("INF.ERR")
        INF_ERR.setLevel(logging.ERROR)
        INF_UNDEF = logging.getLogger("INF.UNDEF")
        INF_ERR_UNDEF = logging.getLogger("INF.ERR.UNDEF")
        UNDEF = logging.getLogger("UNDEF")

        # These should log.
        INF_UNDEF.log(logging.CRITICAL, m())
        INF_UNDEF.error(m())
        INF_UNDEF.warn(m())
        INF_UNDEF.info(m())
        INF_ERR_UNDEF.log(logging.CRITICAL, m())
        INF_ERR_UNDEF.error(m())

        # These should not log.
        INF_UNDEF.debug(m())
        INF_ERR_UNDEF.warn(m())
        INF_ERR_UNDEF.info(m())
        INF_ERR_UNDEF.debug(m())

        self.assert_log_lines([
            ('INF.UNDEF', 'CRITICAL', '1'),
            ('INF.UNDEF', 'ERROR', '2'),
            ('INF.UNDEF', 'WARNING', '3'),
            ('INF.UNDEF', 'INFO', '4'),
            ('INF.ERR.UNDEF', 'CRITICAL', '5'),
            ('INF.ERR.UNDEF', 'ERROR', '6'),
        ]) 
Example 22
Project: friendly-telegram   Author: friendly-telegram   File: updater.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def restart_common(self, message):
        await self.prerestart_common(message)
        atexit.register(restart)
        [handler] = logging.getLogger().handlers
        handler.setLevel(logging.CRITICAL)
        for client in self.allclients:
            # Terminate main loop of all running clients
            # Won't work if not all clients are ready
            if client is not message.client:
                await client.disconnect()
        await message.client.disconnect() 
Example 23
Project: friendly-telegram   Author: friendly-telegram   File: nocollisions.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def cleanbotscmd(self, message):
        """Kills all userbots except 1, selected according to which is fastest (approx)"""
        try:
            await message.edit("<code>DEADBEEF</code>")
            await asyncio.sleep(5)
            await utils.answer(message, self.strings["killed"])
        except telethon.errors.rpcerrorlist.MessageNotModifiedError:
            [handler] = logging.getLogger().handlers
            handler.setLevel(logging.CRITICAL)
            for client in self.allclients:
                # Terminate main loop of all running clients
                # Won't work if not all clients are ready
                if client is not message.client:
                    await client.disconnect()
            await message.client.disconnect() 
Example 24
Project: flasky   Author: RoseOu   File: log.py    MIT License 5 votes vote down vote up
def critical(self, msg, *args, **kwargs):
        """Delegate a critical call to the underlying logger."""

        self.log(logging.CRITICAL, msg, *args, **kwargs) 
Example 25
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: log.py    Apache License 2.0 5 votes vote down vote up
def _get_label(self, level):
        # pylint: disable= missing-docstring
        if level == logging.CRITICAL:
            return 'C'
        elif level == logging.ERROR:
            return 'E'
        elif level == logging.WARNING:
            return 'W'
        elif level == logging.INFO:
            return 'I'
        elif level == logging.DEBUG:
            return 'D'
        return 'U' 
Example 26
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_utils.py    Apache License 2.0 5 votes vote down vote up
def test_execute_fail_and_logging(self, mock_log):
        mock_conf = mock.MagicMock()
        daemon = utils.RootwrapDaemonHelper(mock_conf)
        daemon.client = mock.MagicMock()
        daemon.client.execute = mock.Mock(return_value=(-2, None, None))

        self.assertRaises(processutils.ProcessExecutionError,
                          daemon.execute, 'b', 2,
                          attempts=2,
                          loglevel=logging.CRITICAL,
                          log_errors=processutils.LOG_ALL_ERRORS)
        mock_log.assert_has_calls(
            [
                mock.call(logging.CRITICAL, u'Running cmd (subprocess): %s',
                          u'b 2'),
                mock.call(logging.CRITICAL,
                          'CMD "%(sanitized_cmd)s" returned: %(return_code)s '
                          'in %(end_time)0.3fs',
                          {'sanitized_cmd': u'b 2', 'return_code': -2,
                           'end_time': mock.ANY}),
                mock.call(logging.CRITICAL,
                          u'%(desc)r\ncommand: %(cmd)r\nexit code: %(code)r'
                          u'\nstdout: %(stdout)r\nstderr: %(stderr)r',
                          {'code': -2, 'cmd': u'b 2', 'stdout': u'None',
                           'stderr': u'None', 'desc': None}),
                mock.call(logging.CRITICAL, u'%r failed. Retrying.', u'b 2'),
                mock.call(logging.CRITICAL, u'Running cmd (subprocess): %s',
                          u'b 2'),
                mock.call(logging.CRITICAL,
                          'CMD "%(sanitized_cmd)s" returned: %(return_code)s '
                          'in %(end_time)0.3fs',
                          {'sanitized_cmd': u'b 2', 'return_code': -2,
                           'end_time': mock.ANY}),
                mock.call(logging.CRITICAL,
                          u'%(desc)r\ncommand: %(cmd)r\nexit code: %(code)r'
                          u'\nstdout: %(stdout)r\nstderr: %(stderr)r',
                          {'code': -2, 'cmd': u'b 2', 'stdout': u'None',
                           'stderr': u'None', 'desc': None}),
                mock.call(logging.CRITICAL, u'%r failed. Not Retrying.',
                          u'b 2')]
        ) 
Example 27
Project: cs294-112_hws   Author: xuwd11   File: logger.py    MIT License 5 votes vote down vote up
def _get_logger(self, name, log_path, lvl=logging.INFO, display_name=None):
        if isinstance(lvl, str):
            lvl = lvl.lower().strip()
            if lvl == 'debug':
                lvl = logging.DEBUG
            elif lvl == 'info':
                lvl = logging.INFO
            elif lvl == 'warn' or lvl == 'warning':
                lvl = logging.WARN
            elif lvl == 'error':
                lvl = logging.ERROR
            elif lvl == 'fatal' or lvl == 'critical':
                lvl = logging.CRITICAL
            else:
                raise ValueError('unknown logging level')

        file_handler = logging.FileHandler(log_path)
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(LoggerClass._normal_formatter)
        console_handler = logging.StreamHandler()
        console_handler.setLevel(lvl)
        console_handler.setFormatter(LoggerClass._color_formatter)
        if display_name is None:
            display_name = name
        logger = logging.getLogger(display_name)
        logger.setLevel(logging.DEBUG)
        logger.addHandler(console_handler)
        logger.addHandler(file_handler)

        return logger

    ###############
    ### Logging ###
    ############### 
Example 28
Project: timeseries-mock   Author: ruivieira   File: app.py    Apache License 2.0 5 votes vote down vote up
def loglevel(level):
    levels = {'CRITICAL': logging.CRITICAL,
              'FATAL': logging.FATAL,
              'ERROR': logging.ERROR,
              'WARNING': logging.WARNING,
              'WARN': logging.WARNING,
              'INFO': logging.INFO,
              'DEBUG': logging.DEBUG,
              'NOTSET': logging.NOTSET}
    return levels[level] 
Example 29
Project: calmjs   Author: calmjs   File: command.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, distutils_log=log):
        logging.Handler.__init__(self)
        self.log = distutils_log
        # Basic numeric table
        self.level_table = {
            logging.CRITICAL: distutils_log.FATAL,
            logging.ERROR: distutils_log.ERROR,
            logging.WARNING: distutils_log.WARN,
            logging.INFO: distutils_log.INFO,
            logging.DEBUG: distutils_log.DEBUG,
        }
        self.setFormatter(logging.Formatter('%(message)s')) 
Example 30
Project: openhatch   Author: campbe13   File: compat.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def critical(self, msg, *args, **kwargs):
        self.log(logging.CRITICAL, msg, *args, **kwargs) 
Example 31
Project: FormulaNet   Author: princeton-vl   File: log.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_label(self, level):
        if level == logging.CRITICAL:
            return 'C'
        elif level == logging.ERROR:
            return 'E'
        elif level == logging.WARNING:
            return 'W'
        elif level == logging.INFO:
            return 'I'
        elif level == logging.DEBUG:
            return 'D'
        else:
            return 'U' 
Example 32
Project: TurboPydDNS   Author: ihipop   File: ddns.py    Apache License 2.0 5 votes vote down vote up
def cli(ctx,**kwargs):
    sys.path.append(os.getcwd())
    # print(kwargs)
    debug = logging.CRITICAL - kwargs['debug'] * 10
    # print(debug)
    logger.setLevel(debug)
    if ctx.invoked_subcommand:
        logger.echo('I am about to invoke %s' % ctx.invoked_subcommand)
    else:
        logger.echo('No SubCommand set.')
    ctx.obj = {}
    return ctx.obj.update(kwargs) 
Example 33
Project: jawfish   Author: war-and-code   File: handlers.py    MIT License 5 votes vote down vote up
def getEventType(self, record):
        """
        Return the event type for the record.

        Override this if you want to specify your own types. This version does
        a mapping using the handler's typemap attribute, which is set up in
        __init__() to a dictionary which contains mappings for DEBUG, INFO,
        WARNING, ERROR and CRITICAL. If you are using your own levels you will
        either need to override this method or place a suitable dictionary in
        the handler's typemap attribute.
        """
        return self.typemap.get(record.levelno, self.deftype) 
Example 34
Project: draco   Author: radiocosmology   File: task.py    MIT License 5 votes vote down vote up
def _log_level(x):
    """Interpret the input as a logging level.

    Parameters
    ----------
    x : int or str
        Explicit integer logging level or one of 'DEBUG', 'INFO', 'WARN',
        'ERROR' or 'CRITICAL'.

    Returns
    -------
    level : int
    """

    level_dict = {
        "DEBUG": logging.DEBUG,
        "INFO": logging.INFO,
        "WARN": logging.WARN,
        "WARNING": logging.WARN,
        "ERROR": logging.ERROR,
        "CRITICAL": logging.CRITICAL,
    }

    if isinstance(x, int):
        return x
    elif isinstance(x, basestring) and x in level_dict:
        return level_dict[x.upper()]
    else:
        raise ValueError("Logging level %s not understood" % repr(x)) 
Example 35
Project: Repobot   Author: Desgard   File: test_application.py    MIT License 5 votes vote down vote up
def test_flatten_flags(self):
        cfg = Config()
        cfg.MyApp.log_level = logging.WARN
        app = MyApp()
        app.update_config(cfg)
        self.assertEqual(app.log_level, logging.WARN)
        self.assertEqual(app.config.MyApp.log_level, logging.WARN)
        app.initialize(["--crit"])
        self.assertEqual(app.log_level, logging.CRITICAL)
        # this would be app.config.Application.log_level if it failed:
        self.assertEqual(app.config.MyApp.log_level, logging.CRITICAL) 
Example 36
Project: Repobot   Author: Desgard   File: test_application.py    MIT License 5 votes vote down vote up
def test_flatten_aliases(self):
        cfg = Config()
        cfg.MyApp.log_level = logging.WARN
        app = MyApp()
        app.update_config(cfg)
        self.assertEqual(app.log_level, logging.WARN)
        self.assertEqual(app.config.MyApp.log_level, logging.WARN)
        app.initialize(["--log-level", "CRITICAL"])
        self.assertEqual(app.log_level, logging.CRITICAL)
        # this would be app.config.Application.log_level if it failed:
        self.assertEqual(app.config.MyApp.log_level, "CRITICAL") 
Example 37
Project: brutemap   Author: brutemap-dev   File: logger.py    GNU General Public License v3.0 5 votes vote down vote up
def colorize(self, msg):
        """
        Mewarnai pesan
        """

        color = self.color_map[self.record.levelno]
        reset = Style.RESET_ALL
        levelname = reset + color + self.record.levelname + reset
        if self.record.levelname == "CRITICAL":
            color = self.color_map[logging.ERROR]

        name = Fore.LIGHTBLUE_EX + self.record.name + reset
        message = self.record.message
        # XXX: kenapa cara dibawah ini tidak bekerja?
        #
        # match = re.findall(r"['\"]+(.*?)['\"]+", message)
        # if match:
        #     match.reverse()
        #     for m in match:
        #         message = message.replace(m, color + m + reset, 1)

        match = re.search(r"=> (?P<account>.*?(?:| \: .*?)) \((?P<status>[A-Z]+)\)", message)
        if match:
            account = match.group("account")
            status = match.group("status")
            if status == "NO":
                color_status = Fore.LIGHTRED_EX
            else:
                color_status = Fore.LIGHTGREEN_EX

            newmsg = message.replace(account, color_status + account + reset)
            newmsg = newmsg.replace(status, color_status + status + reset)
            msg = msg.replace(message, newmsg)

        asctime = re.findall(r"\[(.+?)\]", msg)[0]
        msg = msg.replace(asctime, Fore.LIGHTMAGENTA_EX + asctime + reset, 1)
        msg = msg.replace(self.record.name, name, 1)
        msg = msg.replace(self.record.levelname, levelname, 1)
        msg = msg + reset

        return msg 
Example 38
Project: inmanta   Author: inmanta   File: handler.py    Apache License 2.0 5 votes vote down vote up
def critical(self, msg: str, *args, **kwargs) -> None:
        """
        Log 'msg % args' with severity 'CRITICAL'.

        To pass exception information, use the keyword argument exc_info with
        a true value, e.g.

        ``logger.critical("Houston, we have a %s", "major disaster", exc_info=1)``
        """
        self.log_msg(logging.CRITICAL, msg, args, kwargs) 
Example 39
Project: rift-python   Author: brunorijsman   File: test_node_flood_repeater_election.py    Apache License 2.0 5 votes vote down vote up
def make_test_node(parents, additional_node_config=None):
    test_engine = engine.Engine(
        passive_nodes=[],
        run_which_nodes=[],
        interactive=False,
        telnet_port_file=None,
        ipv4_multicast_loopback=False,
        ipv6_multicast_loopback=False,
        log_level=logging.CRITICAL,
        config={}
    )
    test_engine.floodred_system_random = 11111111111111111111    # Make unit test deterministic
    node_config = {
        "name": "node" + str(NODE_SYSID),
        "systemid": NODE_SYSID,
        "level": NODE_LEVEL,
        "skip-self-orginated-ties": True  # The test is in control of what TIEs are in the DB
    }
    if additional_node_config:
        node_config.update(additional_node_config)
    test_node = node.Node(node_config, test_engine)
    # Create fake interfaces for parents (in state 3-way)
    for parent_sysid in parents.keys():
        make_parent_interface(test_node, parent_sysid)
    # Fill TIE-DB for the first time
    update_test_node(test_node, parents)
    return test_node 
Example 40
Project: rift-python   Author: brunorijsman   File: __main__.py    Apache License 2.0 5 votes vote down vote up
def log_level(string):
    string = string.lower()
    if string == 'critical':
        return logging.CRITICAL
    elif string == 'error':
        return logging.ERROR
    elif string == 'warning':
        return logging.WARNING
    elif string == 'info':
        return logging.INFO
    elif string == 'debug':
        return logging.DEBUG
    else:
        msg = "{} is not a valid log level".format(string)
        raise argparse.ArgumentTypeError(msg) 
Example 41
Project: misp42splunk   Author: remg427   File: log.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def set_log_level(log_level):
    """
    Set log level.
    """

    if isinstance(log_level, basestring):
        if log_level.upper() == "DEBUG":
            stclog.Logs().set_level(logging.DEBUG)
        elif log_level.upper() == "INFO":
            stclog.Logs().set_level(logging.INFO)
        elif log_level.upper() == "WARN":
            stclog.Logs().set_level(logging.WARN)
        elif log_level.upper() == "ERROR":
            stclog.Logs().set_level(logging.ERROR)
        elif log_level.upper() == "WARNING":
            stclog.Logs().set_level(logging.WARNING)
        elif log_level.upper() == "CRITICAL":
            stclog.Logs().set_level(logging.CRITICAL)
        else:
            stclog.Logs().set_level(logging.INFO)
    elif isinstance(log_level, int):
        if log_level in [logging.DEBUG, logging.INFO, logging.ERROR,
                         logging.WARN, logging.WARNING, logging.CRITICAL]:
            stclog.Logs().set_level(log_level)
        else:
            stclog.Logs().set_level(logging.INFO)
    else:
        stclog.Logs().set_level(logging.INFO)


# Global logger 
Example 42
Project: misp42splunk   Author: remg427   File: base_modinput.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def set_log_level(self, level):
        """Set the log level this python process uses.

        :param level: log level in `string`. Accept "DEBUG", "INFO", "WARNING", "ERROR" and "CRITICAL".
        """
        if isinstance(level, basestring):
            level = level.lower()
            if level in self.LogLevelMapping:
                level = self.LogLevelMapping[level]
            else:
                level = logging.INFO
        self.logger.setLevel(level) 
Example 43
Project: misp42splunk   Author: remg427   File: base_modinput.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def log_critical(self, msg):
        """Log msg using logging.CRITICAL level.

        :param msg: log `string`
        """
        self.logger.critical(msg) 
Example 44
Project: pyplis   Author: jgliss   File: _init_utils.py    GNU General Public License v3.0 5 votes vote down vote up
def _get_loglevels():
    return dict(critical=logging.CRITICAL,
                exception=logging.ERROR,
                error=logging.ERROR,
                warn=logging.WARNING,
                warning=logging.WARNING,
                info=logging.INFO,
                debug=logging.DEBUG) 
Example 45
Project: bot   Author: python-discord   File: test_base.py    MIT License 5 votes vote down vote up
def test_logging_test_case_respects_logging_level(self):
        """Test if LoggingTestCase does not raise for a logging level lower than provided."""
        with LoggingTestCase.assertNotLogs(self, level=logging.CRITICAL):
            self.log.info("Hello, this should raise an AssertionError") 
Example 46
Project: datadog-logger   Author: ustudio   File: test_datadog_logger.py    MIT License 5 votes vote down vote up
def test_includes_maps_critical_to_error(self, mock_dd):
        handler = DatadogLogHandler()

        record = logging.makeLogRecord({
            "msg": "Some message",
            "levelno": logging.CRITICAL
        })

        handler.emit(record)

        mock_dd.api.Event.create.assert_called_with(
            title="Some message", text="Some message",
            alert_type="error") 
Example 47
Project: pore-c   Author: nanoporetech   File: cli.py    Mozilla Public License 2.0 5 votes vote down vote up
def cli(verbosity, quiet):
    """Pore-C tools

    A suite of tools designed to analyse Oxford Nanopore reads with multiway chromatin contacts.
    """
    if quiet:
        logger.setLevel(logging.CRITICAL)
    elif verbosity > 0:
        LOG_LEVELS = [logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG]
        offset = 2
        idx = min(len(LOG_LEVELS) - 1, offset + verbosity)
        logger.setLevel(LOG_LEVELS[idx])
    else:
        logger.setLevel(logging.INFO)
    logger.debug("Logger set up") 
Example 48
Project: Flask_Blog   Author: sugarguo   File: log.py    GNU General Public License v3.0 5 votes vote down vote up
def critical(self, msg, *args, **kwargs):
        """Delegate a critical call to the underlying logger."""

        self.log(logging.CRITICAL, msg, *args, **kwargs) 
Example 49
Project: battle-notifier   Author: chalamka   File: battle_notifier.py    MIT License 5 votes vote down vote up
def __init__(self, config_path='config.json', log_level=logging.CRITICAL):
        self.battles = []
        self.logger = configure_logging(log_level)

        config = self._load_config(config_path)
        self.application_id = config['application_id']
        self.clan_id = config['clan_id']
        self.bot_name = config['bot_name']
        self.icon_emoji = config['icon_emoji']
        self.channel_name = config['channel_name']
        self.slack_url = config['slack_url']
        self.update_interval = config['update_interval']
        self.clan_tag = config['clan_tag'] 
Example 50
Project: NiujiaoDebugger   Author: MrSrc   File: handlers.py    GNU General Public License v3.0 5 votes vote down vote up
def getEventType(self, record):
        """
        Return the event type for the record.

        Override this if you want to specify your own types. This version does
        a mapping using the handler's typemap attribute, which is set up in
        __init__() to a dictionary which contains mappings for DEBUG, INFO,
        WARNING, ERROR and CRITICAL. If you are using your own levels you will
        either need to override this method or place a suitable dictionary in
        the handler's typemap attribute.
        """
        return self.typemap.get(record.levelno, self.deftype) 
Example 51
Project: NiujiaoDebugger   Author: MrSrc   File: utils.py    GNU General Public License v3.0 5 votes vote down vote up
def disable_logger():
    """Context manager to disable asyncio logger.

    For example, it can be used to ignore warnings in debug mode.
    """
    old_level = logger.level
    try:
        logger.setLevel(logging.CRITICAL+1)
        yield
    finally:
        logger.setLevel(old_level) 
Example 52
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 5 votes vote down vote up
def test_queue_listener(self):
        handler = support.TestHandler(support.Matcher())
        listener = logging.handlers.QueueListener(self.queue, handler)
        listener.start()
        try:
            self.que_logger.warning(self.next_message())
            self.que_logger.error(self.next_message())
            self.que_logger.critical(self.next_message())
        finally:
            listener.stop()
        self.assertTrue(handler.matches(levelno=logging.WARNING, message='1'))
        self.assertTrue(handler.matches(levelno=logging.ERROR, message='2'))
        self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='3'))
        handler.close()

        # Now test with respect_handler_level set

        handler = support.TestHandler(support.Matcher())
        handler.setLevel(logging.CRITICAL)
        listener = logging.handlers.QueueListener(self.queue, handler,
                                                  respect_handler_level=True)
        listener.start()
        try:
            self.que_logger.warning(self.next_message())
            self.que_logger.error(self.next_message())
            self.que_logger.critical(self.next_message())
        finally:
            listener.stop()
        self.assertFalse(handler.matches(levelno=logging.WARNING, message='4'))
        self.assertFalse(handler.matches(levelno=logging.ERROR, message='5'))
        self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='6'))
        handler.close() 
Example 53
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 5 votes vote down vote up
def test_disable(self):
        old_disable = logging.root.manager.disable
        # confirm our assumptions are correct
        self.assertEqual(old_disable, 0)
        self.addCleanup(logging.disable, old_disable)

        logging.disable(83)
        self.assertEqual(logging.root.manager.disable, 83)

        # test the default value introduced in 3.7
        # (Issue #28524)
        logging.disable()
        self.assertEqual(logging.root.manager.disable, logging.CRITICAL) 
Example 54
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 5 votes vote down vote up
def test_nested(self):
        class Adapter(logging.LoggerAdapter):
            prefix = 'Adapter'

            def process(self, msg, kwargs):
                return f"{self.prefix} {msg}", kwargs

        msg = 'Adapters can be nested, yo.'
        adapter = Adapter(logger=self.logger, extra=None)
        adapter_adapter = Adapter(logger=adapter, extra=None)
        adapter_adapter.prefix = 'AdapterAdapter'
        self.assertEqual(repr(adapter), repr(adapter_adapter))
        adapter_adapter.log(logging.CRITICAL, msg, self.recording)
        self.assertEqual(len(self.recording.records), 1)
        record = self.recording.records[0]
        self.assertEqual(record.levelno, logging.CRITICAL)
        self.assertEqual(record.msg, f"Adapter AdapterAdapter {msg}")
        self.assertEqual(record.args, (self.recording,))
        orig_manager = adapter_adapter.manager
        self.assertIs(adapter.manager, orig_manager)
        self.assertIs(self.logger.manager, orig_manager)
        temp_manager = object()
        try:
            adapter_adapter.manager = temp_manager
            self.assertIs(adapter_adapter.manager, temp_manager)
            self.assertIs(adapter.manager, temp_manager)
            self.assertIs(self.logger.manager, temp_manager)
        finally:
            adapter_adapter.manager = orig_manager
        self.assertIs(adapter_adapter.manager, orig_manager)
        self.assertIs(adapter.manager, orig_manager)
        self.assertIs(self.logger.manager, orig_manager) 
Example 55
Project: snaut   Author: pmandera   File: utils.py    Apache License 2.0 5 votes vote down vote up
def get_logger(log_name, log_file, log_level):
    """
    Get a logger.

    If log_dest is set to 'gunicorn.error' use gunicorn error logger. Otherwise,
    create a new logger with log_dest and use log_dest as ther path for the log
    file.
    """

    LEVELS = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'critical': logging.CRITICAL}

    level = LEVELS[log_level]

    logger = logging.getLogger(log_name)
    logger.setLevel(level)

    if log_file:
        handler = RotatingFileHandler(os.path.abspath(log_file),
                                      maxBytes=10000, backupCount=1)
        handler.setLevel(level)

        formatter = logging.Formatter(
            '%(asctime)s [%(levelname)s] %(message)s')

        handler.setFormatter(formatter)

        logger.addHandler(handler)

    return logger 
Example 56
Project: trove-dashboard   Author: openstack   File: tests.py    Apache License 2.0 5 votes vote down vote up
def _test_details(self, database, test_text, assert_contains=True):
        self.mock_instance_get.return_value = database
        self.mock_flavor_get.return_value = self.flavors.first()
        self.mock_root_show.return_value = self.database_user_roots.first()

        # Suppress expected log messages in the test output
        loggers = []
        toSuppress = ["trove_dashboard.content.databases.tabs",
                      "horizon.tables"]
        for cls in toSuppress:
            logger = logging.getLogger(cls)
            loggers.append((logger, logger.getEffectiveLevel()))
            logger.setLevel(logging.CRITICAL)
        try:
            res = self.client.get(DETAILS_URL)
            self.mock_instance_get.assert_called_once_with(
                test.IsHttpRequest(), test.IsA(six.text_type))
            self.mock_flavor_get.assert_called_once_with(
                test.IsHttpRequest(), test.IsA(str))
            self.mock_root_show.assert_called_once_with(
                test.IsHttpRequest(), test.IsA(str))
            self.assertTemplateUsed(res, 'project/databases/'
                                         '_detail_overview.html')
            if assert_contains:
                self.assertContains(res, test_text)
            else:
                self.assertNotContains(res, test_text)
        finally:
            # Restore the previous log levels
            for (log, level) in loggers:
                log.setLevel(level) 
Example 57
Project: trove-dashboard   Author: openstack   File: tests.py    Apache License 2.0 5 votes vote down vote up
def test_log_tab_exception(self):
        database = self.databases.first()
        database_id = database.id

        self.mock_instance_get.return_value = database
        self.mock_log_list.side_effect = self.exceptions.trove
        self.mock_flavor_get.return_value = self.flavors.first()
        self.mock_root_show.return_value = self.database_user_roots.first()

        detail_url = reverse('horizon:project:databases:detail',
                             args=[database_id])
        url = detail_url + '?tab=instance_details__logs_tab'

        toSuppress = ["trove_dashboard.content.databases.tabs"]

        loggers = []
        for cls in toSuppress:
            logger = logging.getLogger(cls)
            loggers.append((logger, logger.getEffectiveLevel()))
            logger.setLevel(logging.CRITICAL)

        try:
            res = self.client.get(url)
            self.mock_instance_get.assert_called_once_with(
                test.IsHttpRequest(), test.IsA(six.text_type))
            self.mock_log_list.assert_called_once_with(
                test.IsHttpRequest(), database_id)
            self.mock_flavor_get.assert_called_once_with(
                test.IsHttpRequest(), database.flavor["id"])
            self.mock_root_show.assert_called_once_with(
                test.IsHttpRequest(), database.id)
            table_data = res.context['logs_table'].data
            self.assertNotEqual(len(self.logs.list()), len(table_data))
            self.assertTemplateUsed(
                res, 'horizon/common/_detail_table.html')
        finally:
            # Restore the previous log levels
            for (log, level) in loggers:
                log.setLevel(level) 
Example 58
Project: trove-dashboard   Author: openstack   File: tests.py    Apache License 2.0 5 votes vote down vote up
def test_shrink_cluster_exception(self):
        cluster = self.trove_clusters.first()
        self.mock_cluster_get.return_value = cluster
        instance_id = cluster.instances[0]['id']
        cluster_instances = [{'id': instance_id}]
        self.mock_cluster_shrink.side_effect = self.exceptions.trove

        url = reverse(
            'horizon:project:database_clusters:cluster_shrink_details',
            args=[cluster.id])
        action = "".join([tables.ClusterShrinkInstancesTable.Meta.name, '__',
                          tables.ClusterShrinkAction.name, '__',
                          instance_id])

        toSuppress = ["trove_dashboard.content.database_clusters.tables"]

        # Suppress expected log messages in the test output
        loggers = []
        for cls in toSuppress:
            logger = logging.getLogger(cls)
            loggers.append((logger, logger.getEffectiveLevel()))
            logger.setLevel(logging.CRITICAL)

        try:
            res = self.client.post(url, {'action': action})
            self.mock_cluster_get.assert_called_once_with(
                test.IsHttpRequest(), cluster.id)
            self.mock_cluster_shrink.assert_called_once_with(
                test.IsHttpRequest(), cluster.id, cluster_instances)
            self.assertMessageCount(error=1)
            self.assertRedirectsNoFollow(res, INDEX_URL)
        finally:
            # Restore the previous log levels
            for (log, level) in loggers:
                log.setLevel(level) 
Example 59
Project: trove-dashboard   Author: openstack   File: tests.py    Apache License 2.0 5 votes vote down vote up
def test_add_parameter_exception_on_parameters(self):
        try:
            config = self.database_configurations.first()
            self.mock_get_configuration.return_value = config

            self.mock_configuration_get.return_value = config

            ds = self._get_test_datastore('mysql')
            dsv = self._get_test_datastore_version(ds.id, '5.5')
            self.mock_configuration_parameters_list.side_effect = (
                self.exceptions.trove)
            toSuppress = ["trove_dashboard.content."
                          "database_configurations.forms", ]

            # Suppress expected log messages in the test output
            loggers = []
            for cls in toSuppress:
                logger = logging.getLogger(cls)
                loggers.append((logger, logger.getEffectiveLevel()))
                logger.setLevel(logging.CRITICAL)

            try:
                res = self.client.get(
                    self._get_url_with_arg(ADD_URL, config.id))
                self.mock_get_configuration.assert_called_once()
                self.mock_configuration_get.assert_called_once_with(
                    test.IsHttpRequest())
                (self.mock_configuration_parameters_list
                     .assert_called_once_with(
                         test.IsHttpRequest(),
                         ds.name,
                         dsv.name))
                self.assertEqual(res.status_code, 302)

            finally:
                # Restore the previous log levels
                for (log, level) in loggers:
                    log.setLevel(level)
        finally:
            config_param_manager.delete(config.id) 
Example 60
Project: dotfiles   Author: cpjreynolds   File: cmd.py    MIT License 5 votes vote down vote up
def google(query):
  sleep(.5) # so we aren't querying EVERYTHING we type
  g = pygoogle(userInput, log_level=logging.CRITICAL)
  g.pages = 1
  out = g.get_urls()
  if (len(out) >= 1):  
    append_output(out[0], "xdg-open " + out[0])
    update_output() 
Example 61
Project: smartcam   Author: GeorgeZhai   File: service.py    Apache License 2.0 5 votes vote down vote up
def get_loglevel(level = "INFO"):
    LOG_LEVEL_MAP = {"CRITICAL": logging.CRITICAL,
            "ERROR": logging.ERROR,
            "WARNING": logging.WARNING,
            "INFO": logging.INFO,
            "DEBUG": logging.DEBUG,
            "NOTSET": logging.NOTSET}
    return LOG_LEVEL_MAP[level] 
Example 62
Project: dephell   Author: dephell   File: logging_helpers.py    MIT License 5 votes vote down vote up
def __init__(self, low=logging.DEBUG, high=logging.CRITICAL):
        # Convert str level representation to level number.
        # Example: "DEBUG" -> 10
        if isinstance(low, str):
            low = getattr(logging, low)
        if isinstance(high, str):
            high = getattr(logging, high)

        self._low = low
        self._high = high
        super().__init__() 
Example 63
Project: qpid-bow   Author: Bynder   File: qpid_bow.py    MIT License 5 votes vote down vote up
def main():
    logging.basicConfig(level=logging.CRITICAL)
    parser = ArgumentParser(prog='qb')
    parser.set_defaults(parser=parser)
    action = parser.add_subparsers(title='commands')

    create_command('queue', 'Manage queues', action,
                   (queue_create_parser, queue_delete_parser,
                    queue_purge_parser, queue_reroute_parser,
                    queue_stats_parser))

    create_command('message', 'Manage messages', action,
                   (message_receive_parser, message_send_parser))

    create_command('route', 'Manage routes', action,
                   (route_dump_parser, route_config_parser))

    create_command('connection', 'Manage connections', action,
                   (connection_kill_parser,))

    create_command('session', 'Manage sessions', action,
                   (session_outgoing_parser,))

    args: Namespace = parser.parse_args()
    if len(args.__dict__) <= 1:
        args.parser.print_help()
    else:
        args.func(args) 
Example 64
Project: maser4py   Author: maserlib   File: toolbox.py    GNU General Public License v3.0 5 votes vote down vote up
def set_level(logger_or_handler, quiet=False, debug=False):
    if debug:
        logger_or_handler.setLevel(logging.DEBUG)
    elif quiet:
        logger_or_handler.setLevel(logging.CRITICAL + 10)
    else:
        logger_or_handler.setLevel(logging.INFO) 
Example 65
Project: spacel-provision   Author: stratos   File: main.py    MIT License 5 votes vote down vote up
def setup_logging(level=logging.DEBUG):
    root_logger = logging.getLogger()
    for handler in root_logger.handlers:
        if isinstance(handler, logging.StreamHandler):
            return

    if level > logging.DEBUG:
        date_format = '%Y-%m-%d %H:%M:%S'
    else:
        date_format = None

    formatter = ColoredFormatter(
        "%(log_color)s%(asctime)s - %(name)s - %(message)s",
        datefmt=date_format,
        reset=True,
        log_colors={
            'DEBUG': 'cyan',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red'
        }
    )
    stream_out = logging.StreamHandler()
    stream_out.setLevel(level)
    stream_out.setFormatter(formatter)
    root_logger.addHandler(stream_out)
    logging.getLogger('boto3').setLevel(logging.CRITICAL)
    logging.getLogger('botocore').setLevel(logging.CRITICAL)
    logging.getLogger('paramiko').setLevel(logging.CRITICAL)
    logging.getLogger('requests').setLevel(logging.CRITICAL)
    logging.getLogger('tldextract').setLevel(logging.CRITICAL)
    logging.getLogger('spacel').setLevel(logging.DEBUG) 
Example 66
Project: cct   Author: awacha   File: logtreeview.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_logentry(self, record: logging.LogRecord):
        model = self.builder.get_object('logstore')
        assert isinstance(model, Gtk.ListStore)
        if record.levelno == logging.DEBUG:
            textcolor = 'gray'
            bgcolor = 'white'
        elif record.levelno == logging.INFO:
            textcolor = 'black'
            bgcolor = 'white'
        elif record.levelno == logging.WARNING:
            textcolor = 'orange'
            bgcolor = 'white'
        elif record.levelno == logging.ERROR:
            textcolor = 'red'
            bgcolor = 'white'
        elif record.levelno == logging.CRITICAL:
            textcolor = 'black'
            bgcolor = 'red'
        else:
            textcolor = 'blue'
            bgcolor = 'white'
        it = model.append(
            [record.asctime, record.levelname, record.levelno, '{}:{:d}'.format(record.name, record.lineno),
             record.getMessage(), textcolor, bgcolor, record.levelno >= self.filterlevel])
        while len(model) > self.nentries:
            model.remove(model.get_iter_first())
        if self.builder.get_object('autoscroll_checkbutton').get_active():
            filteredmodel = self.builder.get_object('logstore_filtered')
            assert isinstance(filteredmodel, Gtk.TreeModelFilter)
            success, it = filteredmodel.convert_child_iter_to_iter(it)
            if success:
                self.builder.get_object('logview').scroll_to_cell(filteredmodel.get_path(it), None, True, 0.0, 1.0)
        self.update_shown_count() 
Example 67
Project: cct   Author: awacha   File: logviewer.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def data(self, modelindex, role=None):
        if role is None:
            role = QtCore.Qt.DisplayRole
        if role == QtCore.Qt.DisplayRole:
            rec = self.records()[modelindex.row()]
            return str(getattr(rec, self.columnConfig[modelindex.column()][0]))
        elif role == QtCore.Qt.BackgroundRole:
            rec = self.records()[modelindex.row()]
            assert isinstance(rec, logging.LogRecord)
            if rec.levelno >= logging.CRITICAL:
                return QtGui.QBrush(Qt.red)
            else:
                return None
        elif role == QtCore.Qt.ForegroundRole:
            rec = self.records()[modelindex.row()]
            assert isinstance(rec, logging.LogRecord)
            if rec.levelno >= logging.CRITICAL:
                return QtGui.QBrush(Qt.black)
            elif rec.levelno >= logging.ERROR:
                return QtGui.QBrush(Qt.red)
            elif rec.levelno >= logging.WARNING:
                return QtGui.QBrush(Qt.darkYellow)
            elif rec.levelno >= logging.INFO:
                return QtGui.QBrush(Qt.black)
            else:
                return QtGui.QBrush(Qt.gray)
        elif role == QtCore.Qt.TextAlignmentRole:
            return Qt.AlignTop | Qt.AlignLeft
        return None 
Example 68
Project: universal_notifications   Author: HealthByRo   File: notifications.py    MIT License 5 votes vote down vote up
def send_email(self, subject, context, receiver):
        template = self.get_template()
        html = template.render(context)
        # update paths
        base = context["protocol"] + context["domain"]
        sender = self.sender or settings.DEFAULT_FROM_EMAIL
        if getattr(settings, "UNIVERSAL_NOTIFICATIONS_USE_PREMAILER", True) and self.use_premailer is not False:
            html = html.replace("{settings.STATIC_URL}CACHE/".format(settings=settings),
                                "{settings.STATIC_ROOT}/CACHE/".format(settings=settings))  # get local file
            html = Premailer(html,
                             remove_classes=False,
                             exclude_pseudoclasses=False,
                             keep_style_tags=True,
                             include_star_selectors=True,
                             strip_important=False,
                             cssutils_logging_level=logging.CRITICAL,
                             base_url=base).transform()

        # if subject is not provided, try to extract it from <title> tag
        if not subject:
            self.email_subject = self.__get_title_from_html(html)
            subject = self.prepare_subject()

        email = EmailMessage(subject, html, sender, [receiver], attachments=self.attachments)
        if self.categories:
            email.categories = self.categories

        if self.sendgrid_asm:
            email.asm = self.sendgrid_asm

        email.content_subtype = "html"
        email.send(fail_silently=False) 
Example 69
Project: logtrace   Author: paul-wolf   File: __init__.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self,
                 logger=None,  # we'll emit output here
                 delimiter="; ",  # delimiter between parts
                 tag='',  # add a label to the log entry, non-unique
                 unique_id=False,  # create a uuid to identify the log?
                 level=None,  # default level if you want to use emit()
                 verbosity='v'):
        self.uid = None
        self.verbosity = verbosity
        self.data = {}
        if logger:
            self.logger = logger
        else:
            self.logger = logging.getLogger(__name__)
        self.delimiter = delimiter
        if level:
            self.level = level
        else:
            self.level = self.logger.getEffectiveLevel()

        self.event_log = []

        if tag:  # prepend log record tag
            if not tag[0] == '#':
                tag = '#' + tag
            self.event_log.append(tag)
        if unique_id:
            self.uid = uuid.uuid4()
        self.clean = False

        self.emit_funcs = {
            logging.DEBUG: self.logger.debug,
            logging.INFO: self.logger.info,
            logging.WARNING: self.logger.warning,
            logging.ERROR: self.logger.error,
            logging.CRITICAL: self.logger.critical,
        }

        self.start = time.time() 
Example 70
Project: squeakspace   Author: eek6   File: _ansistrm.py    GNU General Public License v3.0 5 votes vote down vote up
def main():
    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.addHandler(ColorizingStreamHandler())
    logging.debug('DEBUG')
    logging.info('INFO')
    logging.warning('WARNING')
    logging.error('ERROR')
    logging.critical('CRITICAL') 
Example 71
Project: henet   Author: AcrDijon   File: test_workers.py    Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.workers = MemoryWorkers(size=1)
        logger.setLevel(logging.CRITICAL) 
Example 72
Project: pyblish-win   Author: pyblish   File: test_logging.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_flat(self):
        #Logging levels in a flat logger namespace.
        m = self.next_message

        ERR = logging.getLogger("ERR")
        ERR.setLevel(logging.ERROR)
        INF = logging.getLogger("INF")
        INF.setLevel(logging.INFO)
        DEB = logging.getLogger("DEB")
        DEB.setLevel(logging.DEBUG)

        # These should log.
        ERR.log(logging.CRITICAL, m())
        ERR.error(m())

        INF.log(logging.CRITICAL, m())
        INF.error(m())
        INF.warn(m())
        INF.info(m())

        DEB.log(logging.CRITICAL, m())
        DEB.error(m())
        DEB.warn (m())
        DEB.info (m())
        DEB.debug(m())

        # These should not log.
        ERR.warn(m())
        ERR.info(m())
        ERR.debug(m())

        INF.debug(m())

        self.assert_log_lines([
            ('ERR', 'CRITICAL', '1'),
            ('ERR', 'ERROR', '2'),
            ('INF', 'CRITICAL', '3'),
            ('INF', 'ERROR', '4'),
            ('INF', 'WARNING', '5'),
            ('INF', 'INFO', '6'),
            ('DEB', 'CRITICAL', '7'),
            ('DEB', 'ERROR', '8'),
            ('DEB', 'WARNING', '9'),
            ('DEB', 'INFO', '10'),
            ('DEB', 'DEBUG', '11'),
        ]) 
Example 73
Project: pyblish-win   Author: pyblish   File: test_logging.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_config7_ok(self):
        with captured_stdout() as output:
            self.apply_config(self.config1a)
            logger = logging.getLogger("compiler.parser")
            # See issue #11424. compiler-hyphenated sorts
            # between compiler and compiler.xyz and this
            # was preventing compiler.xyz from being included
            # in the child loggers of compiler because of an
            # overzealous loop termination condition.
            hyphenated = logging.getLogger('compiler-hyphenated')
            # All will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            hyphenated.critical(self.next_message())
            self.assert_log_lines([
                ('INFO', '1'),
                ('ERROR', '2'),
                ('CRITICAL', '3'),
            ], stream=output)
            # Original logger output is empty.
            self.assert_log_lines([])
        with captured_stdout() as output:
            self.apply_config(self.config7)
            logger = logging.getLogger("compiler.parser")
            self.assertFalse(logger.disabled)
            # Both will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            logger = logging.getLogger("compiler.lexer")
            # Both will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            # Will not appear
            hyphenated.critical(self.next_message())
            self.assert_log_lines([
                ('INFO', '4'),
                ('ERROR', '5'),
                ('INFO', '6'),
                ('ERROR', '7'),
            ], stream=output)
            # Original logger output is empty.
            self.assert_log_lines([]) 
Example 74
Project: flasky   Author: RoseOu   File: __main__.py    MIT License 4 votes vote down vote up
def parse_options():
    """
    Define and parse `optparse` options for command-line usage.
    """
    usage = """%prog [options] [INPUTFILE]
       (STDIN is assumed if no INPUTFILE is given)"""
    desc = "A Python implementation of John Gruber's Markdown. " \
           "http://packages.python.org/Markdown/"
    ver = "%%prog %s" % markdown.version
    
    parser = optparse.OptionParser(usage=usage, description=desc, version=ver)
    parser.add_option("-f", "--file", dest="filename", default=None,
                      help="Write output to OUTPUT_FILE. Defaults to STDOUT.",
                      metavar="OUTPUT_FILE")
    parser.add_option("-e", "--encoding", dest="encoding",
                      help="Encoding for input and output files.",)
    parser.add_option("-q", "--quiet", default = CRITICAL,
                      action="store_const", const=CRITICAL+10, dest="verbose",
                      help="Suppress all warnings.")
    parser.add_option("-v", "--verbose",
                      action="store_const", const=INFO, dest="verbose",
                      help="Print all warnings.")
    parser.add_option("-s", "--safe", dest="safe", default=False,
                      metavar="SAFE_MODE",
                      help="'replace', 'remove' or 'escape' HTML tags in input")
    parser.add_option("-o", "--output_format", dest="output_format", 
                      default='xhtml1', metavar="OUTPUT_FORMAT",
                      help="'xhtml1' (default), 'html4' or 'html5'.")
    parser.add_option("--noisy",
                      action="store_const", const=DEBUG, dest="verbose",
                      help="Print debug messages.")
    parser.add_option("-x", "--extension", action="append", dest="extensions",
                      help = "Load extension EXTENSION.", metavar="EXTENSION")
    parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol", 
                      action='store_false', default=True,
                      help="Observe number of first item of ordered lists.")

    (options, args) = parser.parse_args()

    if len(args) == 0:
        input_file = None
    else:
        input_file = args[0]

    if not options.extensions:
        options.extensions = []

    return {'input': input_file,
            'output': options.filename,
            'safe_mode': options.safe,
            'extensions': options.extensions,
            'encoding': options.encoding,
            'output_format': options.output_format,
            'lazy_ol': options.lazy_ol}, options.verbose 
Example 75
Project: fastclass   Author: cwerner   File: fc_download.py    Apache License 2.0 4 votes vote down vote up
def crawl(folder: str, search: str, maxnum:int, crawlers: [List[str]] = ['GOOGLE', 'BING', 'BAIDU']) -> Dict[str, str]:
    """Crawl web sites for images"""
    print('(1) Crawling ...')
    # prepare folders
    os.makedirs(folder, exist_ok=True)

    sources = {}
    if maxnum > 1000:
        print("Max num limited to 1000")
        maxnum = 1000

    for c in crawlers:
        print(f'    -> {c}')
        if c == 'GOOGLE':
            google_crawler = GoogleImageCrawler(
                downloader_cls=CustomDownloader,
                log_level=logging.CRITICAL,
                feeder_threads=1,
                parser_threads=1,
                downloader_threads=4,
                storage={'root_dir': folder})

            google_crawler.crawl(keyword=search, offset=0, max_num=maxnum,
                                min_size=(200,200), max_size=None, file_idx_offset=0)

        if c == 'BING':
            bing_crawler = BingImageCrawler(downloader_cls=CustomDownloader,
                                            log_level=logging.CRITICAL,
                                            downloader_threads=4,
                                            storage={'root_dir': folder})
            bing_crawler.crawl(keyword=search, filters=None, offset=0, max_num=maxnum, file_idx_offset='auto')


        if c == 'BAIDU':
            baidu_crawler = BaiduImageCrawler(downloader_cls=CustomDownloader,
                                    log_level=logging.CRITICAL,
                                    storage={'root_dir': folder})
            baidu_crawler.crawl(keyword=search, offset=0, max_num=maxnum,
                                min_size=(200,200), max_size=None, file_idx_offset='auto')


    return {k: v for k, v in CustomDownloader.registry.items() if k is not None} 
Example 76
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 4 votes vote down vote up
def test_flat(self):
        # Logging levels in a flat logger namespace.
        m = self.next_message

        ERR = logging.getLogger("ERR")
        ERR.setLevel(logging.ERROR)
        INF = logging.LoggerAdapter(logging.getLogger("INF"), {})
        INF.setLevel(logging.INFO)
        DEB = logging.getLogger("DEB")
        DEB.setLevel(logging.DEBUG)

        # These should log.
        ERR.log(logging.CRITICAL, m())
        ERR.error(m())

        INF.log(logging.CRITICAL, m())
        INF.error(m())
        INF.warning(m())
        INF.info(m())

        DEB.log(logging.CRITICAL, m())
        DEB.error(m())
        DEB.warning(m())
        DEB.info(m())
        DEB.debug(m())

        # These should not log.
        ERR.warning(m())
        ERR.info(m())
        ERR.debug(m())

        INF.debug(m())

        self.assert_log_lines([
            ('ERR', 'CRITICAL', '1'),
            ('ERR', 'ERROR', '2'),
            ('INF', 'CRITICAL', '3'),
            ('INF', 'ERROR', '4'),
            ('INF', 'WARNING', '5'),
            ('INF', 'INFO', '6'),
            ('DEB', 'CRITICAL', '7'),
            ('DEB', 'ERROR', '8'),
            ('DEB', 'WARNING', '9'),
            ('DEB', 'INFO', '10'),
            ('DEB', 'DEBUG', '11'),
        ]) 
Example 77
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 4 votes vote down vote up
def test_config7_ok(self):
        with support.captured_stdout() as output:
            self.apply_config(self.config1a)
            logger = logging.getLogger("compiler.parser")
            # See issue #11424. compiler-hyphenated sorts
            # between compiler and compiler.xyz and this
            # was preventing compiler.xyz from being included
            # in the child loggers of compiler because of an
            # overzealous loop termination condition.
            hyphenated = logging.getLogger('compiler-hyphenated')
            # All will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            hyphenated.critical(self.next_message())
            self.assert_log_lines([
                ('INFO', '1'),
                ('ERROR', '2'),
                ('CRITICAL', '3'),
            ], stream=output)
            # Original logger output is empty.
            self.assert_log_lines([])
        with support.captured_stdout() as output:
            self.apply_config(self.config7)
            logger = logging.getLogger("compiler.parser")
            self.assertFalse(logger.disabled)
            # Both will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            logger = logging.getLogger("compiler.lexer")
            # Both will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            # Will not appear
            hyphenated.critical(self.next_message())
            self.assert_log_lines([
                ('INFO', '4'),
                ('ERROR', '5'),
                ('INFO', '6'),
                ('ERROR', '7'),
            ], stream=output)
            # Original logger output is empty.
            self.assert_log_lines([]) 
Example 78
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 4 votes vote down vote up
def test_config_8a_ok(self):
        with support.captured_stdout() as output:
            self.apply_config(self.config1a)
            logger = logging.getLogger("compiler.parser")
            # See issue #11424. compiler-hyphenated sorts
            # between compiler and compiler.xyz and this
            # was preventing compiler.xyz from being included
            # in the child loggers of compiler because of an
            # overzealous loop termination condition.
            hyphenated = logging.getLogger('compiler-hyphenated')
            # All will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            hyphenated.critical(self.next_message())
            self.assert_log_lines([
                ('INFO', '1'),
                ('ERROR', '2'),
                ('CRITICAL', '3'),
            ], stream=output)
            # Original logger output is empty.
            self.assert_log_lines([])
        with support.captured_stdout() as output:
            self.apply_config(self.config8a)
            logger = logging.getLogger("compiler.parser")
            self.assertFalse(logger.disabled)
            # Both will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            logger = logging.getLogger("compiler.lexer")
            # Both will output a message
            logger.info(self.next_message())
            logger.error(self.next_message())
            # Will not appear
            hyphenated.critical(self.next_message())
            self.assert_log_lines([
                ('INFO', '4'),
                ('ERROR', '5'),
                ('INFO', '6'),
                ('ERROR', '7'),
            ], stream=output)
            # Original logger output is empty.
            self.assert_log_lines([]) 
Example 79
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 4 votes vote down vote up
def test_caching(self):
        root = self.root_logger
        logger1 = logging.getLogger("abc")
        logger2 = logging.getLogger("abc.def")

        # Set root logger level and ensure cache is empty
        root.setLevel(logging.ERROR)
        self.assertEqual(logger2.getEffectiveLevel(), logging.ERROR)
        self.assertEqual(logger2._cache, {})

        # Ensure cache is populated and calls are consistent
        self.assertTrue(logger2.isEnabledFor(logging.ERROR))
        self.assertFalse(logger2.isEnabledFor(logging.DEBUG))
        self.assertEqual(logger2._cache, {logging.ERROR: True, logging.DEBUG: False})
        self.assertEqual(root._cache, {})
        self.assertTrue(logger2.isEnabledFor(logging.ERROR))

        # Ensure root cache gets populated
        self.assertEqual(root._cache, {})
        self.assertTrue(root.isEnabledFor(logging.ERROR))
        self.assertEqual(root._cache, {logging.ERROR: True})

        # Set parent logger level and ensure caches are emptied
        logger1.setLevel(logging.CRITICAL)
        self.assertEqual(logger2.getEffectiveLevel(), logging.CRITICAL)
        self.assertEqual(logger2._cache, {})

        # Ensure logger2 uses parent logger's effective level
        self.assertFalse(logger2.isEnabledFor(logging.ERROR))

        # Set level to NOTSET and ensure caches are empty
        logger2.setLevel(logging.NOTSET)
        self.assertEqual(logger2.getEffectiveLevel(), logging.CRITICAL)
        self.assertEqual(logger2._cache, {})
        self.assertEqual(logger1._cache, {})
        self.assertEqual(root._cache, {})

        # Verify logger2 follows parent and not root
        self.assertFalse(logger2.isEnabledFor(logging.ERROR))
        self.assertTrue(logger2.isEnabledFor(logging.CRITICAL))
        self.assertFalse(logger1.isEnabledFor(logging.ERROR))
        self.assertTrue(logger1.isEnabledFor(logging.CRITICAL))
        self.assertTrue(root.isEnabledFor(logging.ERROR))

        # Disable logging in manager and ensure caches are clear
        logging.disable()
        self.assertEqual(logger2.getEffectiveLevel(), logging.CRITICAL)
        self.assertEqual(logger2._cache, {})
        self.assertEqual(logger1._cache, {})
        self.assertEqual(root._cache, {})

        # Ensure no loggers are enabled
        self.assertFalse(logger1.isEnabledFor(logging.CRITICAL))
        self.assertFalse(logger2.isEnabledFor(logging.CRITICAL))
        self.assertFalse(root.isEnabledFor(logging.CRITICAL)) 
Example 80
Project: trove-dashboard   Author: openstack   File: tests.py    Apache License 2.0 4 votes vote down vote up
def test_grow_cluster_exception(self):
        cluster = self.trove_clusters.first()
        self.mock_cluster_get.return_value = cluster
        cluster_volume = 1
        flavor = self.flavors.first()
        cluster_flavor = flavor.id
        cluster_flavor_name = flavor.name
        instances = [
            cluster_manager.ClusterInstance("id1", "name1", cluster_flavor,
                                            cluster_flavor_name,
                                            cluster_volume, "master", None,
                                            None),
            cluster_manager.ClusterInstance("id2", "name2", cluster_flavor,
                                            cluster_flavor_name,
                                            cluster_volume, "slave",
                                            "master", None),
            cluster_manager.ClusterInstance("id3", None, cluster_flavor,
                                            cluster_flavor_name,
                                            cluster_volume, None, None, None),
        ]

        manager = cluster_manager.ClusterInstanceManager(cluster.id)
        manager.instances = instances
        self.mock_get.return_value = manager
        self.mock_cluster_grow.side_effect = self.exceptions.trove

        url = reverse('horizon:project:database_clusters:cluster_grow_details',
                      args=[cluster.id])
        res = self.client.get(url)
        self.assertTemplateUsed(
            res, 'project/database_clusters/cluster_grow_details.html')

        toSuppress = ["trove_dashboard.content.database_clusters.tables"]

        # Suppress expected log messages in the test output
        loggers = []
        for cls in toSuppress:
            logger = logging.getLogger(cls)
            loggers.append((logger, logger.getEffectiveLevel()))
            logger.setLevel(logging.CRITICAL)

        try:
            action = "".join([tables.ClusterGrowInstancesTable.Meta.name, '__',
                              tables.ClusterGrowAction.name, '__',
                              cluster.id])
            res = self.client.post(url, {'action': action})

            self.mock_cluster_get.assert_called_once_with(
                test.IsHttpRequest(), cluster.id)
            self.assert_mock_multiple_calls_with_same_arguments(
                self.mock_get, 3, mock.call(cluster.id))
            self.mock_cluster_grow.assert_called_once_with(
                test.IsHttpRequest(), cluster.id, instances)
            self.assertMessageCount(error=1)
            self.assertRedirectsNoFollow(res, INDEX_URL)
        finally:
            # Restore the previous log levels
            for (log, level) in loggers:
                log.setLevel(level)