我是Node.js的新手,几乎没有JavaScript的背景知识(我不是开发人员,但是我目前需要创建一个小型聊天机器人以进行测试)。
由于使用了Node.js和LUIS中的Microsoft Bot框架,我创建了一个聊天机器人。效果很好,但我想实现一个不错的日志记录功能并发现Winston。
记录发送和接收给机器人用户的消息的效果很好,因为它是自定义功能,我专门要求它们通过Winston登录。不幸的是,我不知道(我已经查看了Google,Stackoverflow,winston文档,Botbuilder文档...)如何要求模块(BotBuilder,Recognizer等)使用我的自定义记录器。>
这是我得到的例子:
[2018-11-12 10:17:47]-(info) Server's online
WARN: ChatConnector: receive - emulator running without security enabled.
ChatConnector: message received.
[2018-11-12 10:22:49]-(info) [User-IN ] : Hi, how are you doing?
UniversalBot("*") routing "Hi, how are you doing?" from "emulator"
Library("*")recognize() recognized: None(0.1)
我该怎么做才能在模块中实现Winston,以便正确格式化每个输出?
非常感谢!
PS:这是我的(真正的标准)温斯顿代码
var appRoot = require('app-root-path');
var winston = require('winston');
const {transports, createLogger, format} = require('winston');
// define the custom settings for each transport (file, console)
var options = {
file: {
level: 'silly',
filename: `logs/Novachat_log.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'silly',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
var logger = winston.createLogger({
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `[${info.timestamp}]-(${info.level}) ${info.message}`)
),
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false, // do not exit on handled exceptions
});
module.exports = logger;
这是我通过Winston手动路由IN-OUT消息的方式:
const logIncomingMessage = (event) => {
logger.info('['+event.address.user.name+'-IN ] : ' + event.text);
};
const logOutgoingMessage = (event) => {
logger.info('['+event.address.user.name+'-OUT] : ' + event.text);
};
// Middleware for logging
bot.use({
receive: function (event, next) {
logIncomingMessage(event);
next();
},
send: function (event, next) {
logOutgoingMessage(event);
next();
}
});