I am trying to log errors in a file using winston, node js.
but it is giving the error as: Cannot read properties of undefined (reading 'log')
code: server.js
const path = require('path');
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const sharp=require('sharp')
const logger=require('./errorLogger')
app.use(express.static(path.join(__dirname,"../public")));
app.get('/down', (req, res) => {
outFile="output.jpg"
sharp("../public/vidgo.jpg")
.resize()
.toFile(outFile,(err)=>{
if(err) {
console.log(err)
logger.errorLogger.log('error',err) // line 37 mentioned in error
}
console.log("----")
res.download(outFile,err=>{
if(err) {
console.log(err)
logger.errorLogger.log('error',err)
}
console.log("down")
})
})
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
errorLogger.js module
const {createLogger, transports,format} = require('winston')
const errorLogger=createLogger({
transports:[
new transports.File({
filename:"errLogs",
level:"error",
format: format.combine(format.timestamp(),format.json())
})
]
})
log and errors
Error: Input file is missing: ../public/vidgo.jpg]
E:\Novo Cabs\requestLog\nodeserver\server.js:37
logger.errorLogger.log('error',err)
^
TypeError: Cannot read properties of undefined (reading 'log')
at Object.<anonymous> (E:\Novo Cabs\requestLog\nodeserver\server.js:37:32)
the file named 'errLogs' is created automatically
Add the
module.exports = logger;
to your errorLogger.js file.