Help us understand the problem. What is going on with this article?

Slack boltをTypescriptで作成時、log4jsで作成したログファイルにログを書き出す

これはなに

Slack bolt公式リファレンスのログ拡張方法がTypescriptに対応していなかったため備忘もかねて作成しました。
https://slack.dev/bolt/ja-jp/concepts#logging

こちらにサンプルも記載しましたのでよかったらご確認にどうぞ。
https://github.com/attumkwg/slackboltlogbytypescript

またこの記事に記載のソースコードは以下Yuta Ohigashi様の記事とリポジトリを参考に作成しています。
Bolt + jsx-slack + TypeScript を使って Slack App をつくる
https://github.com/phigasui/slack_app_boilerplate

内容

対応方法は単純で@slack/logger内に存在するConsoleLoggerを拡張してやるだけです。

import dotenv from 'dotenv'
import { App } from '@slack/bolt'
import Log4js from 'log4js'
import { LogLevel, ConsoleLogger } from '@slack/logger'

dotenv.config()

Log4js.configure('./src/log/log4js.json')

const systemLogger = Log4js.getLogger('system')

// @slack/loggerにConsoleLoggerというclassが存在するのでそれを拡張する
class CustomLogger extends ConsoleLogger {
  debug(...msg: any[]): void {
    systemLogger.debug(JSON.stringify(msg))
  }
  info(...msg: any[]): void {
    systemLogger.info(JSON.stringify(msg))
  }
  warn(...msg: any[]): void {
    systemLogger.warn(JSON.stringify(msg))
  }
  error(...msg: any[]): void {
    systemLogger.error(JSON.stringify(msg))
  }
}

const customLogger = new CustomLogger()
customLogger.setLevel(LogLevel.DEBUG)

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  logger: customLogger
})

log4js.json
{
    "appenders": {
        "ConsoleLogAppender": {
            "type": "console"
        },
        "SystemLogAppender": {
            "type": "dateFile",
            "filename": "./log/system.log",
            "pattern": ".yyyy-MM-dd",
            "daysToKeep": 7
        }
    },
    "categories": {
        "default": {
            "appenders": ["ConsoleLogAppender"],
            "level": "all"
        },
        "system": {
            "appenders": ["SystemLogAppender"],
            "level": "debug"
        }
    }
}

こうしてやることでsystem.logに下記のようなslack appの通信ログが記録されるようになります。

system.log
[2020-02-11T18:12:22.340] [DEBUG] system - ["apiCall('views.open') start"]
[2020-02-11T18:12:22.340] [DEBUG] system - ["will perform http request"]
[2020-02-11T18:12:22.711] [DEBUG] system - ["http response received"]
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした