Skip to content

Logging SQL queries and results #78

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

Open
MeanSquaredError opened this issue Dec 3, 2024 · 5 comments
Open

Logging SQL queries and results #78

MeanSquaredError opened this issue Dec 3, 2024 · 5 comments
Assignees

Comments

@MeanSquaredError
Copy link
Contributor

Hi,

I have a project that uses taopq and I am looking for a way to add logging of the PostgreSQL queries/results that taopq emits.

I checked the code but did not see a way to log the generated SQL queries and/or the returned results, e.g. through a calling callback. In case I am not missing something, are there any plans to add logging to the library in the future?

@d-frey
Copy link
Member

d-frey commented Dec 3, 2024

You are correct, currently there are no logging callbacks available. I hope to add some in the future, but I haven't decided on a specific interface and the exact points when a callback is called yet. Any input is appreciated, feel free to list your ideas and what callbacks you envision.

@d-frey d-frey self-assigned this Dec 3, 2024
@MeanSquaredError
Copy link
Contributor Author

Thank you for the clarification. I imagine a logging interface similar to the following:

namespace tao::pq
{

enum log_type {
    log_type_query = 0x01, 
    log_type_result = 0x02
};

using log_handler = std::function<void (log_type type, std::string_view message);

void set_log_handler (log_handler handler, log_type type = log_type_query | log_type_result);

}

The user would set the log handler by calling set_log_handler like this

void my_taopq_logger (tao::pq::log_type type, std::string_view message)
{
    // Log the taopq query or result somewhere, e.g. by writing it to std::cout    
}

// Use the default log type which gives us both the query and result
set_log_handler (my_taopq_logger);

In the future, the logging interface could be made more fine-grained, e.g. by allowing logging to be configured per-connection or maybe even per-query. However initially I would be happy to have just a way to enable/disable logging globally.

@d-frey d-frey mentioned this issue Dec 25, 2024
@anders-wind
Copy link

Even after overwriting all the log functions I still with the examples from test/log I still get a print:
NOTICE: relation "my_table" already exists, skipping
Perhaps there are still a few places which need to use the logger?

@d-frey
Copy link
Member

d-frey commented May 14, 2025

This looks like an exception, so maybe you didn't catch it and by default its what()-message is printed? Hard to tell without a minimal but complete example.

@anders-wind
Copy link

anders-wind commented May 15, 2025

I'm quite sure it's not an exception but I'll try to make a complete example tomorrow 👍

It's from a create table if not exists statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants