Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

echo=True outputs as "info" instead of "debug" and feature request query_callback #81

Open
WilliamStam opened this issue Oct 25, 2023 · 0 comments

Comments

@WilliamStam
Copy link

WilliamStam commented Oct 25, 2023

im looking to keep track of all statements executed. right now iecho=True outputs to logging info.

if self._echo:
        logger.info(f"[{round((end - start) * 1000, 2)}ms] {query}")

would it be possible in future to set it to logger.debug instead?

would be very useful if you could pass in a callable for returning the exact statement executed on every statement. which would return a float for time taken and string of statement executed.

this would allow for things like being able to "slow query" log applications n stuff.

# replace echo=True
def query_callback(cursor: Cursor, query:str, timer:float):
    logger.info(f"[{timer}ms] {query}")
cursor.query_callback = query_callback

# ----
# log slow queries
def query_callback(cursor: Cursor, query:str, timer:float):
    if timer > 1000:
        logger.warning(f"[{timer}ms] {query}")
cursor.query_callback = query_callback

then in cursors.pyx

def __init__(self, connection: "Connection", echo: bool = False, query_callback = None):
    self._query_callback = query_callback 
...

if self._echo:
    logger.info(f"[{round((end - start) * 1000, 2)}ms] {query}")

if callable(self._query_callback):
    self._query_callback(self,query,round((end - start) * 1000, 2))

wouldn't break backwards compatibility since echo=True could just be a kickstarter to add a log.info() callable if no callable is supplied

def query_callback(cursor: Cursor, query:str, timer:float):
    logger.info(f"[{timer}ms] {query}")

def __init__(self, connection: "Connection", echo: bool = False, query_callback = None):
    if echo and not callable(query_callback):
        self._query_callback = query_callback 
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant