winston에 슬랙으로 포맷 적용하여 로깅

codeing999·2023년 9월 8일
1

express

목록 보기
1/1

https://www.npmjs.com/package/winston-slack-webhook-transport

winston-slack-webhook-transport 패키지란게 있어서 이걸 사용해보기로 했는데

new SlackHook({
    webhookUrl: process.env.SLACK_ERROR_ALARM_URL,
    level: "error", // 에러 레벨 이상의 로그를 Slack으로 보냄
  }),

transports에 이걸 추가해도 다른 곳의 로그와 달리 슬랙에는 winston의 포맷이 적용되지 않은 채로 로그가 남았다.

https://github.com/TheAppleFreak/winston-slack-webhook-transport/blob/master/slackHook.js

패키지 설명을 보니까

new SlackHook({
            webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx",
            formatter: info => {
                return {
                    text: "This will function as a fallback for surfaces that don't support Block Kit, like IRC clients or mobile push notifications.",
                    attachments: [
                        {
                            text: "Or don't pass anything. That's fine too"
                        }
                    ],
                    blocks: [
                        {
                            type: "section",
                            text: {
                                type: "plain_text",
                                text: "You can pass more info to the formatter by supplying additional parameters in the logger call"
                            }
                        }
                    ]
                }
            }
        })

이런 식으로 슬랙의 포맷을 활용한다고 한다.
text는 말그대로 슬랙의 텍스트고. attachments나 blocks는 슬랙의 어떤 포맷 같은데 이건 쓸필요 없을 거 같고.

new SlackHook({
    webhookUrl: process.env.SLACK_ERROR_ALARM_URL,
    level: "error", // 에러 레벨 이상의 로그를 Slack으로 보냄
    formatter: (info) => ({ //슬랙은 포맷 여기서 따로 지정해 줘야 함
      text: `${info.timestamp} [${info.label}] ${info.level} | ${info.message}`
    }),
  }),

이런 식으로 간단히 작성 완료. 아무튼 포맷을 따로 작성해주어야 한다.

profile
코딩 공부 ing..

0개의 댓글