我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用logging._levelToName()。
def make_logging_level_names_consistent(): """Rename the standard library's logging levels to match Twisted's. Twisted's new logging system in `twisted.logger` that is. """ for level in list(logging._levelToName): if level == logging.NOTSET: # When the logging level is not known in Twisted it's rendered as # a hyphen. This is not a common occurrence with `logging` but we # cater for it anyway. name = "-" elif level == logging.WARNING: # "Warning" is more consistent with the other level names than # "warn", so there is a fault in Twisted here. However it's easier # to change the `logging` module to match Twisted than vice-versa. name = "warn" else: # Twisted's level names are all lower-case. name = logging.getLevelName(level).lower() # For a preexisting level this will _replace_ the name. logging.addLevelName(level, name)
def get_stdout_levelname(): """Return the log levelname.""" return logging._levelToName[stdout_logger.level]
def cli(ctx, manager, output_format, stats, stop_on_error): """ CLI for multi-package manager upgrades. """ level = logger.level try: level_to_name = logging._levelToName # Fallback to pre-Python 3.4 internals. except AttributeError: level_to_name = logging._levelNames level_name = level_to_name.get(level, level) logger.debug('Verbosity set to {}.'.format(level_name)) # Print help screen and exit if no sub-commands provided. if ctx.invoked_subcommand is None: click.echo(ctx.get_help()) ctx.exit() # Filters out the subset of managers selected by the user. target_managers = [ m for mid, m in pool().items() if mid in set(manager)] if manager else pool().values() # Apply manager-level option to either raise on error or not. for m in target_managers: m.raise_on_error = stop_on_error # Pre-filters inactive managers. def keep_available(manager): if manager.available: return True logger.warning('Skip unavailable {} manager.'.format(manager.id)) # Use an iterator to not trigger log messages for subcommands not using # this variable. active_managers = ifilter(keep_available, target_managers) # Silence all log message for JSON rendering unless in debug mode. rendering = RENDERING_MODES[output_format] if rendering == 'json' and level_name != 'DEBUG': logger.setLevel(logging.CRITICAL * 2) # Load up global options to the context. ctx.obj = { 'target_managers': target_managers, 'active_managers': active_managers, 'rendering': rendering, 'stats': stats}
def logger(): getpid_patch = patch('logging.os.getpid', return_value=111) getpid_patch.start() time_patch = patch('logging.time.time', return_value=946725071.111111) time_patch.start() localzone_patch = patch('rfc5424logging.handler.get_localzone', return_value=timezone) localzone_patch.start() hostname_patch = patch('rfc5424logging.handler.socket.gethostname', return_value="testhostname") hostname_patch.start() connect_patch = patch('logging.handlers.socket.socket.connect', side_effect=connect_mock) connect_patch.start() sendall_patch = patch('logging.handlers.socket.socket.sendall', side_effect=connect_mock) sendall_patch.start() if '_levelNames' in logging.__dict__: # Python 2.7 level_patch = patch.dict(logging._levelNames) level_patch.start() else: # Python 3.x level_patch1 = patch.dict(logging._levelToName) level_patch1.start() level_patch2 = patch.dict(logging._nameToLevel) level_patch2.start() logging.raiseExceptions = True logger = logging.getLogger() logger.setLevel(logging.DEBUG) yield logger getpid_patch.stop() time_patch.stop() localzone_patch.stop() hostname_patch.stop() connect_patch.stop() sendall_patch.stop() if '_levelNames' in logging.__dict__: # Python 2.7 level_patch.stop() else: # Python 3.x level_patch1.stop() level_patch2.stop() Rfc5424SysLogAdapter._extra_levels_enabled = False