0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TypeScriptにて、ログ出力クラスを作ってみる

Last updated at Posted at 2023-06-10

モジュールインストール

npm i @aws-lambda-powertools/logger

ロガーインスタンス生成。これが無いと始まらない

anime-suko.ts
// ロガー設定
const logger = new Logger({ serviceName: "何かしらサービス名" })

常にログに出力させたいものを指定(Attributeを出力ログに含める)

anime-suko.ts
// ログ出力に出力したいものを連想配列で指定して追加
export const addOutputLogValue = async (addValue: Record<string, string>) => {
  logger.addPersistentLogAttributes(addValue)
}

通常ログ

※ここから注意だが、logger.infoにはnullやundefinedが入るとエラーになる。いい感じに処理してくれないので、自分でなんとかするしかない。

anime-suko.ts
export const outputLog = async (value: string) => {
  // 値が入っているときのみログ出力(nullやundefinedは指定不可能)
  if (!value) {
    logger.info('ログ出力に指定されたデータが入っていません')
    return
  }
  logger.info(value)
}

エラーログ

anime-suko.ts
export const outputErrorLog = async (value: string) => {
  // 値が入っているときのみログ出力(nullやundefinedは指定不可能)
  if (!value) {
    logger.info('ログ出力に指定されたデータが入っていません')
    return
  }
  logger.error(value)
}

デバッグログ

anime-suko.ts
export const outputDebugLog = async (value: string) => {
  // 値が入っているときのみログ出力(nullやundefinedは指定不可能)
  if (!value) {
    logger.info('ログ出力に指定されたデータが入っていません')
    return
  }
  logger.debug(value)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?