没有使用 winston 节点 js 库创建日志文件
No log files are being created using winston node js library
我正在使用 node js 并使用 Winston 库进行日志记录。以下代码不创建日志文件。
var winston = require('winston');
var logger = winston.createLogger({
transports: [
new winston.transports.File({
level: 'info',
filename: './logs/all-logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
module.exports = logger;
module.exports.stream = {
write: function(message, encoding){
logger.info(message);
}
};
logger.info("hello world");
它可以正常登录到终端:
{"message":"hello world","level":"info"}
目录结构是这样的
-test.js
-winston.js
-log
这里是你应该使用的温斯顿:
下面的代码在 /log/
目录中创建日志文件。
首先,安装 winston-daily-rotate-file、fs 和 winston 使用:
npm i winston-daily-rotate-file fs winston
创建名称为 winston.js
的文件
const fs = require("fs");
const winston = require("winston");
const logDir = "log";
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date()).toLocaleTimeString();
module.exports = logger = winston.createLogger({
transports: [
new (winston.transports.Console)({
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.align(),
winston.format.simple(),
),
level: 'info'
}),
new (require("winston-daily-rotate-file"))({
filename: `${logDir}/-results.log`,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
)
}),
new winston.transports.File({
filename: 'log/error.log',
level: 'error',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.simple(),
)
}),
]
});
现在您所要做的就是导入记录器并使用它。下面是例子。
现在在同一目录中,创建一个新文件test.js并添加以下代码:
const logger = require("./winston.js");
logger.info(`Test info Log!`);
logger.error(`Test error Log!`);
现在运行test.js使用
node test.js
希望这就是您要找的。
我正在使用 node js 并使用 Winston 库进行日志记录。以下代码不创建日志文件。
var winston = require('winston');
var logger = winston.createLogger({
transports: [
new winston.transports.File({
level: 'info',
filename: './logs/all-logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
module.exports = logger;
module.exports.stream = {
write: function(message, encoding){
logger.info(message);
}
};
logger.info("hello world");
它可以正常登录到终端:
{"message":"hello world","level":"info"}
目录结构是这样的
-test.js
-winston.js
-log
这里是你应该使用的温斯顿:
下面的代码在 /log/
目录中创建日志文件。
首先,安装 winston-daily-rotate-file、fs 和 winston 使用:
npm i winston-daily-rotate-file fs winston
创建名称为 winston.js
的文件const fs = require("fs");
const winston = require("winston");
const logDir = "log";
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date()).toLocaleTimeString();
module.exports = logger = winston.createLogger({
transports: [
new (winston.transports.Console)({
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.align(),
winston.format.simple(),
),
level: 'info'
}),
new (require("winston-daily-rotate-file"))({
filename: `${logDir}/-results.log`,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
)
}),
new winston.transports.File({
filename: 'log/error.log',
level: 'error',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.simple(),
)
}),
]
});
现在您所要做的就是导入记录器并使用它。下面是例子。
现在在同一目录中,创建一个新文件test.js并添加以下代码:
const logger = require("./winston.js");
logger.info(`Test info Log!`);
logger.error(`Test error Log!`);
现在运行test.js使用
node test.js
希望这就是您要找的。