Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write sequelize log to a file, instead of console?

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.

like image 278
imv_90 Avatar asked Nov 02 '16 07:11

imv_90


3 Answers

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

like image 181
davidkonrad Avatar answered Sep 22 '22 13:09

davidkonrad


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),
}); 
like image 23
alexandre-rousseau Avatar answered Sep 21 '22 13:09

alexandre-rousseau


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

like image 37
Vivek Avatar answered Sep 20 '22 13:09

Vivek