Winston: typscript ์˜ค๋ฅ˜ logger.stream ์“ฐ๊ธฐ ๊ธฐ๋Šฅ

์— ๋งŒ๋“  2018๋…„ 06์›” 30์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: winstonjs/winston

๊ท€ํ•˜์˜ ํ™˜๊ฒฝ์— ๋Œ€ํ•ด ์•Œ๋ ค์ฃผ์‹ญ์‹œ์˜ค.

  • _ winston ๋ฒ„์ „? _

    • [] winston@2

    • [X] winston@3

  • _ node -v ์ถœ๋ ฅ : _ v9.8.0
  • _ ์šด์˜ ์ฒด์ œ? _ MacOS
  • _Language? _ typescript : ๋ฒ„์ „ 2.9.1

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

logger.stream ๊ฐœ์ฒด์— ์“ฐ๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•  ๋•Œ typescript ์˜ค๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

error TS2322: Type '{ write(message: any, encoding: any): void; }' is not assignable to type '(options?: any) => ReadableStream'.
  Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.

39             write(message: any, encoding: any) {
               ~~~~~

๋Œ€์‹  ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๊นŒ?

์˜ค๋ฅ˜ ์—†์Œ

๋‹ค๋ฅธ

const options = {
            file: {
                level: "info",
                filename: `${appRoot}/logs/app.log`,
                handleExceptions: true,
                json: true,
                maxsize: 5242880, // 5MB
                maxFiles: 5,
                colorize: false,
            },
            console: {
                level: "debug",
                handleExceptions: true,
                json: false,
                colorize: true,
            },
        };

        this.logger = winston.createLogger({
            transports: [
                new winston.transports.File(options.file),
                new winston.transports.Console(options.console),
            ],
            exitOnError: false, // do not exit on handled exceptions
        });

        // create a stream object with a 'write' function that will be used by `morgan`
        this.logger.stream = {

            write(message: any, encoding: any) {
                // use the 'info' log level so the output will be picked up by both transports (file and console)
                this.logger.info(message);
            },
        };

์ด ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

https://www.digitalocean.com/community/tutorials/how-to-use-winston-to-log-node-js-applications

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

stream ๋ฅผ ๋กœ๊ฑฐ์— ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๋Œ€์‹  ๋‹ค์Œ์„ ์‹œ๋„ํ•ด๋ณด์„ธ์š”.

// At the bottom of your winston config add

export const stream = {
  write: (message) => {
    logger.info(message);
  },
};

๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๊ทธ์ธ ์ต์Šคํ”„๋ ˆ์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜์‹ญ์‹œ์˜ค.

import { stream } from 'config/winston.ts';
import * as morgan from 'morgan';

app.use(morgan('combined', { stream }));

๊ฒŒ์‹œ ํ•œ ์˜ค๋ฅ˜๋กœ ์ธํ•ด typescript๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฏ€๋กœ ๋‚ด ์˜ˆ์ œ๋Š” typescript๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ž‘์€ ๋ฌธ์ œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ŠคํŠธ๋ฆผ์„ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

export const stream = {
  write: logger.info,
};

๊ทธ๋Ÿฌ๋‚˜ ์ŠคํŠธ๋ฆผ ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ ๋กœ๊ฑฐ info ํ•จ์ˆ˜๊ฐ€ ์˜ˆ์ƒํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋„์›€์ด๋˜๋Š”์ง€ ์•Œ๋ ค์ฃผ์„ธ์š”.

๋ชจ๋“  4 ๋Œ“๊ธ€

๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€์žˆ๋‹ค

์–ธ๋œป๋ณด๊ธฐ์— ๋ฌธ์ œ๊ฐ€ ์œ ํ˜• ์ •์˜์—์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. stream(options?: any): NodeJS.ReadableStream; ์ด์ค‘์ด์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํŽธ์ง‘ : ์‹ค์ œ๋กœ ์“ธ ์ˆ˜์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ๋ณผ๊ฒŒ์š”.

@DABH ์ด๊ฒƒ ์ข€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

stream ๋ฅผ ๋กœ๊ฑฐ์— ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๋Œ€์‹  ๋‹ค์Œ์„ ์‹œ๋„ํ•ด๋ณด์„ธ์š”.

// At the bottom of your winston config add

export const stream = {
  write: (message) => {
    logger.info(message);
  },
};

๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๊ทธ์ธ ์ต์Šคํ”„๋ ˆ์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜์‹ญ์‹œ์˜ค.

import { stream } from 'config/winston.ts';
import * as morgan from 'morgan';

app.use(morgan('combined', { stream }));

๊ฒŒ์‹œ ํ•œ ์˜ค๋ฅ˜๋กœ ์ธํ•ด typescript๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฏ€๋กœ ๋‚ด ์˜ˆ์ œ๋Š” typescript๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ž‘์€ ๋ฌธ์ œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ŠคํŠธ๋ฆผ์„ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

export const stream = {
  write: logger.info,
};

๊ทธ๋Ÿฌ๋‚˜ ์ŠคํŠธ๋ฆผ ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ ๋กœ๊ฑฐ info ํ•จ์ˆ˜๊ฐ€ ์˜ˆ์ƒํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋„์›€์ด๋˜๋Š”์ง€ ์•Œ๋ ค์ฃผ์„ธ์š”.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰