I wonder if anyone can help me with this.
I'm using Node and TypeScript and I'm trying to use the Winston logger, but no matter how much I try, I can't see the log messages in the console.
I made several attempts and nothing worked.
import { createLogger, format, transports } from 'winston';
// Creating the logger with enhanced formatting and transports
const logger = createLogger({
level: 'info',
format: format.combine(
format.colorize(), // Colorizes the output for better visibility
format.timestamp(), // Adds timestamp to each log message
format.printf(({ timestamp, level, message }) => `[${timestamp}] ${level}: ${message}`) // Custom log format using destructuring
),
transports: [
new transports.Console() // Ensuring log messages are output to the console
]
});
// Testing the logger
logger.info('Hello world!');
logger.info('This is a test log message.');
// Exporting the logger for use in other modules
export default logger;
Not even the simplest example works
import winston from "winston";
const logger = winston.createLogger({
transports: [new winston.transports.Console()],
});
logger.error("Error message");
logger.warn("Warning message");
logger.info("Info message");
logger.verbose("Verbose message");
logger.debug("Debug message");
logger.silly("Silly message");
export default logger;
I've tried various configurations and looked into different examples, hoping to find a discrepancy or an overlooked setting that might be causing the issue, but to no avail. Here’s a brief overview of the package.json showing the project setup:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc -p tsconfig.json",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.2",
"@types/nodemailer": "^6.4.15",
"jest": "^29.7.0",
"prisma": "^5.12.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.4.3"
},
"dependencies": {
"@prisma/client": "^5.12.0",
"@sendgrid/mail": "^8.1.3",
"axios": "^1.6.8",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"firebase": "^10.11.0",
"nodemailer": "^6.9.13",
"winston": "^3.13.0",
"winston-daily-rotate-file": "^5.0.0"
}
}
Any insights, tips, or suggestions from the community on why this could be happening and how to resolve it would be highly appreciated. Thank you in advance for your time and help!
You can't really just print the custom logs into the console with Winston. Winston was actually built to save logs; you can download them later or look at them through some other service. So, if you are looking for a logger with a dashboard, maybe look for something else that fits better, there is one errsole.js opensource.