Example #1
0
    def emit(self, record: logging.LogRecord) -> None:  # type: ignore
        """Emit a new log"""
        # if we shouldn't log to cloud, don't emit
        if not context.config.logging.log_to_cloud:
            return

        # ensures emitted logs respect configured logging level
        config_level = getattr(logging, context.config.logging.level, logging.INFO)

        if record.levelno < config_level:
            return

        msg = self.format(record)
        if len(msg) > MAX_LOG_LENGTH:
            get_logger("prefect.logging").warning(
                "Received a log message of %d bytes, exceeding the limit of %d. "
                "The output will be truncated",
                len(msg),
                MAX_LOG_LENGTH,
            )
            msg = msg[:MAX_LOG_LENGTH]

        log = {
            "flow_run_id": context.get("flow_run_id"),
            "task_run_id": context.get("task_run_id"),
            "timestamp": pendulum.from_timestamp(
                getattr(record, "created", None) or time.time()
            ).isoformat(),
            "name": getattr(record, "name", None),
            "level": getattr(record, "levelname", None),
            "message": msg,
        }
        LOG_MANAGER.enqueue(log)
Example #2
0
    def emit(self, record: logging.LogRecord) -> None:  # type: ignore
        """Emit a new log"""
        # if we shouldn't log to cloud, don't emit
        if not context.config.cloud.send_flow_run_logs:
            return

        # backwards compatibility for `PREFECT__LOGGING__LOG_TO_CLOUD` which is
        # a deprecated config variable as of 0.14.20
        if not context.config.logging.get("log_to_cloud", True):
            return

        # if its not during a backend flow run, don't emit
        if not context.get("running_with_backend"):
            return

        # ensures emitted logs respect configured logging level
        config_level = getattr(logging, context.config.logging.level,
                               logging.INFO)

        if record.levelno < config_level:
            return

        msg = self.format(record)
        if len(msg) > MAX_LOG_LENGTH:
            get_logger("prefect.logging").warning(
                "Received a log message of %d bytes, exceeding the limit of %d. "
                "The output will be truncated",
                len(msg),
                MAX_LOG_LENGTH,
            )
            msg = msg[:MAX_LOG_LENGTH]

        log = {
            "flow_run_id":
            context.get("flow_run_id"),
            "task_run_id":
            context.get("task_run_id"),
            "timestamp":
            pendulum.from_timestamp(
                getattr(record, "created", None) or time.time()).isoformat(),
            "name":
            getattr(record, "name", None),
            "level":
            getattr(record, "levelname", None),
            "message":
            msg,
        }
        LOG_MANAGER.enqueue(log)
Example #3
0
    def emit(self, record: logging.LogRecord) -> None:  # type: ignore
        """Emit a new log"""
        # if we shouldn't log to cloud, don't emit
        if not context.config.cloud.send_flow_run_logs:
            return

        # if its not during a backend flow run, don't emit
        if not context.get("running_with_backend"):
            return

        # ensures emitted logs respect configured logging level
        config_level = getattr(logging, context.config.logging.level,
                               logging.INFO)

        if record.levelno < config_level:
            return

        msg = self.format(record)

        log = {
            "flow_run_id":
            context.get("flow_run_id"),
            "task_run_id":
            context.get("task_run_id"),
            "timestamp":
            pendulum.from_timestamp(
                getattr(record, "created", None) or time.time()).isoformat(),
            "name":
            getattr(record, "name", None),
            "level":
            getattr(record, "levelname", None),
            "message":
            msg,
        }

        log_size = getlogsize(log)
        if log_size > MAX_LOG_SIZE:
            warnings.warn(
                "Received a log of size %d bytes, exceeding the limit of %d. "
                "The message will be truncated." % (log_size, MAX_LOG_SIZE))
            log["message"] = msg[:MAX_LOG_SIZE - LOG_OVERHEAD]

        LOG_MANAGER.enqueue(log)
Example #4
0
def _log_record_context_injector(*args: Any, **kwargs: Any) -> logging.LogRecord:
    """
    A custom logger LogRecord Factory that injects selected context parameters into newly
    created logs.

    Args:
        - *args: arguments to pass to the original LogRecord Factory
        - **kwargs: keyword arguments to pass to the original LogRecord Factory

    Returns:
        - logging.LogRecord: the newly created LogRecord
    """
    record = _original_log_record_factory(*args, **kwargs)

    additional_attrs = context.config.logging.get("log_attributes", [])

    for attr in PREFECT_LOG_RECORD_ATTRIBUTES + tuple(additional_attrs):
        value = context.get(attr, None)
        if value or attr in additional_attrs:
            setattr(record, attr, value)

    return record