Search code examples
pythonloggingsnowflake-cloud-data-platform

Python logger in a Snowpark Stored Procedure?


I'm trying to add a Python logger to my Snowpark Python stored procedure.

The documentation says that I must create an Event Table, and that Event Tables must be associated with an account, and that an account can be associated with only one event table at a time.

You associate an event table with your account in order to capture log entries and trace events to that table. You can associate an account with only one event table at a time. The associated event table is referred to as the active event table.

I'm working on a company Snowflake account, and I do not have admin rights.

To enable storage of log and trace event data from functions and procedures for an account, you must specify that the event table you created is the active event table for the account.

This seems to imply that as an individual developer with a restricted account, I cannot use a Python logger to log activity from my stored procedure.

I'm currently working around this by creating my own logs by constructing SQL statements and inserting into a normal table (not an event table).

But if possible I'm wondering if there's a way to still use a Python logging handler without having to send it to an event table. Or if I can construct an event table associated with one or more stored procedure logs, rather than all the logs of a particular account.

Any insight from someone familiar with Snowpark Python is much appreciated.

I tried creating an event table:

CREATE EVENT TABLE my_database.my_schema.my_events;

and then adding a test logger to my stored procedure to see if any information would be sent to that event table:

import logging

logger = logging.getLogger("mylog")

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

But when I successfully created and called my stored procedure, no new rows were added to the event table.

Likely this is because I am unable to associate the event table with my account.

But this is the only code test I've tried so far.


Solution

  • The current design for logging and events tables in Snowflake is that there can only be one events table per account, and the accountadmin (or delegated role) has to create it and grant access to other roles for reading it.

    If your accountadmin doesn't want to create this table (or grant you access to read from it), doing individual inserts will work - but you should note that the events table is better for logging at scale: Instead of making the data immediately available (as an insert would), it collects events and commits them to the table on its own schedule (to allow for higher rates of logging).

    If your accountadmin wants to grant you access to read that table, but only for the logs you have produced, then an enterprise account can set up a row-level policy matching your username or role. Otherwise you can get a similar result with a secure view.