tobinbradley/dirt-simple-postgis-http-api

Log more specific database errors

Closed this issue ยท 3 comments

Hello again!

I am deploying Dirt to a PaaS (Render) and overall it's pretty slick to be able to point it at this repo and deploy by setting some environment variables! Lots of time saved with this new config approach ๐ŸŽ‰

One thing I'm working through is that Dirt is having trouble connecting to my database and in order to dig in, I'm having to add some error logging to a forked copy and redeploy the app, which takes time. I've run into this before where Dirt will return a 500: Database connection error and it's a little opaque to me what's going wrong.

So, this is a somewhat hazy ask to add a server-side console.error to spit out more details when there's a problem connecting to the database. I'd be happy to take a pass at a PR on this, if it sounds helpful. Also would love to know if there's a better debugging strategy I'm not thinking of!

Let me take a look at that. It would probably be better to integrate that with fasify's logger than to console.xxx it.

The reason why it only returns a vague 500 to the client is the actual db connection error message can leak info you probably want to keep under wraps - db server name, database name, db user name, etc. But it would be handy to log that on the server,

I might make the SERVER_LOGGER a level rather than just on or off. Fastify's info logger is the default and it's noisy.

I made a 4.3 release which uses fastify's logger to log database error connections. That happens in each route by adding an explicit request.log.error(err) on database connection error. When you set the SERVER_LOGGER env variable it'll be there.

While I was fiddling with that, I made the SERVER_LOGGER optionally set the log level (setting it to true now sets the default logging level, which is info). Set it to 'error' to avoid lots of noise. I also added a SERVER_LOGGER_PATH env variable so you can log to a file rather than the console, in case dirt is running in an environment where seeing the console is impossible or a hassle. See the updated README for details.

Give it a whirl and see if it helps.

This is awesome @tobinbradley! I set POSTGRES_CONNECTION=foo and I'm seeing the exact error message now. Love that the logging levels are configurable via env vars now, too. Thanks so much for the quick response on this!