I have written according to winston-mysql document. I have described a table in mysql db for storing logs but it's not working.The below is my logger.js -
const winston = require('winston');
const winston_mysql = require('winston-mysql')
var options_default = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
table : 'log',
fields : { level: 'info', meta: 'metadata', message: 'source', timestamp: 'addDate'}
};
var logger = new (winston.createLogger)({
transports: [
new winston_mysql(options_default)
]
});
module.exports = logger;
And this is my controller file where I am using winston.After customer is created it should create a log file into the db. I am not getting any error as such but its not storing into db.What mistake I am doing?
const createCustomer = async(req,res,next) => {
try{
var id = Math.floor(Math.random()*9000000) + 10000000;
var db = req.con;
const data = {
"id": id,
"first_name": req.body.first_name,
"last_name": req.body.last_name ,
"username": req.body.username,
"email": req.body.email,
"password": req.body.password,
"mobile": req.body.mobile,
"address": req.body.address,
"created_date": CurrentTime,
};
console.log(data)
const salt = await bcrypt.genSalt(20);
data.password = await bcrypt.hash(data.password, salt);
var pass = data.password;
console.log(pass)
if (!req.file.filename)
return res.status(400).send('No files were uploaded.');
let filename = fs.readdirSync('./src/template-store/temporaryImage/')
console.log(filename)
var file = req.files
let result = db.query(`INSERT INTO customer set ?`,[data],function(err,rows){
if(err){
res.json({
success:0,
message:"An error occurred",
err
})
}
else{
logger.log({level:'info',message : 'customer'}) // here I am using logger
res.json({
status:1,
message:"Inserted",
data: data
})
// mailer(req.body.username,req.body.email,req.body.password)
}
})
}
catch(error){
res.json({
status:0,
message:"An error occured",
error:error
})
}
}