我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用logging.CRITICAL。
def __init__(self, config_file=DEFAULT_CONFIG_FILE): self.config = ConfigReader(config_file) self.states = {} self.geo_locator = Nominatim() self.tweet_count = 0 self.city_cache_appender = CacheAppender(self.config.cache_file_path) def get_level(): return { 'DEBUG': logging.DEBUG, 'INFO': logging.INFO, 'WARN': logging.WARNING, 'ERROR': logging.ERROR, 'FATAL': logging.FATAL, 'CRITICAL': logging.CRITICAL }[self.config.logging_level] logging.basicConfig(format="[%(levelname)s] %(name)s: %(message)s", level=get_level()) self.logger = logging.getLogger(self.__class__.__name__) self.logger.info("Analysing city names using config in %s" % config_file)
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
def log(self, message, level=logging.DEBUG, depth=0): """Prepend string to log messages to denote class.""" if depth <= 0: prefix = 'AmazonAccountUtils: ' else: prefix = "\t" * depth if level == CRITICAL: self.logger.critical(prefix + str(message)) elif level == ERROR: self.logger.error(prefix + str(message)) elif level == WARNING: self.logger.warning(prefix + str(message)) elif level == INFO: self.logger.info(prefix + str(message)) else: self.logger.debug(prefix + str(message))
def test_on_parse_exception(self): log_entry = self.logger.makeRecord('test', logging.CRITICAL, 'test_request', 22, 'Test Message', tuple(), None, extra={'action': 'EXCEPTION', 'method': 'GET', 'url': 'http://example.com', 'headers': OrderedDict([('Test-Header-1', 'header value 1'), ('Test-Header-2', 'header value 2')]), 'body': 'foobar', 'status_code': 404, 'elapsed': timedelta(seconds=0.214), 'exception': AttributeError('test exception')}) log_text = """EXCEPTION | GET http://example.com | 404 Not Found | 214 ms | AttributeError('test exception',) Headers: Test-Header-1: header value 1 Test-Header-2: header value 2 Body: foobar""" self.assertEqual(self.formatter.formatMessage(log_entry), log_text)
def test_on_parse_exception(self): ex = AttributeError('Testing Exception') resp = await self.session.request() await self.plugin.before_request(self.endpoint_desc, self.session, self.request_params) await self.plugin.on_parse_exception(self.endpoint_desc, self.session, self.request_params, resp, ex) self.assertEqual(self.logger.level, logging.CRITICAL) self.assertEqual(self.logger.message, "Testing Exception") self.assertEqual(self.logger.args, tuple()) self.maxDiff = None self.assertEqual(self.logger.kwargs, {"extra": {'action': 'EXCEPTION', 'endpoint': 'test_endpoint', 'path_param1': 'foo', 'path_param2': 'bar', 'service_name': 'test_service', 'status_code': resp.status, 'body': 'sss', 'elapsed': resp.elapsed, 'headers': resp.headers, 'exception': ex}})
def _get_logging_level(): """ Converts our ENV variable HA_LOG_LEVEL to a logging level object :return: logging level object """ _log_level = _get_config('LOG_LEVEL', 'info').lower() to_return = logging.INFO if _log_level == 'critical': to_return = logging.CRITICAL if _log_level == 'error': to_return = logging.ERROR if _log_level == 'warning': to_return = logging.WARNING if _log_level == 'debug': to_return = logging.DEBUG return to_return
def add_coloring_to_emit_ansi(fn): RED_BOLD = '\x1b[31;1m' RED = '\x1b[31m' GREEN = '\x1b[32m' YELLOW = '\x1b[33m' BLUE = '\x1b[34m' PINK = '\x1b[35m' CYAN = '\x1b[36m' DEFAULT = '\x1b[0m' def new(*args): levelno = args[1].levelno color = DEFAULT if levelno >= logging.CRITICAL: color = RED_BOLD elif levelno >= logging.ERROR: color = RED elif levelno >= logging.WARNING: color = YELLOW elif levelno >= logging.INFO: color = DEFAULT elif levelno >= logging.DEBUG: color = GREEN args[1].msg = color + str(args[1].msg) + DEFAULT return fn(*args) return new
def set_log(level, filename='jumpserver.log'): """ return a log file object ??????log?? """ log_file = os.path.join(LOG_DIR, filename) if not os.path.isfile(log_file): os.mknod(log_file) os.chmod(log_file, 0777) log_level_total = {'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARN, 'error': logging.ERROR, 'critical': logging.CRITICAL} logger_f = logging.getLogger('jumpserver') logger_f.setLevel(logging.DEBUG) fh = logging.FileHandler(log_file) fh.setLevel(log_level_total.get(level, logging.DEBUG)) formatter = logging.Formatter('%(asctime)s - %(filename)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logger_f.addHandler(fh) return logger_f
def _logging_levels(verbosity, quiet): # type: (int, bool) -> Sequence[int] """Determines the proper logging levels given required verbosity level and quiet. :param int verbosity: Requested level of verbosity :param bool quiet: Suppresses all logging when true :returns: local and root logging levels :rtype: list of int """ if quiet: return logging.CRITICAL, logging.CRITICAL if verbosity is None or verbosity <= 0: return logging.WARNING, logging.CRITICAL normalized_local = min(verbosity, MAX_LOGGING_LEVEL) normalized_root = min(verbosity - normalized_local, MAX_LOGGING_LEVEL) return LOGGING_LEVELS[normalized_local], LOGGING_LEVELS[normalized_root]
def __init__(self, fmt = None, datefmt = None): logging.Formatter.__init__(self, fmt, datefmt) # Color escape string COLOR_RED='\033[1;31m' COLOR_GREEN='\033[1;32m' COLOR_YELLOW='\033[1;33m' COLOR_BLUE='\033[1;34m' COLOR_PURPLE='\033[1;35m' COLOR_CYAN='\033[1;36m' COLOR_GRAY='\033[1;37m' COLOR_WHITE='\033[1;38m' COLOR_RESET='\033[1;0m' # Define log color self.LOG_COLORS = { 'DEBUG': '%s', 'INFO': COLOR_GREEN + '%s' + COLOR_RESET, 'WARNING': COLOR_YELLOW + '%s' + COLOR_RESET, 'ERROR': COLOR_RED + '%s' + COLOR_RESET, 'CRITICAL': COLOR_RED + '%s' + COLOR_RESET, 'EXCEPTION': COLOR_RED + '%s' + COLOR_RESET, }
def tolog(self, msg, level=None): try: level = level if level else self._level level = str(level).lower() level = self.get_map_level(level) if level == logging.DEBUG: self._logger.debug(msg) if level == logging.INFO: self._logger.info(msg) if level == logging.WARN: self._logger.warn(msg) if level == logging.ERROR: self._logger.error(msg) if level == logging.CRITICAL: self._logger.critical(msg) except Exception as expt: print expt
def cli(ctx, config, quiet, debug): """ Tumblr Downloader CLI utility """ # Logging setup if debug: log_level = logging.DEBUG else: log_level = logging.CRITICAL if quiet else logging.WARN ctx.log = logging.getLogger('tumdlr') ctx.log.setLevel(log_level) ch = logging.StreamHandler() ch.setLevel(log_level) ch.setFormatter(logging.Formatter('[%(levelname)s] %(name)s: %(message)s')) ctx.log.addHandler(ch) # First run? if not ctx.config['Development'].getboolean('AgreedToTerms'): first_run(ctx)
def format(self, record): if self.debug and self.color: if record.levelno >= logging.CRITICAL: color = TEXT_RED elif record.levelno >= logging.ERROR: color = TEXT_RED elif record.levelno >= logging.WARNING: color = TEXT_YELLOW elif record.levelno >= logging.INFO: color = TEXT_GREEN elif record.levelno >= logging.DEBUG: color = TEXT_CYAN else: color = TEXT_NORMAL record.levelname = "\x1b[%sm%s\x1b[%sm" % (color, record.levelname, TEXT_NORMAL) return logging.Formatter.format(self, record)
def get_logging_level(log_level): logging_level = logging.INFO if log_level == 'DEBUG': logging_level = logging.DEBUG elif log_level == 'INFO': logging_level = logging.INFO elif log_level == 'WARNING': logging_level = logging.WARNING elif log_level == 'ERROR': logging_level = logging.ERROR elif log_level == 'CRITICAL': logging_level = logging.CRITICAL else: print('Unknown or unset logging level. Using INFO') return logging_level
def run(self, argv = None, data = None, logger = None): """ Runs the function """ if not logger is None: assert isinstance(logger, logging.Logger), "logger is not a valid logging.Logger" self.logger = logger if not data is None: assert isinstance(data, Configuration), "data is not a valid QXSConsolas.Configuration.Configuration" self.data = data self.options, self.arguments = self._argparser.parseArguments(argv) if self._argparser.loglevel == 1: self._configureConsoleLoggers(logging.NOTSET, True) elif self._argparser.loglevel == -1: self._configureConsoleLoggers(logging.CRITICAL, False) try: self._argparser.validateRequiredArguments() return self._app(ApplicationData(self)) except Exception as e: logger.exception(e) return 1
def input_processor(state): mainlog = logging.getLogger() global default_log_level while True: # Wait for the user to press a key. command = raw_input() if command == '': # Toggle between scouts and log view state['display'] = 'stats' if state['display'] == 'logs' else 'logs' log.info("Showing {}".format(state['display'])) # Disable logging if in fullscreen more if state['display'] == 'logs': mainlog.setLevel(default_log_level) else: mainlog.setLevel(logging.CRITICAL)
def format(self, record): stdout_template = '{levelname}' + Fore.RESET + '] {threadName}: ' + '{message}' stdout_head = '[%s' allFormats = { logging.DEBUG: logging.StrFormatStyle(stdout_head % Fore.LIGHTBLUE_EX + stdout_template), logging.INFO: logging.StrFormatStyle(stdout_head % Fore.GREEN + stdout_template), logging.WARNING: logging.StrFormatStyle(stdout_head % Fore.LIGHTYELLOW_EX + stdout_template), logging.ERROR: logging.StrFormatStyle(stdout_head % Fore.LIGHTRED_EX + stdout_template), logging.CRITICAL: logging.StrFormatStyle(stdout_head % Fore.RED + stdout_template) } self._style = allFormats.get(record.levelno, logging.StrFormatStyle(logging._STYLES['{'][1])) self._fmt = self._style._fmt result = logging.Formatter.format(self, record) return result
def SuppressLogging(level=logging.ERROR): """Momentarilly suppress logging events from all loggers. TODO(jbudorick): This is not thread safe. Log events from other threads might also inadvertently disappear. Example: with logging_utils.SuppressLogging(): # all but CRITICAL logging messages are suppressed logging.info('just doing some thing') # not shown logging.critical('something really bad happened') # still shown Args: level: logging events with this or lower levels are suppressed. """ logging.disable(level) yield logging.disable(logging.NOTSET)
def test_router_syntax(self): """ Test router syntax error """ level = logger.getEffectiveLevel() logger.setLevel(logging.CRITICAL) # disable logging temporarily self.assertRaises(SyntaxError, load, data='x::y') self.assertRaises( SyntaxError, load, rdict=dict(BASE=dict(badkey="value"))) self.assertRaises(SyntaxError, load, rdict=dict( BASE=dict(), app=dict(default_application="name"))) self.myassertRaisesRegex(SyntaxError, "invalid syntax", load, data='x::y') self.myassertRaisesRegex(SyntaxError, "unknown key", load, rdict=dict(BASE=dict(badkey="value"))) self.myassertRaisesRegex(SyntaxError, "BASE-only key", load, rdict=dict(BASE=dict(), app=dict(default_application="name"))) logger.setLevel(level)
def _pip_main(self, *args): """Run pip.main with the specified ``args`` """ if self.quiet: import logging pip_log = logging.getLogger("pip") _level = pip_log.level pip_log.setLevel(logging.CRITICAL) elif self._should_color(): self._sys.stdout.write("\x1b[36m") try: self._pip.main(list(args)) finally: if self.quiet: pip_log.setLevel(_level) elif self._should_color(): self._sys.stdout.write("\x1b[0m") self._sys.stdout.flush()
def my_log(): logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', handlers=[logging.FileHandler('message.log', 'a', 'utf-8')]) # ?????_?????__ _log = logging.getLogger('app.' + __name__) host = '10.0.0.175' port = 8080 # ??? 'xxxx' % (aa, bb)???????? _log.error('error to connect to %s:%d', host, port) _log.addFilter(FilterFunc('foo')) # ?????foo()??????? lgg = logging.getLogger('app.network.client') lgg.propagate = False # ?????? lgg.error('do you see me?') # ???????? lgg.setLevel(logging.CRITICAL) lgg.error('now you see me?') logging.disable(logging.DEBUG) # ???????? # ??log??????main????????? config.fileConfig('applogcfg.ini')
def test_log_level_from_config(self): cfg = {'verbose_level': 0} self.assertEqual(logging.ERROR, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1} self.assertEqual(logging.WARNING, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 2} self.assertEqual(logging.INFO, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 3} self.assertEqual(logging.DEBUG, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1, 'log_level': 'critical'} self.assertEqual(logging.CRITICAL, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1, 'log_level': 'error'} self.assertEqual(logging.ERROR, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1, 'log_level': 'warning'} self.assertEqual(logging.WARNING, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1, 'log_level': 'info'} self.assertEqual(logging.INFO, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1, 'log_level': 'debug'} self.assertEqual(logging.DEBUG, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1, 'log_level': 'bogus'} self.assertEqual(logging.WARNING, logs.log_level_from_config(cfg)) cfg = {'verbose_level': 1, 'log_level': 'info', 'debug': True} self.assertEqual(logging.DEBUG, logs.log_level_from_config(cfg))
def __init__(self, fitsfile, ID = 0, quiet = False, clobber = False, cadence = 'lc', **kwargs): ''' ''' # Read kwargs self.ID = ID self._season = 12 self.mission = 'k2' self.clobber = clobber self.cadence = cadence # Initialize preliminary logging if not quiet: screen_level = logging.DEBUG else: screen_level = logging.CRITICAL everest.utils.InitLog(None, logging.DEBUG, screen_level, False) # Load self.fitsfile = fitsfile self.model_name = pyfits.getheader(self.fitsfile, 1)['MODEL'] self._weights = None self.load_fits()
def get_map_level(self,level="debug"): level = str(level).lower() #print "get_map_level:",level if level == "debug": return logging.DEBUG if level == "info": return logging.INFO if level == "warn": return logging.WARN if level == "error": return logging.ERROR if level == "critical": return logging.CRITICAL #}}} #{{{Redis
def raise_on_errors(errors, level=logging.CRITICAL): """Raise a CoTError if errors. Helper function because I had this code block everywhere. Args: errors (list): the error errors level (int, optional): the log level to use. Defaults to logging.CRITICAL Raises: CoTError: if errors is non-empty """ if errors: log.log(level, "\n".join(errors)) raise CoTError("\n".join(errors)) # guess_worker_impl {{{1
def getLevel( self ): """ A convenience wrapper around ``getEffectiveLevel()`` because the integer values for the various logging levels are clunky and probably don't mean anything to you. Returns: str: the name of the effective log level for this logging object, in lowercase (``"warning"``, ``"info"``, etc.) """ level = self.getEffectiveLevel() if level == logging.CRITICAL: return 'critical' elif level == logging.ERROR: return 'error' elif level == logging.WARNING: return 'warning' elif level == logging.INFO: return 'info' elif level == logging.DEBUG: return 'debug' elif level == logging.NOTSET: return 'notset' else: return 'unknown ({})'.format( level )
def init(cls): cls.logger = logging.getLogger(_LOGGER_NAME) logger = cls.logger levelname = os.environ.get('JUBAKIT_LOG_LEVEL', None) if not levelname: # Surpress printing logs by default. logger.addHandler(cls._NullHandler()) logger.setLevel(CRITICAL) return # Setup logger from environment variable. for lvl in (DEBUG, INFO, WARNING, ERROR, CRITICAL): if logging.getLevelName(lvl) == levelname: setup_logger(lvl) break else: setup_logger(INFO) logger.warning('invalid JUBAKIT_LOG_LEVEL (%s) specified; continue with INFO', levelname)
def set_log_level(verbose): """Convenience function for setting the log level. Parameters ---------- verbose : bool, str, int, or None The verbosity of messages to print. If a str, it can be either DEBUG, INFO, WARNING, ERROR, or CRITICAL. Note that these are for convenience and are equivalent to passing in logging.DEBUG, etc. For bool, True is the same as 'INFO', False is the same as 'WARNING'. """ if isinstance(verbose, bool): if verbose is True: verbose = 'INFO' else: verbose = 'WARNING' if isinstance(verbose, str): verbose = verbose.upper() logging_types = dict(DEBUG=logging.DEBUG, INFO=logging.INFO, WARNING=logging.WARNING, ERROR=logging.ERROR, CRITICAL=logging.CRITICAL) if verbose not in logging_types: raise ValueError('verbose must be of a valid type') verbose = logging_types[verbose] logger.setLevel(verbose)
def format(self, record): """ Format the contents of the given record to log. :param record: The record to log. :return: Results of calling super.format. """ if record.levelno == logging.CRITICAL: self._fmt = LavaLogFormatter.CRITICAL elif record.levelno == logging.ERROR: self._fmt = LavaLogFormatter.ERROR elif record.levelno == logging.WARNING: self._fmt = LavaLogFormatter.WARNING elif record.levelno == logging.INFO: self._fmt = LavaLogFormatter.INFO elif record.levelno == logging.DEBUG: self._fmt = LavaLogFormatter.DEBUG return super(LavaLogFormatter, self).format(record)
def configure_logging(log_level): """ Configure logging. :param log_level: The level to configure logging at. :return: None """ lvl = None if log_level.upper() == "INFO": lvl = logging.INFO elif log_level.upper() == "WARNING": lvl = logging.WARNING elif log_level.upper() == "ERROR": lvl = logging.ERROR elif log_level.upper() == "CRITICAL": lvl = logging.CRITICAL elif log_level.upper() == "DEBUG": lvl = logging.DEBUG if not lvl: raise ValueError("%s is not a valid log level." % log_level) logger.setLevel(lvl) formatter = LavaLogFormatter() stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) ConfigManager.LOGGING_LEVEL = lvl
def log_at_level(self, value): """ Make sure logging is always set at a valid level """ if value in [ logging.CRITICAL, logging.DEBUG, logging.ERROR, logging.FATAL, logging.INFO, logging.WARNING, ]: self._log_at_level = value self._set_logging() else: if not self._log_at_level: self._log_at_level = logging.WARNING self._set_logging() # ******************************************************************* # methods # *******************************************************************
def test_router_syntax(self): """ Test router syntax error """ level = logger.getEffectiveLevel() logger.setLevel(logging.CRITICAL) # disable logging temporarily self.assertRaises(SyntaxError, load, data='x:y') self.assertRaises( SyntaxError, load, rdict=dict(BASE=dict(badkey="value"))) self.assertRaises(SyntaxError, load, rdict=dict( BASE=dict(), app=dict(default_application="name"))) try: # 2.7+ only self.assertRaisesRegexp(SyntaxError, "invalid syntax", load, data='x:y') self.assertRaisesRegexp(SyntaxError, "unknown key", load, rdict=dict(BASE=dict(badkey="value"))) self.assertRaisesRegexp(SyntaxError, "BASE-only key", load, rdict=dict(BASE=dict(), app=dict(default_application="name"))) except AttributeError: pass logger.setLevel(level)
def init_logger(name='webdirfuzz', log_file_path=None): """ ??????? """ logger = logging.getLogger(name) log_handler = logging.FileHandler(log_file_path) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') log_handler.setFormatter(formatter) ll = (logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG) logger.setLevel(ll[4]) log_handler.setLevel(ll[4]) logger.addHandler(log_handler) return logger
def SuppressLogging(level=logging.ERROR): """Momentarilly suppress logging events from all loggers. TODO(jbudorick): This is not thread safe. Log events from other threads might also inadvertently dissapear. Example: with logging_utils.SuppressLogging(): # all but CRITICAL logging messages are suppressed logging.info('just doing some thing') # not shown logging.critical('something really bad happened') # still shown Args: level: logging events with this or lower levels are suppressed. """ logging.disable(level) yield logging.disable(logging.NOTSET)
def _get_logging_level(level): """ Map a syslog level to a logging level. The logging module levels start from 10 (DEBUG) and increase by 10 for every level of of severity (INFO, WARNING, ERROR, CRITICAL). syslog starts at 0 for the most severe messages (EMERGENCY) and increases by 1 for each lower level of messages (ALERT, CRITICAL, etc.). Mapping is mostly straightforward. """ assert level >= 0 if level >= logging.DEBUG: # level is already a logging-compatible value return level if level > LOG_DEBUG: return logging.DEBUG return [ logging.CRITICAL, # syslog.LOG_EMERG logging.CRITICAL, # syslog.LOG_ALERT, logging.CRITICAL, # syslog.LOG_CRIT, logging.ERROR, # syslog.LOG_ERR, logging.WARNING, # syslog.LOG_WARNING, _NOTICE, # syslog.LOG_NOTICE, logging.INFO, # syslog.LOG_INFO, logging.DEBUG, # syslog.LOG_DEBUG ][level]
def get_logger(level=logging.INFO, quite=False, debug=False, to_file=''): """ This function initialises a logger to stdout. :return: logger """ assert level in [logging.DEBUG, logging.INFO, logging.WARNING, logging.CRITICAL] logger = logging.getLogger('main') formatter = logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s - %(message)s') if debug: level = logging.DEBUG logger.setLevel(level=level) if not quite: if to_file: fh = logging.FileHandler(to_file) fh.setLevel(level=level) fh.setFormatter(formatter) logger.addHandler(fh) else: ch = logging.StreamHandler() ch.setLevel(level=level) ch.setFormatter(formatter) logger.addHandler(ch) return logger