Winston: prettyPrint 和 simple 在 3.0 中不起作用

创建于 2018-11-02  ·  3评论  ·  资料来源: winstonjs/winston

请告诉我们您的环境:

  • _ winston版本?_

    • [ ] winston@2

    • [x] winston@3

  • _ node -v输出:_ v10.12.0
  • _操作系统?_(Windows、macOS 或 Linux)窗口
  • _语言?_全部

问题是什么?

漂亮打印不起作用

  winston.format.combine(
    winston.format.timestamp(),
    winston.format.colorize(),
    winston.format.prettyPrint(),
    winston.format.splat(),
    winston.format.simple(), // removing this gives an even worse output
  )

你期望会发生什么?

与winston 2有相同的行为

其他信息

试图从 winston 2 获得相同的日志行为,但不起作用。 日志都含糊不清,无法使用控制台传输读取。

image

image

最有用的评论

如果有人在解决问题时正在寻找解决方法,我将在本地开发环境中使用以下格式:

import { format } from 'winston';
const {
  colorize,
  combine,
  timestamp,
  errors,
  printf,
  splat,
} = format;

const devFormat = combine(
        format((info) => {
          info.level = info.level.toUpperCase();
          return info;
        })(),
        colorize(),
        timestamp(),
        splat(),
        errors(),
        printf(
          ({
            timestamp,
            level,
            message,
            ...rest
          }) => {
            let restString = JSON.stringify(rest, undefined, 2);
            restString = restString === '{}' ? '' : restString;

            return `[${timestamp}] ${level} - ${message} ${restString}`;
          },
        ),
      );

所有3条评论

我有同样的问题。 看看#1537。

如果有人在解决问题时正在寻找解决方法,我将在本地开发环境中使用以下格式:

import { format } from 'winston';
const {
  colorize,
  combine,
  timestamp,
  errors,
  printf,
  splat,
} = format;

const devFormat = combine(
        format((info) => {
          info.level = info.level.toUpperCase();
          return info;
        })(),
        colorize(),
        timestamp(),
        splat(),
        errors(),
        printf(
          ({
            timestamp,
            level,
            message,
            ...rest
          }) => {
            let restString = JSON.stringify(rest, undefined, 2);
            restString = restString === '{}' ? '' : restString;

            return `[${timestamp}] ${level} - ${message} ${restString}`;
          },
        ),
      );

对我来说似乎工作正常:

这是一些示例代码,它们根据处理模拟 S3 事件故意引发错误

~/repos/ghe/lambda-logging (master *% u=)> node -r dotenv/config ./src/test-harness.js 
{
  module: 'app.js',
  s3SchemaVersion: '1.0',
  configurationId: '828aa6fc-f7b5-4305-8584-487c791949c1',
  bucket: {
    name: 'lambda-artifacts-deafc19498e3f2df',
    ownerIdentity: { principalId: 'A3I5XTEXAMAI3E' },
    arn: 'arn:aws:s3:::lambda-artifacts-deafc19498e3f2df'
  },
  object: {
    key: 'b21b84d653bb07b05b1e6b33684dc11b',
    size: 1305107,
    eTag: 'b21b84d653bb07b05b1e6b33684dc11b',
    sequencer: '0C0F6F405D6ED209E1'
  },
  level: 'INFO',
  message: 'processing event',
  timestamp: '2020-05-09 20:03:43'
}
{
  level: 'ERROR',
  module: 'index.js',
  timestamp: '2020-05-09 20:03:43',
  message: "Cannot read property '0' of undefined",
  stack: "TypeError: Cannot read property '0' of undefined\n" +
    '    at Object.run (/Users/jason.berk/repos/ghe/lambda-logging/src/app.js:5:26)\n' +
    '    at Object.exports.handler (/Users/jason.berk/repos/ghe/lambda-logging/src/index.js:7:22)\n' +
    '    at Object.<anonymous> (/Users/jason.berk/repos/ghe/lambda-logging/src/test-harness.js:44:9)\n' +
    '    at Module._compile (internal/modules/cjs/loader.js:1158:30)\n' +
    '    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)\n' +
    '    at Module.load (internal/modules/cjs/loader.js:1002:32)\n' +
    '    at Function.Module._load (internal/modules/cjs/loader.js:901:14)\n' +
    '    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)\n' +
    '    at internal/main/run_main_module.js:18:47'
}

我的记录器配置是:

const path = require('path');
const { createLogger, format, transports } = require('winston');
const { combine, errors, timestamp } = format;

const baseFormat = combine(
  timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
  errors({ stack: true }),
  format((info) => {
    info.level = info.level.toUpperCase();
    return info;
  })(),
);

const splunkFormat = combine(
  baseFormat,
  format.json(),
);

const prettyFormat = combine(
  baseFormat,
  format.prettyPrint(),
);

const createCustomLogger = (moduleName) => createLogger({
  level: process.env.LOG_LEVEL,
  format: process.env.PRETTY_LOGS ? prettyFormat : splunkFormat,
  defaultMeta: { module: path.basename(moduleName) },
  transports: [
    new transports.Console(),
  ],
});

module.exports = createCustomLogger;
此页面是否有帮助?
0 / 5 - 0 等级