gpt4 book ai didi

node.js - Sailsjs - 使用 Winston 自定义日志记录

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:35 25 4
gpt4 key购买 nike

我目前正在尝试为 sailsjs 编写一个自定义记录器,它将使用 Winston 将文件发送到 s3 存储桶或 mongodb 数据库。

文档似乎缺乏,但到目前为止我发现了这个:

var customLogger = new winston.Logger({
transports: [
new(winston.transports.File)({
level: 'debug',
filename: './logs/my_log_file.log'
})
]
});

module.exports.log = {
colors: false, // To get clean logs without prefixes or color codings
custom: customLogger
};

这总体上不适合我。

有什么想法吗?

最佳答案

在扩展了 MayBeColin 的工作之后,工作解决方案:

在config文件夹中创建一个新的js文件(其中的代码将由sails自动执行)并添加mongodb传输,如下所示,

var winston = require('winston');
var MongoDB = require('winston-mongodb').MongoDB;

var customLogger = new(winston.Logger)({
transports: [
new(winston.transports.MongoDB)({
db: 'mongodb://localhost:27017/test',
collection: 'logs',
level: 'debug'
})
]
});

module.exports.logging = {
colors: false, // To get clean logs without prefixes or color codings
custom: customLogger
};

并在任何地方使用它,例如

sails.config.logging.custom.debug("winston mongodb transport logging");

关于node.js - Sailsjs - 使用 Winston 自定义日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26258774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com