로그 작성 및 보기


로깅은 코드 디버깅 및 모니터링을 하는 데 중요한 수단입니다. 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/v1");

  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() 명령어의 로그 수준은 정보입니다.
  • logger.info() 명령어의 로그 수준은 정보입니다.
  • logger.warn() 명령어의 로그 수준은 경고입니다.
  • logger.error() 명령어의 로그 수준은 오류입니다.
  • 내부 시스템 메시지의 로그 수준은 디버그입니다.

logger.write() 명령어를 사용하면 로그 심각도 수준이 CRITICAL, ALERT, EMERGENCY인 로그 항목을 작성할 수 있습니다. 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를 사용하는 것입니다. 할 수 있습니다. Node.js를 사용하면 표준 자바스크립트 로깅을 대신 사용할 수 있습니다. console.logconsole.error와 같은 호출을 실행하지만 먼저 특수 모듈을 사용하여 표준 메서드를 패치할 수 있습니다.

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

로거 호환성 모듈이 필요한 경우 코드에 평소처럼 console.log() 메서드를 사용하면 됩니다.

exports.helloError = functions.https.onRequest((request, response) => {
  console.log('I am a log entry!');
  response.send('Hello World...');
});
  • console.log() 명령어의 로그 수준은 정보입니다.
  • console.info() 명령어의 로그 수준은 정보입니다.
  • console.warn() 명령어의 로그 수준은 오류입니다.
  • console.error() 명령어의 로그 수준은 오류입니다.
  • 내부 시스템 메시지의 로그 수준은 디버그입니다.

로그 보기

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 Functions의 로그를 확인할 수 있습니다. (Cloud Logging UI)

로그 분석

Cloud Logging는 데이터를 분석할 수 있는 강력한 로그 분석 도구 모음을 제공합니다. 사용하여 Cloud Functions을(를) 모니터링할 수 있습니다.

차트 및 알림

로그 기반 측정항목을 만들어 함수를 모니터링한 후에는 이 측정항목을 바탕으로 차트와 알림을 만들 수 있습니다. 예를 들어 시간에 따른 지연 시간을 시각화하는 차트를 만들거나 특정 오류가 과도하게 발생할 경우 알려주는 알림을 만들 수 있습니다.

차트 및 알림 정책에서 로그 기반 측정항목을 사용하는 자세한 방법은 차트 및 알림 만들기를 참조하세요.