最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

将Winston Logger从2.4.4迁移到3.x ...新的传输方式让我迷失了

运维笔记admin17浏览0评论

将Winston Logger从2.4.4迁移到3.x ...新的传输方式让我迷失了

将Winston Logger从2.4.4迁移到3.x ...新的传输方式让我迷失了

所以我试图从Winston 2.x迁移到3.x,但是它在设置传输方式方面发生了相当大的变化,我似乎无法像以前那样对其进行设置,所以仅此而已。我想要的控制台中的内容

[human-readable-date] [level(colourised)] : [text string], [formatted JSON]

在2.4中,我让它以未格式化的格式打印出JSON,这足够了,但是改进总是不错。

这是我的旧配置文件

const winston = require("winston");
require("winston-mongodb");
const config = require("./mongoDb").config;
const url = config.URL;

const tsFormat = () =>
  `${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}`;

const logger = new winston.Logger({
  transports: [
    new winston.transports.Console({
      timestamp: tsFormat,
      colorize: true
    }),
    new winston.transports.MongoDB({
      timestamp: tsFormat,
      db: url,
      level: "debug",
      autoReconnect: true
    })
  ]
});
module.exports = logger;

-编辑-

我现在在这里

const winston = require("winston");
require("winston-mongodb");
const config = require("./");
const mongo = require("./mongo");

const logger = winston.createLogger({
  transports: [
    new winston.transports.Console({
      format: winston.formatbine(
        winston.format.colorize(),
        winston.format.timestamp({
          format: "YYYY-MM-DD HH:mm:ss"
        }),
        winston.format.align(),
        winston.format.printf(
          info => `${info.timestamp} ${info.level}: ${info.message}`
        )
      )
    }),

    new winston.transports.MongoDB({
      db: `${config.mongoURI}/${config.mongodb}`,
      level: "debug",
      tryReconnect: true,
      storeHost: true
    })
  ]
});
module.exports = logger;

但是我根本无法获得所需的JSON部分,也无法将其发送到mongodb

回答如下:

与我喜欢的本地样式pino-pretty获得了相似的结果。

能够打印错误堆栈和其他元数据对象。

代码:

我为命名空间添加了其他打印“字段”,因为应用使用pino-pretty为每个文件创建了一个子记录器>

logger.child({ label: namespace })

结果:

winston.formatbine( winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format.errors({ stack: true }), winston.format.colorize(), winston.format.printf( ({ timestamp, level, label, message, stack, ...rest }) => { const namespace = label ? `(${label})` : '' const errStack = stack ? `\n${stack}` : '' const meta = rest && Object.keys(rest).length ? `\n${JSON.stringify(rest, undefined, 2)}` : '' return `[${timestamp}] ${level}: ${namespace} ${message} ${meta} ${errStack}` } ) )

发布评论

评论列表(0)

  1. 暂无评论