我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用logging.getLoggerClass()。
def create_logger(app): """Creates a logger for the given application. This logger works similar to a regular Python logger but changes the effective logging level based on the application's debug flag. Furthermore this function also removes all attached handlers in case there was a logger with the log name before. """ Logger = getLoggerClass() class DebugLogger(Logger): def getEffectiveLevel(x): if x.level == 0 and app.debug: return DEBUG return Logger.getEffectiveLevel(x) class DebugHandler(StreamHandler): def emit(x, record): StreamHandler.emit(x, record) if app.debug else None handler = DebugHandler() handler.setLevel(DEBUG) handler.setFormatter(Formatter(app.debug_log_format)) logger = getLogger(app.logger_name) # just in case that was not a new logger, get rid of all the handlers # already attached to it. del logger.handlers[:] logger.__class__ = DebugLogger logger.addHandler(handler) return logger
def get_a_logger(name): """ Read log config from `log.conf` if file is present. See the `log.conf.example` for a sample. :param name: string used for naming the logger """ cwd = os.getcwd() log_conf = os.path.join(cwd, CONFIG_FILE) if os.path.exists(log_conf): logging.config.fileConfig(log_conf) log = logging.getLogger(name) figlet = get_figlet(name) if figlet: print(figlet.format(__version__)) log.debug("{} logging was configured using: {}" .format(name, log_conf)) handlers = logging.getLoggerClass().root.handlers for h in [h for h in handlers if hasattr(h, 'baseFilename')]: log.debug("log file: {}".format(h.baseFilename)) else: lformat = '%(asctime)s: %(name)s.%(levelname)s ' \ '- %(filename)s+%(lineno)d: %(message)s' logging.basicConfig(format=lformat, level=logging.INFO) log = logging.getLogger(name) log.info("Logging was configured using defaults. " " To change that please use {} .".format(CONFIG_FILE)) return log
def create_logger(name, debug=False, format=None): Logger = getLoggerClass() class DebugLogger(Logger): def getEffectiveLevel(x): if x.level == 0 and debug: return DEBUG else: return Logger.getEffectiveLevel(x) class DebugHandler(StreamHandler): def emit(x, record): StreamHandler.emit(x, record) if debug else None handler = DebugHandler() handler.setLevel(DEBUG) if format: handler.setFormatter(Formatter(format)) logger = getLogger(name) del logger.handlers[:] logger.__class__ = DebugLogger logger.addHandler(handler) return logger
def getLogger(name): og_class = logging.getLoggerClass() try: logging.setLoggerClass(Logger) return logging.getLogger(name) finally: logging.setLoggerClass(og_class) # The main 'eyed3' logger
def viewLogs() -> None: QDesktopServices.openUrl(QUrl.fromLocalFile(logging.getLoggerClass().root.handlers[0].baseFilename))
def _test_logger(): logger = (logging.getLoggerClass())(name="test_logged_subprocess") logger.setLevel(logging.DEBUG) handler = ArrayHandler() logger.addHandler(handler) logger.messages = handler.messages return logger
def test_log_adapter(): import logging from takumi.log import MetaAdapter import sys ctx = {} logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) logger = MetaAdapter(logging.getLogger('takumi'), {'ctx': ctx}) logger_class = logging.getLoggerClass() with mock.patch.object(logger_class, '_log') as mock_log: logger.info('hello world') mock_log.assert_called_with(20, '[-/- -] hello world', ()) ctx['meta'] = { 'client_name': 'test_client', 'client_version': '1.0.1' } ctx['env'] = {'client_addr': '127.0.0.1'} with mock.patch.object(logger_class, '_log') as mock_log: logger.info('hello world') mock_log.assert_called_with( 20, '[test_client/1.0.1 127.0.0.1] hello world', ()) ctx['log_extra'] = '353456436546 xxxx yyyy' with mock.patch.object(logger_class, '_log') as mock_log: logger.info('hello world') mock_log.assert_called_with( 20, '[test_client/1.0.1 127.0.0.1 353456436546 xxxx yyyy] hello world', ())
def getLogger(name=None): OrigLoggerClass = logging.getLoggerClass() try: logging.setLoggerClass(Logger) return logging.getLogger(name) finally: logging.setLoggerClass(OrigLoggerClass)
def test_set_logger_class(self): self.assertRaises(TypeError, logging.setLoggerClass, object) class MyLogger(logging.Logger): pass logging.setLoggerClass(MyLogger) self.assertEqual(logging.getLoggerClass(), MyLogger) logging.setLoggerClass(logging.Logger) self.assertEqual(logging.getLoggerClass(), logging.Logger)
def _get_logger(self, name): old_class = logging.getLoggerClass() logging.setLoggerClass(MPILogger.CustomLogger) logger = logging.getLogger(name) logging.setLoggerClass(old_class) return logger ########################################################################### # Standard logging functions. Log only at mpi rank 0.
def test_doesnt_affect_general_logger_class(self): self.patch(logging, 'Formatter') name = factory.make_string() get_maas_logger(name) self.assertIsNot( MAASLogger, logging.getLoggerClass())
def getLogger(name): og_class = logging.getLoggerClass() try: logging.setLoggerClass(Logger) return logging.getLogger(name) finally: logging.setLoggerClass(og_class) ## The main 'eyed3' logger