Python logging.getLogRecordFactory() Examples

The following are 8 code examples of logging.getLogRecordFactory(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logging , or try the search function .
Example #1
Source File: test_logging.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def setUp(self):
        class CheckingFilter(logging.Filter):
            def __init__(self, cls):
                self.cls = cls

            def filter(self, record):
                t = type(record)
                if t is not self.cls:
                    msg = 'Unexpected LogRecord type %s, expected %s' % (t,
                            self.cls)
                    raise TypeError(msg)
                return True

        BaseTest.setUp(self)
        self.filter = CheckingFilter(DerivedLogRecord)
        self.root_logger.addFilter(self.filter)
        self.orig_factory = logging.getLogRecordFactory() 
Example #2
Source File: test_logging.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def setUp(self):
        class CheckingFilter(logging.Filter):
            def __init__(self, cls):
                self.cls = cls

            def filter(self, record):
                t = type(record)
                if t is not self.cls:
                    msg = 'Unexpected LogRecord type %s, expected %s' % (t,
                            self.cls)
                    raise TypeError(msg)
                return True

        BaseTest.setUp(self)
        self.filter = CheckingFilter(DerivedLogRecord)
        self.root_logger.addFilter(self.filter)
        self.orig_factory = logging.getLogRecordFactory() 
Example #3
Source File: test_logging.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def setUp(self):
        class CheckingFilter(logging.Filter):
            def __init__(self, cls):
                self.cls = cls

            def filter(self, record):
                t = type(record)
                if t is not self.cls:
                    msg = 'Unexpected LogRecord type %s, expected %s' % (t,
                            self.cls)
                    raise TypeError(msg)
                return True

        BaseTest.setUp(self)
        self.filter = CheckingFilter(DerivedLogRecord)
        self.root_logger.addFilter(self.filter)
        self.orig_factory = logging.getLogRecordFactory() 
Example #4
Source File: log.py    From conjure-up with MIT License 5 votes vote down vote up
def setup_logging(app, logfile, debug=True):
    old_factory = logging.getLogRecordFactory()

    def spell_record_factory(*args, **kwargs):
        record = old_factory(*args, **kwargs)
        if record.name != 'conjure-up':
            record.filename = '{}: {}'.format(record.name, record.filename)
        spell_name = app.config.get('spell', consts.UNSPECIFIED_SPELL)
        record.name = 'conjure-up/{}'.format(spell_name)
        return record

    logging.setLogRecordFactory(spell_record_factory)

    cmdslog = TimedRotatingFileHandler(logfile,
                                       when='D',
                                       interval=1,
                                       backupCount=7)
    cmdslog.setFormatter(logging.Formatter(
        "%(asctime)s [%(levelname)s] %(name)s - "
        "%(filename)s:%(lineno)d - %(message)s"))

    root_logger = logging.getLogger()
    app_logger = logging.getLogger('conjure-up')

    if debug:
        app_logger.setLevel(logging.DEBUG)
        root_logger.setLevel(logging.DEBUG)
    else:
        # always use DEBUG level for app, for now
        app_logger.setLevel(logging.DEBUG)
        root_logger.setLevel(logging.INFO)

    root_logger.addHandler(cmdslog)
    if os.path.exists('/dev/log'):
        st_mode = os.stat('/dev/log').st_mode
        if stat.S_ISSOCK(st_mode):
            syslog_h = SysLogHandler(address='/dev/log')
            syslog_h.set_name('conjure-up')
            app_logger.addHandler(syslog_h)

    return app_logger 
Example #5
Source File: logging_tests.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_automatic_log_record_factory_install(elasticapm_client):
    """
    Use the elasticapm_client fixture to load the client, which in turn installs
    the log_record_factory. Check to make sure it happened.
    """
    transaction = elasticapm_client.begin_transaction("test")
    with capture_span("test") as span:
        record_factory = logging.getLogRecordFactory()
        record = record_factory(__name__, logging.DEBUG, __file__, 252, "dummy_msg", [], None)
        assert record.elasticapm_transaction_id == transaction.id
        assert record.elasticapm_trace_id == transaction.trace_parent.trace_id
        assert record.elasticapm_span_id == span.id
        assert record.elasticapm_labels 
Example #6
Source File: logging.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def log_record_factory(wrapped, instance, args, kwargs):
    """
    Decorator, designed to wrap the python log record factory (fetched by
    logging.getLogRecordFactory), adding the same custom attributes as in
    the LoggingFilter provided above.

    :return:
        LogRecord object, with custom attributes for APM tracing fields
    """
    record = wrapped(*args, **kwargs)
    return _add_attributes_to_log_record(record) 
Example #7
Source File: nemo_logging.py    From NeMo with Apache License 2.0 5 votes vote down vote up
def _define_logger(self):

        # Use double-checked locking to avoid taking lock unnecessarily.
        if self._logger is not None:
            return self._logger

        with self._logger_lock:
            try:
                self._logger = _logging.getLogger("nemo_logger")
                # By default, silence all loggers except the logger for rank 0
                self.remove_stream_handlers()
                if get_envbool(NEMO_ENV_VARNAME_TESTING, False):
                    old_factory = _logging.getLogRecordFactory()

                    def record_factory(*args, **kwargs):
                        record = old_factory(*args, **kwargs)
                        record.rank = get_envint("RANK", 0)
                        return record

                    _logging.setLogRecordFactory(record_factory)
                    self.add_stream_handlers(formatter=DebugNeMoFormatter)
                elif get_envint("RANK", 0) == 0:
                    self.add_stream_handlers()

            finally:
                level = Logger.INFO
                if get_envbool(NEMO_ENV_VARNAME_TESTING, False):
                    level = Logger.DEBUG
                self.set_verbosity(verbosity_level=level)

        self._logger.propagate = False 
Example #8
Source File: ch16_ex6.py    From Mastering-Object-Oriented-Python-Second-Edition with MIT License 5 votes vote down vote up
def __init__(self) -> None:
        self.user: Optional[str] = None
        self.previous = logging.getLogRecordFactory()