Python logging.LogRecord() Examples

The following are 30 code examples for showing how to use logging.LogRecord(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module logging , or try the search function .

Example 1
Project: qutebrowser   Author: qutebrowser   File: log.py    License: GNU General Public License v3.0 6 votes vote down vote up
def format(self, record: logging.LogRecord) -> str:
        record_clone = copy.copy(record)
        record_clone.__dict__.update(self._colordict)
        if record_clone.levelname in self._log_colors:
            color = self._log_colors[record_clone.levelname]
            color_str = self._colordict[color]
            record_clone.log_color = color_str  # type: ignore[attr-defined]
        else:
            record_clone.log_color = ''  # type: ignore[attr-defined]
        for field in ['msg', 'filename', 'funcName', 'levelname', 'module',
                      'name', 'pathname', 'processName', 'threadName']:
            data = str(getattr(record_clone, field))
            setattr(record_clone, field, pyhtml.escape(data))
        msg = super().format(record_clone)
        if not msg.endswith(self._colordict['reset']):
            msg += self._colordict['reset']
        return msg 
Example 2
Project: maubot   Author: maubot   File: log.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def _emit(self, record: logging.LogRecord) -> None:
        # JSON conversion based on Marsel Mavletkulov's json-log-formatter (MIT license)
        # https://github.com/marselester/json-log-formatter
        content = {
            name: value
            for name, value in record.__dict__.items()
            if name not in EXCLUDE_ATTRS
        }
        content["id"] = str(record.relativeCreated)
        content["msg"] = record.getMessage()
        content["time"] = datetime.fromtimestamp(record.created)

        if record.exc_info:
            content["exc_info"] = self.formatter.formatException(record.exc_info)

        for name, value in content.items():
            if isinstance(value, datetime):
                content[name] = value.astimezone().isoformat()
        asyncio.run_coroutine_threadsafe(self.send(content), loop=self.loop)
        self.lines.append(content) 
Example 3
Project: moler   Author: nokia   File: test_loggers.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_RawFileHandler_appends_binary_message_into_logfile():
    import os.path
    from moler.config.loggers import RAW_DATA, RawFileHandler
    cwd = os.getcwd()
    logfile_full_path = os.path.join(cwd, "tmp.raw.log")
    raw_handler = RawFileHandler(filename=logfile_full_path, mode='wb')
    binary_msg = b"1 0.000000000    127.0.0.1 \xe2\x86\x92 127.0.0.1    ICMP 98 Echo (ping) request  id=0x693b, seq=48/12288, ttl=64"
    record = logging.LogRecord(name=None, level=RAW_DATA, pathname="", lineno=0,
                               msg=binary_msg,  # only this is used
                               args=(), exc_info=None)
    raw_handler.emit(record=record)
    raw_handler.close()
    with open(logfile_full_path, mode='rb') as logfh:
        content = logfh.read()
        assert content == binary_msg
    os.remove(logfile_full_path) 
Example 4
Project: modelforge   Author: src-d   File: slogging.py    License: Apache License 2.0 6 votes vote down vote up
def check_trailing_dot(func: Callable) -> Callable:
    """
    Decorate a function to check if the log message ends with a dot.

    AssertionError is raised if so.
    """
    @functools.wraps(func)
    def decorated_with_check_trailing_dot(record: logging.LogRecord):
        if record.name not in trailing_dot_exceptions:
            msg = record.msg
            if isinstance(msg, str) and msg.endswith(".") and not msg.endswith(".."):
                raise AssertionError(
                    "Log message is not allowed to have a trailing dot: %s: \"%s\"" %
                    (record.name, msg))
        return func(record)
    return decorated_with_check_trailing_dot 
Example 5
Project: modelforge   Author: src-d   File: slogging.py    License: Apache License 2.0 6 votes vote down vote up
def getMessage(self):
        """
        Return the message for this LogRecord.

        Return the message for this LogRecord after merging any user-supplied \
        arguments with the message.
        """
        if isinstance(self.msg, numpy.ndarray):
            msg = self.array2string(self.msg)
        else:
            msg = str(self.msg)
        if self.args:
            a2s = self.array2string
            if isinstance(self.args, Dict):
                args = {k: (a2s(v) if isinstance(v, numpy.ndarray) else v)
                        for (k, v) in self.args.items()}
            elif isinstance(self.args, Sequence):
                args = tuple((a2s(a) if isinstance(a, numpy.ndarray) else a)
                             for a in self.args)
            else:
                raise TypeError("Unexpected input '%s' with type '%s'" % (self.args,
                                                                          type(self.args)))
            msg = msg % args
        return msg 
Example 6
Project: modelforge   Author: src-d   File: slogging.py    License: Apache License 2.0 6 votes vote down vote up
def formatMessage(self, record: logging.LogRecord) -> str:
        """Convert the already filled log record to a string."""
        level_color = "0"
        text_color = "0"
        fmt = ""
        if record.levelno <= logging.DEBUG:
            fmt = "\033[0;37m" + logging.BASIC_FORMAT + "\033[0m"
        elif record.levelno <= logging.INFO:
            level_color = "1;36"
            lmsg = record.message.lower()
            if self.GREEN_RE.search(lmsg):
                text_color = "1;32"
        elif record.levelno <= logging.WARNING:
            level_color = "1;33"
        elif record.levelno <= logging.CRITICAL:
            level_color = "1;31"
        if not fmt:
            fmt = "\033[" + level_color + \
                  "m%(levelname)s\033[0m:%(rthread)s:%(name)s:\033[" + text_color + \
                  "m%(message)s\033[0m"
        fmt = _fest + fmt
        record.rthread = reduce_thread_id(record.thread)
        return fmt % record.__dict__ 
Example 7
Project: modelforge   Author: src-d   File: slogging.py    License: Apache License 2.0 6 votes vote down vote up
def emit(self, record: logging.LogRecord):
        """Print the log record formatted as JSON to stdout."""
        created = datetime.datetime.fromtimestamp(record.created, timezone)
        obj = {
            "level": record.levelname.lower(),
            "msg": record.msg % record.args,
            "source": "%s:%d" % (record.filename, record.lineno),
            "time": format_datetime(created),
            "thread": reduce_thread_id(record.thread),
        }
        if record.exc_info is not None:
            obj["error"] = traceback.format_exception(*record.exc_info)[1:]
        try:
            obj["context"] = self.local.context
        except AttributeError:
            pass
        json.dump(obj, sys.stdout, sort_keys=True)
        sys.stdout.write("\n")
        sys.stdout.flush() 
Example 8
Project: dephell   Author: dephell   File: logging_helpers.py    License: MIT License 6 votes vote down vote up
def merge_record_extra(record, target, reserved):
    """
    Merges extra attributes from LogRecord object into target dictionary
    :param record: logging.LogRecord
    :param target: dict to update
    :param reserved: dict or list with reserved keys to skip
    """
    for key, value in record.__dict__.items():
        # this allows to have numeric keys
        if key not in reserved:
            if not hasattr(key, 'startswith') or not key.startswith('_'):
                target[key] = value
    return target


# https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output 
Example 9
Project: pyquarkchain   Author: QuarkChain   File: utils.py    License: MIT License 6 votes vote down vote up
def get_qkc_log_prefix(record: logging.LogRecord):
    """Returns the absl-like log prefix for the log record.
  Args:
    record: logging.LogRecord, the record to get prefix for.
  """
    created_tuple = time.localtime(record.created)
    created_microsecond = int(record.created % 1.0 * 1e6)

    level = record.levelno
    severity = get_colored_initial_for_level(level)
    end_severity = get_end_color_for_level(level)

    return "%s%02d%02d%s %02d:%02d:%02d.%06d %s:%d] " % (
        severity,
        created_tuple.tm_mon,
        created_tuple.tm_mday,
        end_severity,
        created_tuple.tm_hour,
        created_tuple.tm_min,
        created_tuple.tm_sec,
        created_microsecond,
        record.filename,
        record.lineno,
    ) 
Example 10
Project: python-netsurv   Author: sofia-netsurv   File: logging.py    License: MIT License 6 votes vote down vote up
def get_records(self, when):
        """
        Get the logging records for one of the possible test phases.

        :param str when:
            Which test phase to obtain the records from. Valid values are: "setup", "call" and "teardown".

        :rtype: List[logging.LogRecord]
        :return: the list of captured records at the given stage

        .. versionadded:: 3.4
        """
        handler = self._item.catch_log_handlers.get(when)
        if handler:
            return handler.records
        else:
            return [] 
Example 11
Project: python-netsurv   Author: sofia-netsurv   File: logging.py    License: MIT License 6 votes vote down vote up
def get_records(self, when):
        """
        Get the logging records for one of the possible test phases.

        :param str when:
            Which test phase to obtain the records from. Valid values are: "setup", "call" and "teardown".

        :rtype: List[logging.LogRecord]
        :return: the list of captured records at the given stage

        .. versionadded:: 3.4
        """
        handler = self._item.catch_log_handlers.get(when)
        if handler:
            return handler.records
        else:
            return [] 
Example 12
Project: sentry-python   Author: getsentry   File: logging.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def setup_once():
        # type: () -> None
        old_callhandlers = logging.Logger.callHandlers  # type: ignore

        def sentry_patched_callhandlers(self, record):
            # type: (Any, LogRecord) -> Any
            try:
                return old_callhandlers(self, record)
            finally:
                # This check is done twice, once also here before we even get
                # the integration.  Otherwise we have a high chance of getting
                # into a recursion error when the integration is resolved
                # (this also is slower).
                if record.name not in _IGNORED_LOGGERS:
                    integration = Hub.current.get_integration(LoggingIntegration)
                    if integration is not None:
                        integration._handle_record(record)

        logging.Logger.callHandlers = sentry_patched_callhandlers  # type: ignore 
Example 13
Project: clusterfuzz   Author: google   File: logs_test.py    License: Apache License 2.0 6 votes vote down vote up
def get_record(self):
    """Make a fake record."""
    os.environ['BOT_NAME'] = 'linux-bot'
    os.environ['TASK_PAYLOAD'] = 'fuzz fuzzer1 job1'
    record = mock.Mock(
        specset=logging.LogRecord,
        levelname='INFO',
        exc_info='exc_info',
        created=10,
        location={
            'path': 'path',
            'line': 123,
            'method': 'func'
        })
    record.name = 'logger_name'
    record.getMessage.return_value = 'log message'
    return record 
Example 14
Project: clusterfuzz   Author: google   File: logs_test.py    License: Apache License 2.0 6 votes vote down vote up
def test_format_record(self):
    """Test format a LogRecord into JSON string."""
    os.environ['FUZZ_TARGET'] = 'fuzz_target1'
    record = self.get_record()
    record.extras = {'a': 1}
    self.assertEqual({
        'message': 'log message',
        'created': '1970-01-01T00:00:10Z',
        'severity': 'INFO',
        'bot_name': 'linux-bot',
        'task_payload': 'fuzz fuzzer1 job1',
        'fuzz_target': 'fuzz_target1',
        'name': 'logger_name',
        'extras': {
            'a': 1,
        },
        'location': {
            'path': 'path',
            'line': 123,
            'method': 'func'
        }
    }, json.loads(logs.format_record(record)))

    self.mock.update_entry_with_exc.assert_called_once_with(
        mock.ANY, 'exc_info') 
Example 15
Project: fastapi-realworld-example-app   Author: nsidnev   File: logging.py    License: MIT License 6 votes vote down vote up
def emit(self, record: logging.LogRecord) -> None:  # pragma: no cover
        # Get corresponding Loguru level if it exists
        try:
            level = logger.level(record.levelname).name
        except ValueError:
            level = str(record.levelno)

        # Find caller from where originated the logged message
        frame, depth = logging.currentframe(), 2
        while frame.f_code.co_filename == logging.__file__:  # noqa: WPS609
            frame = cast(FrameType, frame.f_back)
            depth += 1

        logger.opt(depth=depth, exception=record.exc_info).log(
            level, record.getMessage(),
        ) 
Example 16
Project: fragile   Author: FragileTech   File: slogging.py    License: MIT License 6 votes vote down vote up
def check_trailing_dot(func: Callable) -> Callable:
    """
    Decorate a function to check if the log message ends with a dot.

    AssertionError is raised if so.
    """

    @functools.wraps(func)
    def decorated_with_check_trailing_dot(record: logging.LogRecord):
        if record.name not in trailing_dot_exceptions:
            msg = record.msg
            if isinstance(msg, str) and msg.endswith(".") and not msg.endswith(".."):
                raise AssertionError(
                    'Log message is not allowed to have a trailing dot: %s: "%s"'
                    % (record.name, msg)
                )
        return func(record)

    return decorated_with_check_trailing_dot 
Example 17
Project: fragile   Author: FragileTech   File: slogging.py    License: MIT License 6 votes vote down vote up
def emit(self, record: logging.LogRecord):
        """Print the log record formatted as JSON to stdout."""
        created = datetime.datetime.fromtimestamp(record.created, timezone)
        obj = {
            "level": record.levelname.lower(),
            "msg": record.msg % record.args,
            "source": "%s:%d" % (record.filename, record.lineno),
            "time": format_datetime(created),
            "thread": reduce_thread_id(record.thread),
        }
        if record.exc_info is not None:
            obj["error"] = traceback.format_exception(*record.exc_info)[1:]
        try:
            obj["context"] = self.local.context
        except AttributeError:
            pass
        json.dump(obj, sys.stdout, sort_keys=True)
        sys.stdout.write("\n")
        sys.stdout.flush() 
Example 18
Project: streamlink   Author: streamlink   File: logger.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
                   func=None, extra=None, sinfo=None):
        """
        A factory method which can be overridden in subclasses to create
        specialized LogRecords.
        """
        if name.startswith("streamlink"):
            rv = _LogRecord(name, level, fn, lno, msg, args, exc_info, func, sinfo)
        else:
            rv = _CompatLogRecord(name, level, fn, lno, msg, args, exc_info, func, sinfo)
        if extra is not None:
            for key in extra:
                if (key in ["message", "asctime"]) or (key in rv.__dict__):
                    raise KeyError("Attempt to overwrite %r in LogRecord" % key)
                rv.__dict__[key] = extra[key]
        return rv 
Example 19
Project: airflow   Author: apache   File: stackdriver_task_handler.py    License: Apache License 2.0 6 votes vote down vote up
def emit(self, record: logging.LogRecord) -> None:
        """Actually log the specified logging record.

        :param record: The record to be logged.
        :type record: logging.LogRecord
        """
        message = self.format(record)
        labels: Optional[Dict[str, str]]
        if self.labels and self.task_instance_labels:
            labels = {}
            labels.update(self.labels)
            labels.update(self.task_instance_labels)
        elif self.labels:
            labels = self.labels
        elif self.task_instance_labels:
            labels = self.task_instance_labels
        else:
            labels = None
        self._transport.send(record, message, resource=self.resource, labels=labels) 
Example 20
Project: sphinxcontrib-versioning   Author: sphinx-contrib   File: setup_logging.py    License: MIT License 6 votes vote down vote up
def format(self, record):
        """Apply little arrow and colors to the record.

        Arrow and colors are only applied to sphinxcontrib.versioning log statements.

        :param logging.LogRecord record: The log record object to log.
        """
        formatted = super(ColorFormatter, self).format(record)
        if self.verbose or not record.name.startswith(self.SPECIAL_SCOPE):
            return formatted

        # Arrow.
        formatted = '=> ' + formatted

        # Colors.
        if not self.colors:
            return formatted
        if record.levelno >= logging.ERROR:
            formatted = str(colorclass.Color.red(formatted))
        elif record.levelno >= logging.WARNING:
            formatted = str(colorclass.Color.yellow(formatted))
        else:
            formatted = str(colorclass.Color.cyan(formatted))
        return formatted 
Example 21
Project: scanpy   Author: theislab   File: logging.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def format(self, record: logging.LogRecord):
        format_orig = self._style._fmt
        if record.levelno == INFO:
            self._style._fmt = '{message}'
        elif record.levelno == HINT:
            self._style._fmt = '--> {message}'
        elif record.levelno == DEBUG:
            self._style._fmt = '    {message}'
        if record.time_passed:
            # strip microseconds
            if record.time_passed.microseconds:
                record.time_passed = timedelta(seconds=int(record.time_passed.total_seconds()))
            if '{time_passed}' in record.msg:
                record.msg = record.msg.replace('{time_passed}', str(record.time_passed))
            else:
                self._style._fmt += ' ({time_passed})'
        if record.deep:
            record.msg = f'{record.msg}: {record.deep}'
        result = logging.Formatter.format(self, record)
        self._style._fmt = format_orig
        return result 
Example 22
Project: supervisor-logging   Author: infoxchange   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def main():
    """
    Main application loop.
    """

    env = os.environ

    try:
        host = env['SYSLOG_SERVER']
        port = int(env['SYSLOG_PORT'])
        socktype = socket.SOCK_DGRAM if env['SYSLOG_PROTO'] == 'udp' \
            else socket.SOCK_STREAM
    except KeyError:
        sys.exit("SYSLOG_SERVER, SYSLOG_PORT and SYSLOG_PROTO are required.")

    handler = SysLogHandler(
        address=(host, port),
        socktype=socktype,
    )
    handler.setFormatter(PalletFormatter())

    for event_headers, event_data in supervisor_events(sys.stdin, sys.stdout):
        event = logging.LogRecord(
            name=event_headers['processname'],
            level=logging.INFO,
            pathname=None,
            lineno=0,
            msg=event_data,
            args=(),
            exc_info=None,
        )
        event.process = int(event_headers['pid'])
        handler.handle(event) 
Example 23
Project: codepost-python   Author: codepost-io   File: custom_logging.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def format_message(self, record):
        # type: (_logging.LogRecord) -> str
        """
        Format a log record as provided by the standard `logging` library, to
        have color characters for terminal output.
        """
        header = self._title.get(record.levelname, self._title.get("INFO"))
        return("{} {} (\"{}\", line {}): {}".format(
            header,
            record.module,
            self.normalize_path(record.filename),
            record.lineno,
            record.message
        )) 
Example 24
Project: codepost-python   Author: codepost-io   File: custom_logging.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def formatMessage(self, record):
        # type: (_logging.LogRecord) -> str
        return self.format_message(record=record)

# ============================================================================= 
Example 25
Project: macops   Author: google   File: gmacpyutil.py    License: Apache License 2.0 5 votes vote down vote up
def emit(self, record):
    msg = self.format(record)

    if len(msg) > 2000:
      break_loc_pre = 0
      for break_char in ['\n', ' ', '\t']:
        break_loc_pre = msg.rfind(break_char, 1000, 2000)
        break_loc_post = break_loc_pre + 1
        if break_loc_pre > 0:
          break

      if break_loc_pre < 1:
        break_loc_pre = 2000
        break_loc_post = 2000

      r1msg = msg[:break_loc_pre]
      r2msg = 'CONTINUED: %s' % msg[break_loc_post:]

      r1 = logging.LogRecord(
          record.name, record.levelno, record.pathname, record.lineno,
          r1msg, None, None, func=record.funcName)
      r2 = logging.LogRecord(
          record.name, record.levelno, record.pathname, None, r2msg, None, None)

      logging.handlers.SysLogHandler.emit(self, r1)
      self.emit(r2)
    else:
      logging.handlers.SysLogHandler.emit(self, record) 
Example 26
Project: spectree   Author: 0b01001001   File: falcon_demo.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(self, *args, **kwargs)
        lr = logging.LogRecord(None, None, '', 0, '', (), None, None)
        self.default_keys = [key for key in lr.__dict__] 
Example 27
Project: qutebrowser   Author: qutebrowser   File: quteprocess.py    License: GNU General Public License v3.0 5 votes vote down vote up
def formatted_str(self, colorized=True):
        """Return a formatted colorized line.

        This returns a line like qute without --json-logging would produce.

        Args:
            colorized: If True, ANSI color codes will be embedded.
        """
        r = logging.LogRecord(self.category, self.loglevel, '', self.line,
                              self.message, (), None)
        # Patch some attributes of the LogRecord
        if self.line is None:
            r.line = 0
        r.created = self.timestamp.timestamp()
        r.msecs = self.msecs
        r.module = self.module
        r.funcName = self.function

        format_str = log.EXTENDED_FMT
        format_str = format_str.replace('{asctime:8}',
                                        '{asctime:8}.{msecs:03.0f}')
        # Mark expected errors with (expected) so it's less confusing for tests
        # which expect errors but fail due to other errors.
        if self.expected and self.loglevel > logging.INFO:
            new_color = '{' + log.LOG_COLORS['DEBUG'] + '}'
            format_str = format_str.replace('{log_color}', new_color)
            format_str = re.sub(r'{levelname:(\d*)}',
                                # Leave away the padding because (expected) is
                                # longer anyway.
                                r'{levelname} (expected)', format_str)

        formatter = log.ColoredFormatter(format_str, log.DATEFMT, '{',
                                         use_colors=colorized)
        result = formatter.format(r)
        # Manually append the stringified traceback if one is present
        if self.traceback is not None:
            result += '\n' + self.traceback
        return result 
Example 28
Project: qutebrowser   Author: qutebrowser   File: log.py    License: GNU General Public License v3.0 5 votes vote down vote up
def filter(self, record: logging.LogRecord) -> bool:
        """Determine if the specified record is to be logged."""
        do_log = not record.msg.strip().startswith(self._pattern)
        return do_log 
Example 29
Project: qutebrowser   Author: qutebrowser   File: log.py    License: GNU General Public License v3.0 5 votes vote down vote up
def filter(self, record: logging.LogRecord) -> bool:
        """Determine if the specified record is to be logged."""
        if not self.names:
            # No filter
            return True
        elif record.levelno > logging.DEBUG and self.only_debug:
            # More important than DEBUG, so we won't filter at all
            return True
        elif record.name.split('.')[0] in self.names:
            return not self.negated
        return self.negated 
Example 30
Project: qutebrowser   Author: qutebrowser   File: log.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, capacity: int) -> None:
        super().__init__()
        self.html_formatter = None  # type: typing.Optional[HTMLFormatter]
        if capacity != -1:
            self._data = collections.deque(
                maxlen=capacity
            )  # type: typing.MutableSequence[logging.LogRecord]
        else:
            self._data = collections.deque()