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

Logging at wire-protocol level?

Open alxndrsn opened this issue 4 months ago • 3 comments

Is there a simple way to enable logging at the wire-protocol level?

I've worked out how to DIY it via changes to node_modules/pg/lib/connection.js and node_modules/pg-protocol/dist/buffer-reader.js, but maybe there's a more formal approach? I also can't see any relevant log calls in those files.

alxndrsn avatar Jul 30 '25 15:07 alxndrsn

Is the message event on Connections low-level enough for your needs?

charmander avatar Aug 02 '25 05:08 charmander

Is the message event on Connections low-level enough for your needs?

This looks really helpful, although maybe not as low-level as I may need for some purposes.

What's the correct way to enable this logging? I tried adding a 'message' event listener to my Pool instance, but no luck.

Instead I had a look at the messages like this:

--- lib/connection.js
+++ lib/connection.js
@@ -107,6 +107,7 @@
       this.emit('end')
     })
     parse(stream, (msg) => {
+      console.log('emitting message:', msg);
       var eventName = msg.name === 'error' ? 'errorMessage' : msg.name
       if (this._emitMessage) {
         this.emit('message', msg)

From the emitted msg object, how do you tell which direction each message is going?

alxndrsn avatar Aug 05 '25 08:08 alxndrsn

The message event is only for incoming messages. You would add the event listener to each Connection object:

pool.on('connect', client => {
    client.connection.on('message', …);
});

charmander avatar Aug 08 '25 07:08 charmander