Skip to content

Config

PySQLXEngine has some settings that can help the developer identify problems and have a better experience when dealing with the application's logs and errors.

You can configure these settings using environment variables or LOG_CONFIG const to change logs and errors.


Keyword

  • PYSQLX_DEV_MODE: default(False) If True, the PySQLXEngine will be in development mode. In this mode, will show the SQL statements builded with the parameters. (Just for development, the SQL is not sent to the database).
  • PYSQLX_SQL_LOG: default(False) If True, the SQL statements will be printed in the console. This log is available at the INFO level of the logging.
  • PYSQLX_USE_COLOR: default(False) If True, the messages will be printed in color.
  • PYSQLX_ERROR_JSON_FMT: default(False) If True, the error messages will be printed in JSON format.

Set environment variables

export PYSQLX_DEV_MODE=1
export PYSQLX_SQL_LOG=1
export PYSQLX_USE_COLOR=1
export PYSQLX_ERROR_JSON_FMT=1
set PYSQLX_DEV_MODE=1
set PYSQLX_SQL_LOG=1
set PYSQLX_USE_COLOR=1
set PYSQLX_ERROR_JSON_FMT=1

Set LOG_CONFIG const

1
2
3
4
5
6
from pysqlx_engine import LOG_CONFIG

LOG_CONFIG.PYSQLX_DEV_MODE = True
LOG_CONFIG.PYSQLX_SQL_LOG = True
LOG_CONFIG.PYSQLX_USE_COLOR = True
LOG_CONFIG.PYSQLX_ERROR_JSON_FMT = True

Config the logger

After setting the environment variables or LOG_CONFIG const, you can configure the logger as you wish.

The PySQLXEngine logger is available at root logger.

main.py
import logging
from pysqlx_engine import PySQLXEngineSync

# set the logger level to INFO
logging.basicConfig(level=logging.INFO)

# connect to the database
db = PySQLXEngineSync(uri="postgresql://user:pass@host:port/db")
db.connect()

# run the query and see the log
db.query("SELECT * FROM valid_table") # change the 'valid_table' 
db.query("SELECT * FROM invalid_table")
main.py
import logging
from pysqlx_engine import PySQLXEngineSync, LOG_CONFIG

LOG_CONFIG.PYSQLX_DEV_MODE = True
LOG_CONFIG.PYSQLX_SQL_LOG = True
LOG_CONFIG.PYSQLX_USE_COLOR = True
LOG_CONFIG.PYSQLX_ERROR_JSON_FMT = True

# set the logger level to INFO
logging.basicConfig(level=logging.INFO)

# connect to the database
db = PySQLXEngineSync(uri="postgresql://user:pass@host:port/db")
db.connect()

# run the query and see the log
db.query("SELECT * FROM valid_table") # change the 'valid_table'
db.query("SELECT * FROM invalid_table")

Running the code

Running the code using the terminal

$ python3 main.py

Output

SQL log
INFO:root:SELECT * FROM valid_table
INFO:root:SELECT * FROM invalid_table
Error log
pysqlx_engine._core.errors.QueryError: 
{
    "code": "42P01",
    "message": "relation 'invalid_table' does not exist",
    "error": "QueryError"
}