LoginSignup
10
4

More than 3 years have passed since last update.

Google Functions: console.infoやconsole.errorなどとログビューアの「重大度」の関係性

Last updated at Posted at 2020-08-05

JavaScriptのConsole APIには、ロギングで良く使うconsole.log以外に、console.infoconsole.errorなど、ログに「情報」や「エラー」といった色をつけるメソッドがあります。

一方、Google Cloud Platform(GCP)のログビューアの「重大度(SEVERITY)」という概念があり、ログごとに「INFO」や「ERROR」などの意味合いを持たせることができます。

では、Console APIとGCPの「重大度」はどのような関係になっているのでしょうか? 実験してみたので、この投稿ではその結果をお伝えしたいと思います。

結論

先に結論を示します。JavaScriptのConsole APIのメソッドの違いは、基本的にGCPの重大度に影響しません。ただし、console.warnconsole.errorErrorオブジェクトをロギングした場合に限り、重大度が「ERROR」になります。

Errorオブジェクト以外をロギングした場合

Console API GCPの重大度
console.log DEFAULT
console.info DEFAULT
console.warn DEFAULT
console.error DEFAULT

Errorオブジェクトをロギングした場合

Console API GCPの重大度
console.log DEFAULT
console.info DEFAULT
console.warn ERROR
console.error ERROR

console.infoやconsole.errorなどが、ログビューアでどの「重大度」になるか検証する

各種メソッドを検証するために、次のような関数を用意しました:

index.js
exports.logging = (req, res) => {
  console.log('テキストをconsole.log')
  console.info('テキストをconsole.info')
  console.warn('テキストをconsole.warn')
  console.error('テキストをconsole.error')
  console.log(new Error('Errorオブジェクトをconsole.log'))
  console.info(new Error('Errorオブジェクトをconsole.info'))
  console.warn(new Error('Errorオブジェクトをconsole.warn'))
  console.error(new Error('Errorオブジェクトをconsole.error'))
  res.send('OK')
}

これをデプロイして、

gcloud functions deploy logging --runtime=nodejs12 --trigger-http

実行してみます:

curl https://asia-northeast1-${PROJECT}.cloudfunctions.net/logging

すると、ログビューアには次のようなログが残りました:

CleanShot 2020-08-05 at 12.10.12@2x.png

この結果を確認すると、console.warnconsole.errorErrorオブジェクトをロギングした場合は、重大度がERRORになり、それ以外はDEFAULTになったことが分かります。

次に読む

10
4
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
10
4