Snowpark Checkpoints 库:日志记录

Snowpark Checkpoints 使用 Python 内置的 日志记录 (https://docs.python.org/3/library/logging.html) 模块来提供有关其内部操作的日志消息。库会发出不同 日志级别 (https://docs.python.org/3/library/logging.html#logging-levels) 的日志消息,可用于了解库的行为和诊断问题。

日志结构

Snowpark Checkpoints follows a module-level logging approach, where each Python module that needs to log messages defines its own logger, and the logger’s name matches the module’s fully qualified name.

Each Snowpark Checkpoints package defines a top-level logger, which is named after the package itself and acts as the parent for all module-level loggers within that package. The top-level logger is initialized with a NullHandler (https://docs.python.org/3/library/logging.handlers.html#nullhandler), ensuring that the logger does not produce output that it wasn't explicitly configured to produce. Any logging configuration applied to the top-level logger automatically applies to all the module loggers within that package.

Top-level logger names of Snowpark Checkpoints:

包名称

顶级记录器名称

snowpark-checkpoints-collectors

snowflake.snowpark_checkpoints_collector

snowpark-checkpoints-validators

snowflake.snowpark_checkpoints

snowpark-checkpoints-configuration

snowflake.snowpark_checkpoints_configuration

snowpark-checkpoints-hypothesis

snowflake.hypothesis_snowpark

这种模块级方法允许对日志记录输出进行细粒度控制,并确保日志从更高级别的记录器继承设置,同时发出有关其来源的精确信息。

日志记录配置

Snowpark Checkpoints 不提供默认日志记录配置。您必须在应用程序中显式配置日志记录才能查看日志消息。

如果您的应用程序已经使用 Python 内置的日志记录模块进行了日志记录配置,那么您应该能够看到 Snowpark Checkpoints 发出的日志消息,而无需任何额外的配置。如果没有日志记录配置,可以使用 basicConfig (https://docs.python.org/3/library/logging.html#logging.basicConfig) 函数或通过创建自定义配置来设置日志记录。

最好在应用程序的入口点配置一次日志记录,例如在主脚本或初始化应用程序的模块中。这样可以确保在使用任何库组件之前设置日志记录。如果在独立脚本中使用库,则应在该脚本的开头设置日志记录。以下是一些帮助您入门的示例:

基本日志记录配置

启用日志最简单快捷的方法是使用 basicConfig (https://docs.python.org/3/library/logging.html#logging.basicConfig) 函数。此功能允许您配置根记录器,该根记录器是记录模块层次结构中所有记录器的“父级”。

以下示例演示了如何设置根记录器以捕获指定日志级别及以上的日志消息并将其打印到控制台:

import logging

logging.basicConfig(
  level=logging.DEBUG, # Adjust the log level as needed
  format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
Copy

高级日志记录配置

对于更高级的日志记录配置,您可以使用 logging.config (https://docs.python.org/3/library/logging.config.html) 模块设置日志记录。此方法允许您定义自定义记录器、处理程序和格式化程序,并使用字典对其进行配置。

以下示例演示了如何使用自定义配置设置根记录器,该配置将消息记录到控制台和文件中:

import logging.config
from datetime import datetime

LOGGING_CONFIG = {
  "version": 1,
  "disable_existing_loggers": False,
  "formatters": {
      "standard": {
          "format": "{asctime} - {name} - {levelname} - {message}",
          "style": "{",
          "datefmt": "%Y-%m-%d %H:%M:%S",
      },
  },
  "handlers": {
      "console": {
          "class": "logging.StreamHandler",
          "formatter": "standard",
          "level": "DEBUG",  # Adjust the log level as needed
      },
      "file": {
          "class": "logging.FileHandler",
          "formatter": "standard",
          "filename": f"app_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log",
          "level": "DEBUG",  # Adjust the log level as needed
          "encoding": "utf-8",
      },
  },
  "root": {
      "handlers": ["console", "file"],
      "level": "DEBUG",  # Adjust the log level as needed
  },
}

logging.config.dictConfig(LOGGING_CONFIG)
Copy

启用特定包的日志记录

要为 Snowpark Checkpoints 的特定包配置日志记录而不影响其他记录器,您可以为该包使用顶级记录器名称,并根据需要应用任何自定义处理程序和格式化程序。将配置应用于顶级记录器确保所有模块级记录器都继承该配置。

以下示例演示了如何仅为以下包配置日志记录:

  • snowpark-checkpoints-collectors

  • snowpark-checkpoints-configuration

  • snowpark-checkpoints-validators

  • snowpark-checkpoints-hypothesis

import logging.config
from datetime import datetime

LOGGING_CONFIG = {
  "version": 1,
  "disable_existing_loggers": False,
  "formatters": {
      "standard": {
          "format": "{asctime} - {name} - {levelname} - {message}",
          "style": "{",
          "datefmt": "%Y-%m-%d %H:%M:%S",
      },
  },
  "handlers": {
      "console": {
          "class": "logging.StreamHandler",
          "formatter": "standard",
          "level": "DEBUG",  # Adjust the log level as needed
      },
      "file": {
          "class": "logging.FileHandler",
          "formatter": "standard",
          "filename": f"app_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log",
          "level": "DEBUG",  # Adjust the log level as needed
          "encoding": "utf-8",
      },
  },
  "loggers": {
      "snowflake.snowpark_checkpoints_collector": {
          "handlers": ["console", "file"],
          "level": "DEBUG",  # Adjust the log level as needed
          "propagate": False,
      },
      "snowflake.snowpark_checkpoints": {
          "handlers": ["console", "file"],
          "level": "DEBUG",  # Adjust the log level as needed
          "propagate": False,
      },
      "snowflake.snowpark_checkpoints_configuration": {
          "handlers": ["console", "file"],
          "level": "DEBUG",  # Adjust the log level as needed
          "propagate": False,
      },
      "snowflake.hypothesis_snowpark": {
          "handlers": ["console", "file"],
          "level": "DEBUG",  # Adjust the log level as needed
          "propagate": False,
      },
  },
}

logging.config.dictConfig(LOGGING_CONFIG)
Copy

有关 Python 日志记录模块的更多详细信息,请参阅 Python 日志记录文档 (https://docs.python.org/3/library/logging.html)。

语言: 中文