Snowflake
Opinionated Snowflake cursor for logging Snowflake execution calls with parameter obfuscation options.
from snowflake.connector import connect
from prefecto.ext.snowflake import LogCursor
with connect(...).cursor(LogCursor) as cursor:
cursor.execute(
"SELECT * FROM table WHERE id = %(id)s AND secret = %(secret)s",
params={"id": 123, "secret": "shhh"},
obfuscate_params=["secret"],
command_id="secret-selection",
)
INFO - [secret-selection] Beginning command.
DEBUG - [secret-selection] Executing command:
SELECT * FROM table WHERE id = 123 AND secret = ****
INFO - [secret-selection] Command executed successfully.
CommandLogAdapter
Bases: LoggerAdapter
A logging adapter that prepends the command ID to the log messages.
LogCursor
Bases: SnowflakeCursor
A Snowflake cursor that logs command executions. The execute
method has additional parameters for altering log behavior.
Example:
```python
from snowflake.connector import connect
from prefecto.ext.snowflake import LogCursor
c = connect(...).cursor(LogCursor)
c: LogCursor
c.execute("SELECT * FROM table")
```
Produces logs like
```txt
INFO - [rustic-human] Beginning command.
DEBUG - [rustic-human] Executing command:
SELECT * FROM table
INFO - [rustic-human] Command executed successfully.
```
execute(command, params=None, _bind_stage=None, timeout=None, _exec_async=False, _no_retry=False, _do_reset=True, _put_callback=None, _put_azure_callback=None, _put_callback_output_stream=sys.stdout, _get_callback=None, _get_azure_callback=None, _get_callback_output_stream=sys.stdout, _show_progress_bar=True, _statement_params=None, _is_internal=False, _describe_only=False, _no_results=False, _is_put_get=None, _raise_put_get_error=True, _force_put_overwrite=False, file_stream=None, *, logger=None, command_id=None, obfuscate_params=False, level=logging.DEBUG)
Executes a command on the Snowflake connection after logging it.
Parameters: |
|
---|
Returns: |
|
---|
Example:
```python
import logging
from snowflake.connector import connect
from prefecto.ext.snowflake import LogCursor
c = connect(...).cursor(LogCursor)
c.execute("SELECT * FROM table")
```
Produces logs like
```txt
INFO - [rustic-human] Beginning command.
DEBUG - [rustic-human] Executing command:
SELECT * FROM table
INFO - [rustic-human] Command executed successfully.
```
And tracebacks like
```txt
ERROR - [rustic-human] Command failed.
Traceback (most recent call last):
...
snowflake.connector.errors.ProgrammingError: ...
[rustic-human] Command failed
SELECT * FROM table
```
You can also obfuscate parameters:
```python
c.execute("SELECT * FROM table WHERE id = %(id)s", params={"id": 123}, obfuscate_params=True)
c.execute("SELECT * FROM table WHERE id = %(id)s AND %(secret)s", params={"id": 123, "secret": "shhh"}, obfuscate_params=["secret"])
```
```txt
INFO - [delightful-octopus] Beginning command.
DEBUG - [delightful-octopus] Executing command:
SELECT * FROM table WHERE id = ****
INFO - [delightful-octopus] Command executed successfully.
INFO - [super-athlete] Beginning command.
DEBUG - [super-athlete] Executing command:
SELECT * FROM table WHERE id = 123 AND secret = ****
INFO - [super-athlete] Command executed successfully.
```
execute(c, command, params=None, _bind_stage=None, timeout=None, _exec_async=False, _no_retry=False, _do_reset=True, _put_callback=None, _put_azure_callback=None, _put_callback_output_stream=sys.stdout, _get_callback=None, _get_azure_callback=None, _get_callback_output_stream=sys.stdout, _show_progress_bar=True, _statement_params=None, _is_internal=False, _describe_only=False, _no_results=False, _is_put_get=None, _raise_put_get_error=True, _force_put_overwrite=False, file_stream=None, *, logger=None, command_id=None, obfuscate_params=False, level=logging.DEBUG)
Executes a command on the Snowflake connection after logging it.
Parameters: |
|
---|
Returns: |
|
---|
Example:
```python
import logging
from snowflake.connector import connect
from prefecto.ext.snowflake import execute
c = connect(...).cursor()
execute(c, "SELECT * FROM table")
```
Produces logs like
```txt
INFO - [rustic-human] Beginning command.
DEBUG - [rustic-human] Executing command:
SELECT * FROM table
INFO - [rustic-human] Command executed successfully.
```
And tracebacks like
```txt
ERROR - [rustic-human] Command failed.
Traceback (most recent call last):
...
snowflake.connector.errors.ProgrammingError: ...
[rustic-human] Command failed
SELECT * FROM table
```
You can also obfuscate parameters:
```python
execute(c, "SELECT * FROM table WHERE id = %(id)s", params={"id": 123}, obfuscate_params=True)
execute(c, "SELECT * FROM table WHERE id = %(id)s AND %(secret)s", params={"id": 123, "secret": "shhh"}, obfuscate_params=["secret"])
```
```txt
INFO - [delightful-octopus] Beginning command.
DEBUG - [delightful-octopus] Executing command:
SELECT * FROM table WHERE id = ****
INFO - [delightful-octopus] Command executed successfully.
INFO - [super-athlete] Beginning command.
DEBUG - [super-athlete] Executing command:
SELECT * FROM table WHERE id = 123 AND secret = ****
INFO - [super-athlete] Command executed successfully.
```