Python logging.LogRecord() Examples
The following are 30
code examples of logging.LogRecord().
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: logging.py From fastapi-realworld-example-app with MIT License | 9 votes |
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 #2
Source File: logs_test.py From clusterfuzz with Apache License 2.0 | 6 votes |
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 #3
Source File: logging.py From scanpy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #4
Source File: setup_logging.py From sphinxcontrib-versioning with MIT License | 6 votes |
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 #5
Source File: stackdriver_task_handler.py From airflow with Apache License 2.0 | 6 votes |
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 #6
Source File: logging.py From sentry-python with BSD 2-Clause "Simplified" License | 6 votes |
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 #7
Source File: log.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
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 #8
Source File: logging.py From python-netsurv with MIT License | 6 votes |
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 #9
Source File: log.py From maubot with GNU Affero General Public License v3.0 | 6 votes |
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 #10
Source File: test_loggers.py From moler with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #11
Source File: logger.py From streamlink with BSD 2-Clause "Simplified" License | 6 votes |
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 #12
Source File: slogging.py From modelforge with Apache License 2.0 | 6 votes |
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 #13
Source File: slogging.py From fragile with MIT License | 6 votes |
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 #14
Source File: slogging.py From fragile with MIT License | 6 votes |
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 #15
Source File: logs_test.py From clusterfuzz with Apache License 2.0 | 6 votes |
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 #16
Source File: utils.py From pyquarkchain with MIT License | 6 votes |
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 #17
Source File: logging_helpers.py From dephell with MIT License | 6 votes |
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 #18
Source File: slogging.py From modelforge with Apache License 2.0 | 6 votes |
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 #19
Source File: slogging.py From modelforge with Apache License 2.0 | 6 votes |
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 #20
Source File: slogging.py From modelforge with Apache License 2.0 | 6 votes |
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 #21
Source File: logging.py From python-netsurv with MIT License | 6 votes |
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 #22
Source File: debug.py From sentry-python with BSD 2-Clause "Simplified" License | 5 votes |
def filter(self, record): # type: (LogRecord) -> bool if _client_init_debug.get(False): return True hub = Hub.current if hub is not None and hub.client is not None: return hub.client.options["debug"] return False
Example #23
Source File: utils.py From bioconda-utils with MIT License | 5 votes |
def filter(self, record: logging.LogRecord) -> bool: if record.name.startswith("bioconda_utils"): record.name = "BIOCONDA" else: record.name = record.name.split('.')[0].upper() return True
Example #24
Source File: utils.py From bioconda-utils with MIT License | 5 votes |
def filter(self, record: logging.LogRecord) -> bool: if record.name == self.func.__module__ and record.funcName == self.func.__name__: if self.cur_max_lines > 1: self.cur_max_lines -= 1 return True if self.cur_max_lines == 1 and self.trunc_msg: self.cur_max_lines -= 1 record.msg = self.trunc_msg return True return False if self.consecutive: self.cur_max_lines = self.max_lines return True
Example #25
Source File: logger.py From cornerwise with MIT License | 5 votes |
def emit(self, record: logging.LogRecord): message = self.format(record) with self.redis.pipeline() as pipe: pipe.lpush(self.topic, message) if self.nrecents: pipe.ltrim(self.topic, 0, self.nrecents-1) if self.expiration: pipe.expire(self.topic, self.expiration) pipe.execute()
Example #26
Source File: borgmatic.py From borgmatic with GNU General Public License v3.0 | 5 votes |
def load_configurations(config_filenames, overrides=None): ''' Given a sequence of configuration filenames, load and validate each configuration file. Return the results as a tuple of: dict of configuration filename to corresponding parsed configuration, and sequence of logging.LogRecord instances containing any parse errors. ''' # Dict mapping from config filename to corresponding parsed config dict. configs = collections.OrderedDict() logs = [] # Parse and load each configuration file. for config_filename in config_filenames: try: configs[config_filename] = validate.parse_configuration( config_filename, validate.schema_filename(), overrides ) except (ValueError, OSError, validate.Validation_error) as error: logs.extend( [ logging.makeLogRecord( dict( levelno=logging.CRITICAL, levelname='CRITICAL', msg='{}: Error parsing configuration file'.format(config_filename), ) ), logging.makeLogRecord( dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error) ), ] ) return (configs, logs)
Example #27
Source File: logtap.py From OpenMTC with Eclipse Public License 1.0 | 5 votes |
def emit(self, record, level = NOT_SET): if isinstance(record, LogRecord): return super(BufferingLogTap, self).emit(record) self.memhandler.buffer.append(record)
Example #28
Source File: logging.py From sentry-python with BSD 2-Clause "Simplified" License | 5 votes |
def _emit(self, record): # type: (LogRecord) -> None if not _can_record(record): return Hub.current.add_breadcrumb( _breadcrumb_from_record(record), hint={"log_record": record} )
Example #29
Source File: testing.py From opendevops with GNU General Public License v3.0 | 5 votes |
def filter(self, record: logging.LogRecord) -> bool: if record.exc_info: self.logged_stack = True message = record.getMessage() if self.regex.match(message): self.matched = True return False return True
Example #30
Source File: borgmatic.py From borgmatic with GNU General Public License v3.0 | 5 votes |
def make_error_log_records(message, error=None): ''' Given error message text and an optional exception object, yield a series of logging.LogRecord instances with error summary information. As a side effect, log each record. ''' if not error: yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=message) return try: raise error except CalledProcessError as error: yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=message) if error.output: # Suppress these logs for now and save full error output for the log summary at the end. yield log_record( levelno=logging.CRITICAL, levelname='CRITICAL', msg=error.output, suppress_log=True ) yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error) except (ValueError, OSError) as error: yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=message) yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error) except: # noqa: E722 # Raising above only as a means of determining the error type. Swallow the exception here # because we don't want the exception to propagate out of this function. pass