Logging Messages from Functions and Procedures

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, refer to Supported Languages.

For example, in a Java UDF, you can use the SLF4J API to log messages. Later, you can access those logged messages in an event table.

Note

Before you can begin logging messages, you must set up an event table. For more information, refer to Setting up an Event Table.

Logging Example

The Python code in the following example imports the logging module, gets a logger, and logs a message at the INFO level.

Note

A message logged from a method that processes an input row will be logged for every row processed by the UDF. If the UDF is executed in a large table, this can result in a large number of messages in the event table.

import logging

logger = logging.getLogger("mylog")

def test_logging(self):
    logger.info("This is an INFO test.")
Copy

Getting Started

To get started logging from handler code, follow these high-level steps:

  1. Set up an event table.

    Snowflake will use your event table to store messages logged from your handler code. An event table has columns predefined by Snowflake.

  2. Get acquainted with the logging API for the handler language you’ll be using.

    Refer to Supported Languages for a list of handler languages, then view content about how to log from your language.

  3. Add logging code to your handler.

  4. Learn how to retrieve logging data from the event table.

Level for Log Messages

You can manage the level of log event data stored in the event table by setting the log level. Before logging, use this setting to make sure you’re capturing the log message severity.

For more information, refer to Setting Log Level.

Supported Languages

You can log messages from function and procedure handler code written in the following languages:

Language / Type

Java

JavaScript

Python

Scala

SQL

Stored Procedures

✔ **

Scalar UDFs

UDTFs

✔ *

Legend

*

This includes Snowpark APIs used to create UDTFs.

**

Snowflake Scripting used to write stored procedures.

Note

Logging is not supported from anonymous procedures or anonymous blocks in Snowflake Scripting.

Handler Code Language Support

To log messages, you can use functions common to your handler code language. Snowflake intercepts messages and stores them in the event table you create.

For example, in a Java UDF, you can use the SLF4J API 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.

The following table lists handler languages supported for logging, along with links to content on logging from code.

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

Log Message Access

You can access log messages stored in your event table by executing a SELECT command on the event table. For more information, refer to Accessing Logged Message Data.

Costs of Logging

When you log messages from a function or procedure, Snowflake collects the messages in batches and ingests the batches into the event table.

To perform this work, Snowflake uses Snowflake-managed resources, also referred to as the serverless compute model. As is the case with other serverless features, Snowflake bills your account for the compute resource and cloud services usage needed to ingest the logged messages. Snowflake credits charged per compute-hour:

  • Snowflake-managed compute resources: 1.25

  • Cloud services: 1

These costs appear on your bill as separate line items.

To determine the credit usage for logging over time, use the EVENT_USAGE_HISTORY View.

To reduce the cost of logging:

  • Avoid logging frequently over a long period of time.

  • Set the level of messages ingested on specific objects. For example, set the log level for specific functions or procedures in a session, rather than setting the log level for all functions or procedures.