Knex 查询事件日志到 winston 文件。

Knex query event logs to winston file.

我想将所有 knex 查询事件保存到 winston 文件中。我发现了这样的东西 http://knexjs.org/#Interfaces-Events。它对我很有用,但现在我必须添加

 .on('query-response', function(response, obj, builder)...

对于每个 knex 查询。

我想为所有 qnex 查询添加一个全局函数。有可能的?

您可以从单独的 dbConnection 文件中导出您的 knex 对象,然后将其导入到您需要的其他文件中。在您的 dbConnection 文件中,将事件侦听器添加到 knex。像这儿: 在你的 dbConnection 文件中写下:

const knex = require('knex')({ 
    //Your db configuration here
});

knex.on('query', console.log);

module.exports = knex;

在你的其他文件中需要它并使用它。

const knex = require('/dbConnection');