JuliaDatabases / LibPQ.jl

A Julia wrapper for libpq
MIT License
217 stars 51 forks source link

Can't suppress LibPQ's error output #283

Closed nguiard closed 1 year ago

nguiard commented 1 year ago

Hi, thanks a lot for this library!

I have a bit of code that, by design, tries to connect to a non-existent database. I expect an error so I put it in a try/catch block, but libpq's red error message is still printed to screen. To be clear, the rest of the code works, it's just a bit annoying that users would see a red message that actually shouldn't be treated as an error.

Even with Suppressor.jl macros or setting throw_error=false, the message still gets through. Any idea how I could disable that temporarily?

Minimal example:

using LibPQ

try
    c = LibPQ.Connection("dbname=x host=/notexists")
catch e
    # do nothing
end

output (in red):

[error | LibPQ]: connection to server on socket "/notexists/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?
iamed2 commented 1 year ago

LibPQ.jl uses https://github.com/invenia/Memento.jl for logging (which I think we can remove at some point), so you can use functions from that package to suppress logs.

You can either temporarily set the log level for LibPQ.jl for your function:

setlevel!(getlogger("LibPQ"), "critical") do 
    mything()
end

set the log level globally for your application with a setlevel!(getlogger("LibPQ"), "critical"), or use Memento filters (I'd have to hunt through Memento for a good example of how to do that).

nguiard commented 1 year ago

Oh thanks a lot!