GCP Cloud Logging - Custom Logs

When writing an application that uses multple micro-services it's nice to have a consolidated log from all the pieces of the application as a whole.

When writing an application that uses multple micro-services it's nice to have a consolidated log from all the pieces of the application as a whole.

You can accomplish this on GCP buy creating a custom log in Google Cloud Logging.

Here is an example using Nodejs. First install the cloud logging NPM package.

npm i @google-cloud/logging

Then you can add a logging function to you application and call it when you want to make a log entry.

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

async function logEntry(LEVEL, LOG_OBJECT) {
  // Creates a client
  // Service account used for GPC instances needs "Logging Admin" role
  const logging = new Logging();

  // Selects the log to write to
  const log = logging.log("custom-log-demo");

  // The metadata associated with the entry
  const metadata = {
    resource: { type: "global" },
    severity: LEVEL,
  };

  // Prepares a log entry
  const entry = log.entry(metadata, LOG_OBJECT);

  async function writeLog() {
    // Writes the log entry
    await log.write(entry);
    console.log(`Logged: ${LOG_OBJECT.message ?? "New Entry"}`);
  }
  writeLog();
}

You can export this function for use in your application and call it like this.

logEntry("INFO", {
  message: "Test Message",
  customKey: "customValue",
});

Reference

Index custom fields

Custom fields appear to already be searchable but maybe indexing them makes results faster?

https://cloud.google.com/logging/docs/analyze/custom-index

Sink logs with Cloud Storage / BigQuery / PubSub

https://cloud.google.com/logging/docs/export/configure_export_v2

Log Levels

https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity

LEVEL DESCRIPTION
DEFAULT (0) The log entry has no assigned severity level.
DEBUG (100) Debug or trace information.
INFO (200) Routine information, such as ongoing status or performance.
NOTICE (300) Normal but significant events, such as start up, shut down, or a configuration change.
WARNING (400) Warning events might cause problems.
ERROR (500) Error events are likely to cause problems.
CRITICAL (600) Critical events cause more severe problems or outages.
ALERT (700) A person must take an action immediately.
EMERGENCY (800) One or more systems are unusable.

Run this on your local development environment

To run this on your local development environment checkout this guide to set up Service Account impersonation.

https://www.dadonk.com/impersonating-a-service-account-for-local-development/

GitHub - dadonk-com/gcp-cloud-logging-custom-logs: Example of logging to a custom log in Google Cloud Logging
Example of logging to a custom log in Google Cloud Logging - GitHub - dadonk-com/gcp-cloud-logging-custom-logs: Example of logging to a custom log in Google Cloud Logging
@google-cloud/logging
Cloud Logging Client Library for Node.js. Latest version: 10.1.11, last published: 5 days ago. Start using @google-cloud/logging in your project by running `npm i @google-cloud/logging`. There are 230 other projects in the npm registry using @google-cloud/logging.

Subscribe to dadonk

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe