2

From the mysql2 readme we read :

MySQL2 is mostly API compatible with mysqljs

Which package has an option defined as :

  • debug: Prints protocol details to stdout. Can be true/false or an array of packet type names that should be printed. (Default: false)

This option even has an example :

var connection = mysql.createConnection({debug: ['ComQueryPacket', 'RowDataPacket']});

However, setting this option with either true, "ComQueryPacket" and/or "RowDataPacket" vomits a ton load of irrelevant data; all I care is to have the SQL query and variables being sent. How can this be done with this package?

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214

1 Answers1

2

debug option only supports true/false values in mysql2. Easiest way to log all queries would be to monkey patch Connection.prototype.addCommand, example pseudocode:

let addCommand = Connection.prototype.addCommand
function loggingAddCommand(cmd) {
  if (cmd.constructor === Commands.Query) {
    console.log(`Adding query command, sql: ${cmd.sql}`);
  }
  this.addCommand(cmd);
}

connection.addCommand = loggingAddCommand;

I'll try to think about better api to make this easier. Maybe having "command" event on a connection? If we add this, above example would become conn.on('command_added', cmd => { /* ... */ })

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • Hey! Thank you for answering! If you're open to suggestion (I could even open an issue ticket on Github?), then perhaps `conn.on("afterExecute", ({ sql, params, result, error }) => { ... })` would allow for debugging. An event `beforeExecute` could also be emitted with only the `sql` and `params` options. – Yanick Rochon Oct 23 '20 at 15:05