W. Reyna
W. Reyna

Reputation: 724

Using Pino as a logger for Sequelize

I am trying to use Pino with Sequelize's options.logging:

A function that gets executed every time Sequelize would log something. Function may receive multiple parameters but only first one is printed by console.log. To print all values use (...msg) => console.log(msg)

Here's what I've tried:

const pino = require('pino')
const logger = pino({ level: 'debug', prettyPrint: true })
const Sequelize = require('sequelize')

sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: '../db.sqlite3',
  logging: logger.debug()
})

But nothing is printed to the console. I know logging is working, as I logger.debug('test') works when called elsewhere in the code.

I found this library (from this issue) but I am not really sure how to use it with Sequelize.

Upvotes: 1

Views: 2067

Answers (1)

exsesx
exsesx

Reputation: 51

You do not need to call your function, you just need to pass it to Sequelize.

So basically you should write logging: msg => logger.info(msg), for example. Don't worry about losing some other parameters, console.log only uses the first one (as described in the documentation).

Simple working example:

{
  // ...

  logging: sql => logger.info(sql),

  // ...
}

Full (or almost full) clone of console.log behavior:

{
  // ...

  logging: (sql, timing) => logger.info(sql, typeof timing === 'number' ? `Elapsed time: ${timing}ms` : ''),

  // ...
}

Tip: You can use the logging option for each of your queries and they will obviously work the same way.

Tip #2: You can also use logging: logger.info.bind(logger). But you will probably search for another workaround if you choose this one :)

Upvotes: 4

Related Questions