Winston: Circular structure to JSON: Sequelize logging

Created on 5 Nov 2017  ·  3Comments  ·  Source: winstonjs/winston

Code:

const winston = require('winston');

const sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
    host: config.db.host,
    dialect: config.db.dialect,
    logging: winston.info // Not work it
});

Versions:

winston: 3.0.0-rc1
sequelize: 4.22.2
mysql2: 1.4.2

Error stack:

Unhandled rejection TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at Format.transform (/demo/node_modules/logform/json.js:13:24)
    at DerivedLogger._transform (/demo/node_modules/winston/lib/winston/logger.js:218:25)
    at DerivedLogger.Transform._read (_stream_transform.js:167:10)
    at DerivedLogger.Transform._write (_stream_transform.js:155:12)
    at doWrite (_stream_writable.js:331:12)
    at writeOrBuffer (_stream_writable.js:317:5)
    at DerivedLogger.Writable.write (_stream_writable.js:243:11)
    at DerivedLogger.log (/demo/node_modules/winston/lib/winston/logger.js:174:8)
    at DerivedLogger.(anonymous function) [as info] (/demo/node_modules/winston/lib/winston/create-logger.js:54:16)
    at winston.(anonymous function) (/demo/node_modules/winston/lib/winston.js:84:34)
    at Sequelize.log (/demo/node_modules/sequelize/lib/sequelize.js:1062:23)
    at Query.run (/demo/node_modules/sequelize/lib/dialects/mysql/query.js:39:22)
    at retry (/demo/node_modules/sequelize/lib/sequelize.js:543:32)
    at /demo/node_modules/retry-as-promised/index.js:39:21
    at Promise._execute (/demo/node_modules/bluebird/js/release/debuggability.js:300:9)
    at Promise._resolveFromExecutor (/demo/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/demo/node_modules/bluebird/js/release/promise.js:79:10)
    at retryAsPromised (/demo/node_modules/retry-as-promised/index.js:29:10)
    at Promise.try.then.connection (/demo/node_modules/sequelize/lib/sequelize.js:543:14)
    at tryCatcher (/demo/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/demo/node_modules/bluebird/js/release/promise.js:512:31)

Solution alternative
Used
circular-json
instance of
https://github.com/winstonjs/logform/blob/master/json.js#L13

Most helpful comment

I believe this is related to sequelize and not winston.
We ran into this issue after upgrading sequelize to version 4.
I think, from this version, a logging function is called with two arguments sql and some sequelize object (I can't remember what the object was exactly). winston tries to log every argument passed so it tried sequelize object too, what is not meant to be logged.

To fix this try to wrap winston log call into another function.

const winston = require('winston');

const sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
    host: config.db.host,
    dialect: config.db.dialect,
    logging: function(sql, sequelizeObject) {
        winston.info(sql); // Not work it
    }
});

All 3 comments

I believe this is related to sequelize and not winston.
We ran into this issue after upgrading sequelize to version 4.
I think, from this version, a logging function is called with two arguments sql and some sequelize object (I can't remember what the object was exactly). winston tries to log every argument passed so it tried sequelize object too, what is not meant to be logged.

To fix this try to wrap winston log call into another function.

const winston = require('winston');

const sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
    host: config.db.host,
    dialect: config.db.dialect,
    logging: function(sql, sequelizeObject) {
        winston.info(sql); // Not work it
    }
});

Thanks @matuszeman!

Thanks a bunch @matuszeman, I was scratching my head for a while with this!

Was this page helpful?
0 / 5 - 0 ratings