node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

add optional error logging mechanism

Open sniklaus opened this issue 11 years ago • 6 comments

It would be nice to have an optional error logging mechanism within the client, such that all occurred errors are conveniently logged into a file. This should be fairly straightforward to implement and be beneficial to the entire community.

sniklaus avatar Apr 03 '15 16:04 sniklaus

You can use Winston for this: https://github.com/winstonjs/winston , let's keep node-postgres clean.

ngervasi avatar Apr 03 '15 16:04 ngervasi

I am afraid that this does not solve the initial problem. There still needs to be some sort of integration into node-postgres as a potential user probably neither wants to override the .query(...) prototype to implement the logging nor add a logging mechanism to every query within the application.

sniklaus avatar Apr 03 '15 17:04 sniklaus

Well, I use quite a complex logging strategy (different loggers for console, error file and queries log) and since I use the connections pool I have to handle the error anyway so it's not that hard to do logging on my own:

client.query(query, params, function(err, result) {
                    if(err) {
                        done();
                        logger.error('Query error: '+err.message);
                        loggerConsole.error(err);
                        handleError();
                    }

I think that most people, especially in complex apps are doing the same but if you feel like implementing that as an extra option (off by default) you'll have my vote.

ngervasi avatar Apr 03 '15 23:04 ngervasi

This is similar to #721, the only difference is logging errors vs the original SQL statement. In both cases I don't think this belongs in the core driver. It's very easy to add this via a helper module.

The pg-db module (written by me) allows you to register event listeners for various actions (execution, commit, rollback, etc). If there's an error executing a query, the executeComplete includes it in the event data so it's easy to add a logging step there.

The only catch I see is that if a separate module, say an ORM or some other module directly atop node-postgres, doesn't have logging hooks built into it. Then there's no way to add in the logging calls. On the other hand if you're not controlling the pool and/or creation of the Client objects then you'd have to store the event listeners globally (yuck...) and have them apply across the board.

sehrope avatar Apr 04 '15 13:04 sehrope

This excellent idea prompted me to implement it within my pg-promise library right away. Query error handling should definitely be centralized, it is too awkward to deal with in-line, no matter if your code is built on callbacks or promises.

@CodeRect , thanks for the idea!

vitaly-t avatar Apr 06 '15 05:04 vitaly-t

I agree with @sehrope - logging does not belong within this module. It's going to be separate and different for every application built that uses node-postgres. The best thing to do is to centralize your access to the database through some db.js or other file you control. Within this module export a query function or something...you can then log anything you want to within your code, and do other things like timeout queries, remove passwords from logs, whatever your app needs. This is kinda accepted practice within any application that runs database queries. Sprinkling calls directly to a database client driver throughout your codebase makes it hard to make sweeping changes and database clients aren't usually the most terse way to interact with things. You can also check out https://github.com/brianc/node-pg-query which has a before hook you can use to log queries - I generally use that module when building apps.

brianc avatar Apr 20 '15 14:04 brianc