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) ์ฐฝ
  • _์–ธ์–ด?_ ๋ชจ๋‘

๋ฌธ์ œ๊ฐ€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

PrettyPrint๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค

  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 ๋“ฑ๊ธ‰