Logging Messages in Java

You can log messages from a function or procedure handler written in Java by using the SLF4J 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.

You can use the SLF4J API included with the Snowflake Telemetry library included on Snowflake. To do so, include the following value in the PACKAGES clause when you create the function or procedure: com.snowflake:telemetry:latest.

For information on including the Telemetry library when packaging your code with Maven, refer to Setting Up Your Java and Scala Environment to Use the Telemetry Class.

Note

Using the Snowflake Telemetry Library adds other libraries to your function or procedure’s execution environment. For more information, refer to Snowflake Telemetry Package Dependencies.

Note

SLF4J does not support logging messages at the FATAL level. For handlers written in Java or Scala, the FATAL level is treated as the ERROR level.

For example, if you set the LOG_LEVEL parameter to FATAL, ERROR-level messages from a Java or Scala handler are ingested.

For general information about setting up logging and retrieving messages in Snowflake, refer to Logging Messages from Functions and Procedures.

Before logging from code, you must:

  • Set up an event table to collect messages logged from handler code.

    For more information, refer to Setting up an Event Table.

  • Be sure you have the logging level set so that the messages you want are stored in the event table.

    For more information, refer to Setting log level.

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, call methods of the slf4j fluent API, such as Logger.atInfo and Logger.atError. Use these methods to set key-value pairs in the log entry. Each returns an org.slf4j.spi.LoggingEventBuilder, which you can use to set the log message.

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

CREATE OR REPLACE PROCEDURE do_logging_java()
RETURNS VARCHAR
LANGUAGE JAVA
RUNTIME_VERSION = '11'
PACKAGES = ('com.snowflake:telemetry:latest','com.snowflake:snowpark:latest')
HANDLER = 'JavaLoggingHandler.doThings'
AS
$$
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import com.snowflake.snowpark_java.Session;

  public class JavaLoggingHandler {
    private static Logger logger = LoggerFactory.getLogger(JavaLoggingHandler.class);

    public String doThings(Session session) {
      logger.atInfo().addKeyValue("custom1", "value1").setMessage("Logging with attributes").log();
      return "SUCCESS";
    }
  }
$$;
Copy

Output of this Logger.atInfo 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",             |
|                           |   "thread.name": "Thread-5"        |
|                           | }                                  |
------------------------------------------------------------------

Java Example

Code in the following example imports references the Snowflake Telemetry library and from it gets a logger. It logs a message at the INFO level. It also logs an error for an exception.

For more information about the methods you can use to log at specific levels, refer to SLF4J methods.

CREATE OR REPLACE PROCEDURE do_logging()
RETURNS VARCHAR
LANGUAGE JAVA
RUNTIME_VERSION = '11'
PACKAGES=('com.snowflake:snowpark:latest', 'com.snowflake:telemetry:latest')
HANDLER = 'JavaLoggingHandler.doThings'
AS
$$
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import com.snowflake.snowpark_java.Session;

  public class JavaLoggingHandler {
    private static Logger logger = LoggerFactory.getLogger(JavaLoggingHandler.class);

    public JavaLoggingHandler() {
      logger.info("Logging from within the constructor.");
    }

    public String doThings(Session session) {
      logger.info("Logging from method start.");

      try {
        throwException();
      } catch (Exception e) {
        logger.error("Logging an error: " + e.getMessage());
        return "ERROR";
      }
      return "SUCCESS";
    }

    // Simulate a thrown exception to catch.
    private void throwException() throws Exception {
      throw new Exception("Something went wrong.");
    }
  }
$$
;
Copy

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

Code in the following example queries the event table where the log messages are stored. The query reports on the severity and message of each log entry from the handler class.

SET event_table_name='my_db.public.my_event_table';

SELECT
  RECORD['severity_text'] AS SEVERITY,
  VALUE AS MESSAGE
FROM
  IDENTIFIER($event_table_name)
WHERE
  SCOPE['name'] = 'JavaLoggingHandler'
  AND RECORD_TYPE = 'LOG';
Copy

The preceding example generates the following output.

--------------------------------------------------------
| SEVERITY | MESSAGE                                   |
--------------------------------------------------------
| "INFO"   | "Logging from within the constructor."    |
--------------------------------------------------------
| "INFO"   | "Logging from method start."              |
--------------------------------------------------------
| "ERROR"  | "Logging an error: Something went wrong." |
--------------------------------------------------------