温斯顿没有将日志写入文件 [英] Winston is not writing logs to files
本文介绍了温斯顿没有将日志写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Winston中进行了两次错误和警告传输,据推测这些错误和警告写入了文件.现有的用于控制台日志记录的传输方式工作正常,我确实检查了 pm2日志
并看到了日志,但是文件传输方式并未保存任何内容.
I made two transports for errors and warnings in Winston that supposedly writes to files. The existing transport for console logging works fine, and I did check pm2 logs
and saw the logs, but the transports for files are not saving anyting.
'use strict';
const winston = require('winston');
const m = require('moment-timezone');
let logger = null;
/**
* Initializes the logger
* @param {object} configLogging
*/
module.exports.initialize = function initialize(configLogging) {
const dateFormat = 'dddd, MMMM Do YYYY, h:mm:ss a';
logger = new winston.Logger({
transports: [
new (winston.transports.Console)({
name: 'info-console',
level: configLogging.level,
colorize: true,
timestamp: function() { return m.utc().format(dateFormat); }
}),
new (winston.transports.File)({
name: 'warning-file',
filename: 'warning-file.log',
level: 'warning'
}),
new (winston.transports.File)({
name: 'error-file',
filename: 'error-file.log',
level: 'error'
})
]
});
logger.info('Starting logging service');
};
/**
* Gets the logger instance
* @returns {LoggerInstance} winLogger
*/
module.exports.get = function get() {
return logger;
};
推荐答案
请创建一个log.js文件并编写所有代码
please create one log.js file and write this all code
var winston = require('winston');
const env = process.env.NODE_ENV;
const logDir = 'logs';
const fs = require('fs');
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const now = new Date();
var logger = new(winston.Logger)({
transports: [
new winston.transports.File({
name: 'error-file',
filename: './logs/exceptions.log',
level: 'error',
json: false
}),
new(require('winston-daily-rotate-file'))({
filename: `${logDir}/-apimodules.log`,
timestamp: now,
datePattern: 'dd-MM-yyyy',
prepend: true,
json: false,
level: env === 'development' ? 'verbose' : 'info'
})
],
exitOnError: false
});
module.exports = logger;
module.exports.stream = {
write: function(message, encoding) {
logger.info(message);
console.log('message=', message);
}
};
要添加日志,请在需要使用此代码进行日志记录的所有地方使用此文件
For add log, use this file everywhere that need to log using this code
var logger = require('./path of/log.js');
logger.info('*** Requested for First log... ***');
这篇关于温斯顿没有将日志写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文