我想知道是否有人可以帮助我。
我正在使用 Node 和 TypeScript,并且正在尝试使用 Winston 记录器,但无论我如何尝试,我都无法在控制台中看到日志消息。
我进行了多次尝试,但没有任何效果。
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;
即使是最简单的例子也不起作用
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;
我尝试了各种配置并研究了不同的示例,希望找到可能导致问题的差异或被忽视的设置,但无济于事。以下是显示项目设置的 package.json 的简要概述:
{
"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"
}
}
社区对发生这种情况的原因以及如何解决的任何见解、提示或建议将不胜感激。预先感谢您的时间和帮助!
您实际上不能仅使用 Winston 将自定义日志打印到控制台中。 Winston 实际上是为了保存日志而构建的;您可以稍后下载它们或通过其他服务查看它们。因此,如果您正在寻找带有仪表板的记录器,也许寻找其他更适合的东西,有一个 errsole.js 开源。