Logging messages in JavaScript

You can log messages from a function or procedure handler written in JavaScript by using the snowflake object included with the Snowflake JavaScript API. When you’ve set up an event table to store log entries, Snowflake stores log entries generated by your handler code in the table. For reference about the JavaScript API, see JavaScript stored procedures API.

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.

Code in the following example uses the included snowflake object to log messages at each of the supported levels. Note that 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.

snowflake.log("info", "Information-level message");
snowflake.log("error", "Error message");
snowflake.log("warn", "Warning message");
snowflake.log("debug", "Debug message");
snowflake.log("trace", "Trace message");
snowflake.log("fatal", "Fatal message");
Copy

Adding custom attributes

When you create a log entry, you can add your own attributes in key-value pairs. Snowflake saves these custom attributes to the event table’s RECORD_ATTRIBUTES column.

To add custom attributes when calling the snowflake.log method, assemble the key-value pairs in JSON that you pass as a third argument to the log function.

Code in the following example logs a message “Logging with attributes” to the event table’s VALUE column. It also adds two custom attributes to the RECORD_ATTRIBUTES column.

CREATE OR REPLACE PROCEDURE do_logging_javascript()
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS $$
  let log_attributes = {
    "custom1": "value1",
    "custom2": "value2"
  }
  snowflake.log("info", "Logging with attributes", log_attributes)
  return "success";
$$;
Copy

Output of this log call appears in the event table as follows. Note that the RECORD_ATTRIBUTES column will include attributes that Snowflake adds automatically.

------------------------------------------------------------------
| VALUE                     | RECORD_ATTRIBUTES                  |
------------------------------------------------------------------
| "Logging with attributes" | {                                  |
|                           |   "custom1": "value1",             |
|                           |   "custom2": "value2"              |
|                           | }                                  |
------------------------------------------------------------------
Language: English