Sequelize与log4js结合使用

Sequelize与log4js结合使用

1.如何将Sequelize的日志通过log4js进行输出。

log.js

const log4js = require('log4js');
const config = require('config');

log4js.configure({
    appenders: {
        deepLink: {
            type: 'file', filename: config.get("log.filename")
        },
        stdout: {
            type: 'stdout'
        }
     },
    categories: {
        default: {
            appenders: ['deepLink','stdout'],
            level: config.get("log.level")
        }
    }
});

const logger = log4js.getLogger('default');

//重新写了info方法
function info(message){
    logger.info(message)
}

exports.logger = logger
exports.log4js = log4js
exports.info = info

database.js

const Sequelize = require('sequelize')
const config = require('config');
const log = require('../log/log')

module.exports = new Sequelize(config.get("mysql.database"), config.get("mysql.username"), config.get("mysql.password"), {
    host: config.get("mysql.host"),
    dialect: 'mysql',
    pool: {
        max: config.get("mysql.pool.max"),
        min: config.get("mysql.pool.min"),
        acquire: config.get("mysql.pool.acquire"),
        idle: config.get("mysql.pool.idle")
    },
    //如果选择log.logger.info,会有Cannot read property 'isLevelEnabled' of null的异常。
    logging:log.info
})

你可能感兴趣的:(nodejs)