Trigger function from Cloud Logging

Stay organized with collections Save and categorize content based on your preferences.

Triggers a function based on Cloud Logging entries.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


// Package log contains examples for handling Cloud Functions logs.
package log

import (

// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
type PubSubMessage struct {
	Data []byte `json:"data"`

// ProcessLogEntry processes a Pub/Sub message from Cloud Logging.
func ProcessLogEntry(ctx context.Context, m PubSubMessage) error {
	log.Printf("Log entry data: %s", string(m.Data))
	return nil


import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.logging.Logger;

public class StackdriverLogging implements BackgroundFunction<Message> {
  private static final Logger logger = Logger.getLogger(StackdriverLogging.class.getName());

  public void accept(Message message, Context context) {
    String name = "World";

    if (!message.getData().isEmpty()) {
      name = new String(Base64.getDecoder().decode(
          message.getData().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    String res = String.format("Hello, %s", name);;


exports.processLogEntry = data => {
  const dataBuffer = Buffer.from(, 'base64');

  const logEntry = JSON.parse(dataBuffer.toString('ascii')).protoPayload;
  console.log(`Method: ${logEntry.methodName}`);
  console.log(`Resource: ${logEntry.resourceName}`);
  console.log(`Initiator: ${logEntry.authenticationInfo.principalEmail}`);


import base64
import json

def process_log_entry(data, context):
    data_buffer = base64.b64decode(data['data'])
    log_entry = json.loads(data_buffer)['protoPayload']

    print(f"Method: {log_entry['methodName']}")
    print(f"Resource: {log_entry['resourceName']}")
    print(f"Initiator: {log_entry['authenticationInfo']['principalEmail']}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.