Logging Messages in Python

You can log messages from a function or procedure handler written in Python by using logging, the logging module in Python’s standard library. 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 more information about logging levels supported by Python, refer to the logging levels documentation. Note that Snowflake treats two of the Python logging levels in a particular way:

  • The Python CRITICAL level will be treated as FATAL.

  • The Python NOTSET level will be treated as TRACE.

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.

Python Example

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

For more information about logging levels supported by Python, refer to the logging levels documentation.

CREATE OR REPLACE PROCEDURE do_logging()
RETURNS VARCHAR
LANGUAGE PYTHON
PACKAGES=('snowflake-snowpark-python')
RUNTIME_VERSION=3.8
HANDLER='do_things'
AS $$
import logging

logger = logging.getLogger("python_logger")
logger.info("Logging from Python module.")

def do_things(session):
  logger.info("Logging from Python function start.")

  try:
    throw_exception()
  except Exception:
    logger.error("Logging an error from Python handler: ")
    return "ERROR"

  return "SUCCESS"

def throw_exception():
  raise 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'] = 'python_logger'
  AND RECORD_TYPE = 'LOG';
Copy

The preceding example generates the following output.

---------------------------------------------------------------------------
| SEVERITY | MESSAGE                                                      |
---------------------------------------------------------------------------
| "INFO"   | "Logging from Python module."                 |
---------------------------------------------------------------------------
| "INFO"   | "Logging from Python function start."                           |
---------------------------------------------------------------------------
| "ERROR"  | "Logging an error from Python handler." |
---------------------------------------------------------------------------