来自函数和过程的日志记录消息
You can log messages (such as warning or error messages) from a stored procedure, UDF, or UDTF, including those you write using Snowpark APIs. You can access the logged messages from an event table (a type of predefined table that captures events, including logged messages). For a list of supported handler languages, see 支持的语言.
For example, in a Java UDF, you can use the SLF4J API (http://www.slf4j.org/) to log messages. Later, you can access those logged messages in an event table.
Note
Before you can collect log messages, you must enable telemetry data collection. When you instrument your code, Snowflake generates the data and collects it in an event table.
日志记录示例
The Python code in the following example imports the logging module, gets a logger, and logs a message at the INFO level.
Note
从用于处理输入行的方法记录的消息会针对 UDF 处理的 每行 进行记录。如果在大型表中执行 UDF,则可能会导致事件表中出现大量消息。
开始使用
若要从处理程序代码开始记录,请按照下列高级步骤操作:
-
Snowflake will use your event table to store messages logged from your handler code. An event table has columns predefined by Snowflake.
-
熟悉针对要使用的处理程序语言的日志记录 API。
see 支持的语言 for a list of handler languages, then view content about how to log from your language.
-
将日志记录代码添加到处理程序。
-
Learn how to retrieve logging data from the event table.
日志消息的级别
您可以设置日志级别,从而管理事件表中存储的日志事件数据的级别。在日志记录之前,请使用此设置来确保获取日志消息的严重性。
For more information, see Setting levels for logging, metrics, and tracing.
支持的语言
You can log messages from code written in the following languages, including when handler code is written with Snowpark APIs.
| Language / Type | Java | JavaScript | Python | Scala | SQL |
|---|---|---|---|---|---|
| Stored procedure handler | ✔ | ✔ | ✔ | ✔ | ✔ ** |
| Streamlit app | ✔ | ||||
| UDF handler (scalar function) | ✔ | ✔ | ✔ | ✔ | |
| UDTF handler (table function) | ✔ | ✔ | ✔ | ✔ * |
图例
- *:
在 Snowpark 中编写的 Scala UDTF 处理程序。
- **:
Snowflake Scripting 用于编写存储过程。
Note
Logging is not supported for Request and response translators in external functions.
从处理程序代码记录
若要记录消息,您可以使用处理程序代码语言通用的函数。Snowflake 截获消息并将其存储在您创建的事件表中。
For example, in a Java UDF, you can use the SLF4J API (http://www.slf4j.org/) to log messages. Later, you can access those logged messages in an event table.
If you plan to log messages when errors occur, you should log them from within the construct for handling errors in the language
that you are using. For example, in a Java UDF, call the method for logging a message in the catch block where you handle
the exception.
下表列出了日志记录支持的处理程序语言,以及指向通过代码记录相关内容的链接。
| Language | Logging Library | Documentation |
|---|---|---|
| Java | SLF4J API | Logging messages in Java |
| JavaScript | Snowflake JavaScript API snowflake object | Logging messages in JavaScript |
| Python | Standard Library logging module | Logging messages in Python |
| Scala | SLF4J API | Logging messages in Scala |
| Snowflake Scripting | Snowflake SYSTEM$LOG function. | Logging messages in Snowflake Scripting |
查看日志消息
You can view the log messages either through Snowsight or by querying the event table in which log entries are stored. For more information, see Viewing log messages.