I'm geting DB is not a constructor
for the following code. If I move the const DB = require("./db");
inside the mysql() arrow function then it works. But I can't understand why...
const winston = require("winston");
const DB = require("./db");
let logger = winston.createLogger(
{
format: winston.format.json(),
transports: [
new winston.transports.File({
filename: 'logs.txt'}),
new winston.transports.Console({
format: winston.format.colorize()})
]
});
logger.mysql = (level, msg, filename, stack)=>
{
//Create the database object.
const db = new DB();
//Open the connection.
db.open().then((result)=>
{
db.conn.query(`INSERT INTO t_logs (m_level, m_msg, m_filename, m_stack)
VALUES (?, ?, ?, ?);`, [level, msg, filename, stack], (err)=>
{
//Something went wrong.
if (err)
logger.error(err.stack);
//Close the connection.
db.close().catch((e)=>logger.error(e.stack));
});
})
.catch((e)=>
{
logger.error(e.stack);
});
};
module.exports = logger;