我正在尝试将JSON日志写入按大小旋转的文件中。我尝试了以下代码,但未按预期方式调用“ formmatter”。
import path from "path";
import winston from "winston";
require('winston-logrotate');
const myFormat = function(options) {
console.log("formatter was called");
var noEndOfLineMessage = options.message.substring(0, options.message.length-1);
return `{"data-collection":{"graph-proxy":{"level":"${options.level}","message":${noEndOfLineMessage}}}}`;
};
export const createFileLogger = () => {
let filepath = path.join(GRAPH_LOG_PATH , GRAPH_LOG_NAME );
console.log("Building logger.... with " + filepath);
let rotate_transformer = new (winston.transports.Rotate)({
file: filepath, // this path needs to be absolute
colorize: false,
json: true,
size: 1000,
keep: MAX_LOG_FILES,
handleExceptions: true,
compress: false,
formatter: myFormat
});
rotate_transformer.formatter = myFormat;
var win_logger = new (winston.Logger)({
options: { format: myFormat},
transports: [rotate_transformer] });
win_logger.stream = {
write: function(message, encoding){
win_logger.info(message);
}
};
return win_logger
};
export default createFileLogger;
我正在使用
"winston": "2.4.4",
"winston-logrotate": "1.3.0"
所以,我看到的问题是,当我使用另一台变压器时,“ formmatter”没有被调用,但旋转变压器却无法使用。
感谢任何帮助