I want to write sequelize log to file instead of console. All sequelize logs are being written to console, which is default.
Thanks in advance.
Check out options.logging
in the docs. The default value is
options.logging = console.log
which means all logging is passed to console.log()
. So simply change logging to a function that append messages to a log / text file. Example
var sequelize = new Sequelize('database', 'username', 'password', {
logging: myLogFunc
});
var myLogFunc = function(msg) {
}
winston is a good nodejs package for managing logs -> https://github.com/winstonjs/winston
This is the dirty way (use only in local environment):
import fs = require('fs');
const logStream = fs.createWriteStream('./sql.log', {'flags': 'a'});
const sequelize = new Sequelize('database', 'username', 'password', {
logging: msg => logStream.write(msg),
});
const sequelize = new Sequelize('sqlite::memory:', {
// Choose one of the logging options
logging: console.log, // Default, displays the first parameter of the log function call
logging: (...msg) => console.log(msg), // Displays all log function call parameters
logging: false, // Disables logging
logging: msg => logger.debug(msg), // Use custom logger (e.g. Winston or Bunyan), displays the first parameter
logging: logger.debug.bind(logger) // Alternative way to use custom logger, displays all messages
});
use any of one logger i used winston
logging: msg => logger.log('info',msg)
this worked for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With