Winston: 允许在同一记录器中配置每个传输的格式

创建于 2018-07-08  ·  5评论  ·  资料来源: winstonjs/winston

有什么特点?

我有一个带有控制台传输的记录器,另一个传输到基于云的日志记录服务。 对于控制台,我想用时间戳作为消息前缀,并为它们着色。 对于基于云的日志服务,我不想要时间戳,因为该服务提供自己的时间戳,而且我也不想要颜色。

这是您准备在我们的支持下实施的功能吗?

此功能的 PR 已发送: #427#422

是否已经可以按传输配置格式,但我没有在自述文件中看到该功能的解释?

最有用的评论

没关系,这确实是可能的,但是文档完全省略了提及该功能。

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

所有5条评论

没关系,这确实是可能的,但是文档完全省略了提及该功能。

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

请务必添加到文档中

@dandv 注释中的 printf 代码无法立即使用,时间戳代码后缺少

所以这条线...

format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),

...需要一个像这样的右大括号

format: winston.format.printf(info => `${new Date().toISOString()}, ${info.message}`),

感谢您提供自定义格式的代码和知识! 非常感激!

没关系,这确实可能,但文档完全省略了提及该功能。

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

@dandv如果我在传输之外和创建记录器方法中有另一种格式会怎样。 将应用哪种格式?

没关系,这确实可能,但文档完全省略了提及该功能。

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

@dandv如果我在传输之外和创建记录器方法中有另一种格式会怎样。 将应用哪种格式?

里面的那个。

此页面是否有帮助?
0 / 5 - 0 等级