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.

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." |
--------------------------------------------------------