ログの書き込みと表示


ロギングは、コードのデバッグとモニタリングに重要なツールです。Cloud Functions には、ロガー SDK、カスタムの Google Cloud Logging、ウェブ開発用の console オブジェクト標準のオプションがあります。

ログの書き込み

ほとんどの場合、Cloud Functions ロガー SDK を使用することをおすすめしますが、次の理由から他のオプションも選択できます。

  • 既存のコードベースがあり、console.log からのリファクタリングを行いたくない場合。
  • Cloud Logging(旧 StackDriver Logging)に精通していて、カスタム ロギングを行うためにそれを使用したい場合。

Cloud Functions ロガー SDK の使用

Cloud Functions ロガー SDK は、console.log ステートメントと同様の API を備えた標準インターフェースを提供し、他のログレベルもサポートします。この SDK を使用すると、構造化データを使用してイベントをログに記録でき、分析とモニタリングが容易になります。

ロガー SDK は、ワイルドカード インポートの一部としてのログエントリをサポートしています。次に例を示します。

  const functions = require("firebase-functions");

  functions.logger.log("Hello from info. Here's an object:", someObj);

また、個々のエクスポートを使用することもできます。この例は、最後の引数としてログに添付された構造化データを示しています。

const { warn } = require("firebase-functions/logger");


// Attach structured data to the log as the last argument.
warn("This is a 'WARNING' severity message with some metadata.", {
  key1: 'val1',
  key2: 'val2'
});
  • logger.log() コマンドのログレベルは INFO です。
  • logger.info() コマンドのログレベルは INFO です。
  • logger.warn() コマンドのログレベルは WARNING です。
  • logger.error() コマンドのログレベルは ERROR です。
  • 内部システム メッセージのログレベルは DEBUG です。

logger.write() を使用すると、ログエントリに加えて、CRITICALALERTEMERGENCY の重大度レベルを書き込むことができます。LogSeverity をご覧ください。

カスタム Cloud Logging ログ

ロガー SDK を使用した Cloud Functions のログは、Cloud Logging を基盤としています。Node.js 用の Cloud Logging ライブラリを使用してイベントに構造化データを補完してログに記録し、分析とモニタリングを容易に行うことができます。

const { Logging } = require('@google-cloud/logging');

// ...

// Instantiate the logging SDK. The project ID will
// be automatically inferred from the Cloud Functions environment.
const logging = new Logging();
const log = logging.log('my-custom-log-name');

// This metadata is attached to each log entry. This specifies a fake
// Cloud Function called 'Custom Metrics' in order to make your custom
// log entries appear in the Cloud Functions logs viewer.
const METADATA = {
  resource: {
    type: 'cloud_function',
    labels: {
      function_name: 'CustomMetrics',
      region: 'us-central1'
    }
  }
};

// ...

// Data to write to the log. This can be a JSON object with any properties
// of the event you want to record.
const data = {
  event: 'my-event',
  value: 'foo-bar-baz',

  // Optional 'message' property will show up in the Firebase
  // console and other human-readable logging surfaces
  message: 'my-event: foo-bar-baz'
};

// Write to the log. The log.write() call returns a Promise if you want to
// make sure that the log was written successfully.
const entry = log.entry(METADATA, data);
log.write(entry);

console.log の使用

関数でロギングを行うのに推奨されるソリューションは、ロガー SDK を使用することです。代わりに、console.logconsole.error などの標準の JavaScript のロギング呼び出しを使用することもできますが、その場合は、標準のメソッドを正しく動作させるためのパッチとなる特別なモジュールを require する必要があります。

require("firebase-functions/logger/compat");

ロガー互換性モジュールを require したら、コード内で通常どおり console.log() メソッドを使用できます。

exports.helloError = functions.https.onRequest((request, response) => {
  console.log('I am a log entry!');
  response.send('Hello World...');
});
  • console.log() コマンドのログレベルは INFO です。
  • console.info() コマンドのログレベルは INFO です。
  • console.warn() コマンドのログレベルは ERROR です。
  • console.error() コマンドのログレベルは ERROR です。
  • 内部システム メッセージのログレベルは DEBUG です。

ログの表示

Cloud Functions のログは、Google Cloud コンソール、Cloud Logging UI、または firebase コマンドライン ツールで表示できます。

Firebase CLI の使用

firebase ツールでログを表示するには、functions:log コマンドを使用します。

firebase functions:log

特定の関数のログを表示するには、引数として関数名を入力します。

firebase functions:log --only <FUNCTION_NAME>

すべてのログ表示オプションについては、functions:log のヘルプをご参照ください。

firebase help functions:log

Google Cloud コンソールの使用

Google Cloud コンソールで関数のログを表示できます。

Cloud Logging UI の使用

Cloud Logging UI で Cloud Functions のログを表示できます。

ログの分析

Cloud Logging には強力なログ分析ツールのスイートが備わっており、これを利用して Cloud Functions をモニタリングできます。

グラフとアラート

自分の関数をモニタリングするためのログベースの指標を作成したら、それらの指標に基づいてグラフやアラートを作成できます。たとえば、経時的なレイテンシを視覚化するグラフを作成したり、特定のエラーが非常に多く発生した場合に知らせるアラートを作成したりできます。

グラフやアラートのポリシーでログベースの指標を使用する方法については、グラフとアラートの作成をご覧ください。