Logging messages in Snowflake Scripting¶
You can log messages from a stored procedure handler written in Snowflake Scripting by using the Snowflake SYSTEM$LOG, SYSTEM$LOG_<level> (for Snowflake Scripting) function. When you’ve set up an event table to store log entries, Snowflake stores log entries generated by your handler code in the table.
Before logging from code, be sure you have the logging level set so that the messages you want are stored in the event table. For more information, see Setting levels for logging, metrics, and tracing.
Note
Before you can begin logging messages, you must set up an event table. For more information, see Event table overview.
You can access log messages by executing a SELECT command on the event table. For more information, see Viewing log messages.
For general information about setting up logging and retrieving messages in Snowflake, see Logging messages from functions and procedures.
Snowflake Scripting example¶
Code in the following example uses the SYSTEM$LOG function to log messages at each of the supported levels. Note that a message logged from code that processes an input row will be logged for every row processed by the handler. If the handler is executed in a large table, this can result in a large number of messages in the event table.
Automatically add log messages about blocks and child jobs¶
You can automatically log the following additional information about the execution of a Snowflake Scripting stored procedure:
BEGIN/END of a Snowflake Scripting block.
BEGIN/END of a child job request.
Automatic logging is intended for the following use cases:
You want to generate the additional log messages without modifying the body of the stored procedure.
You want comprehensive information about the execution of the stored procedure.
You want more visibility into stored procedure execution to make it easier to develop and debug it without manually adding logging code in the procedure.
To automatically log these Snowflake Scripting messages for a stored procedure, set the AUTO_EVENT_LOGGING parameter
for the stored procedure to LOGGING or ALL using the ALTER PROCEDURE command. When
you set this parameter to ALL, additional trace events are also emitted automatically
for the stored procedure.
Important
The additional information is added to the event table only if the effective LOG_LEVEL is set
to TRACE. For more information, see Setting levels for logging, metrics, and tracing.
For example, create a simple table and insert data:
Next, create a stored procedure named auto_event_logging_sp. This sample stored procedure updates a table row and
then queries the table:
The following examples set the AUTO_EVENT_LOGGING parameter for the stored procedure:
Call the stored procedure:
Query the event table for messages logged by the stored procedure named auto_event_logging_sp. For each message,
print out the timestamp, log level, and text of the message.