我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用logging.__dict__()。
def add_filehandler(logger,style="default",flevel='DEBUG', filename=None,filemode='w'): name = "" #-- define formats if style=='default': format = '%(asctime)s %(name)-12s %(levelname)-7s %(message)s' datefmt = '%a, %d %b %Y %H:%M' elif style=='grandpa': format = '# %(levelname)-7s %(message)s' datefmt = '%a, %d %b %Y %H:%M' elif style=='minimal': format = '' datefmt = '%a, %d %b %Y %H:%M' flevel = logging.__dict__[flevel.upper()] logging.basicConfig(level=flevel, format=format,datefmt=datefmt, filename=filename,filemode=filemode) fh = logging.FileHandler(filename) fh.setLevel(flevel) formatter = logging.Formatter(fmt=format,datefmt=datefmt) fh.setFormatter(formatter) logging.getLogger(name).addHandler(fh) logging.getLogger(name).handlers[-1].level = flevel return logging.getLogger(name)
def get_logger(obj, log_to_file, log_to_stdout): """Get logger for given object. Removes all previously assigned handlers from the logger. Parameters ---------- obj : Unknown Some object log_to_file: optional, boolean or string (default: False) Log results to given file, it will be located in the $BL_LOG_PATH log_to_stdout: optional, boolean (default: False) Log to standard output Returns ------- logger : Logger Logger object """ logger = logging.getLogger(type(obj).__name__) logger.handlers = [] # Remove all handlers logger.setLevel(logging.DEBUG) if log_to_file: bl_log_path = os.environ.get("BL_LOG_PATH", ".") log_file_name = "%s/%s" % (bl_log_path, log_to_file) handler = logging.FileHandler(log_file_name) handler.setLevel(logging.DEBUG) logger.addHandler(handler) if log_to_stdout: handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) logger.addHandler(handler) if (not (log_to_file or log_to_stdout) and "NullHandler" in logging.__dict__): handler = logging.NullHandler() logger.addHandler(handler) return logger
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