Cloud Logging

gRPC · port 8092

Configuration

No env var. Cloud Logging has no official *_EMULATOR_HOST environment variable. You must configure the endpoint manually in your client code.

Go SDK example

Write log entries with severity levels and list them back with a filter:

package main

import (
    "context"
    "fmt"
    "log"

    logging "cloud.google.com/go/logging/apiv2"
    loggingpb "cloud.google.com/go/logging/apiv2/loggingpb"
    "google.golang.org/api/option"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    ltype "google.golang.org/genproto/googleapis/logging/type"
    monitoredres "google.golang.org/genproto/googleapis/api/monitoredres"
)

func main() {
    ctx := context.Background()

    // Connect to localgcp (no TLS, no auth)
    client, err := logging.NewClient(ctx,
        option.WithEndpoint("localhost:8092"),
        option.WithoutAuthentication(),
        option.WithGRPCDialOption(grpc.WithTransportCredentials(
            insecure.NewCredentials(),
        )),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    project := "projects/my-project"

    // Write log entries with severity
    err = client.WriteLogEntries(ctx, &loggingpb.WriteLogEntriesRequest{
        LogName:  project + "/logs/my-service",
        Resource: &monitoredres.MonitoredResource{
            Type: "global",
            Labels: map[string]string{
                "project_id": "my-project",
            },
        },
        Entries: []*loggingpb.LogEntry{
            {
                Severity: ltype.LogSeverity_INFO,
                Payload:  &loggingpb.LogEntry_TextPayload{
                    TextPayload: "Server started on port 8080",
                },
                Labels: map[string]string{
                    "env": "dev",
                },
            },
            {
                Severity: ltype.LogSeverity_ERROR,
                Payload:  &loggingpb.LogEntry_TextPayload{
                    TextPayload: "Connection refused: database unreachable",
                },
            },
        },
    })
    if err != nil {
        log.Fatal(err)
    }

    // List entries with a filter (e.g. only errors)
    it := client.ListLogEntries(ctx, &loggingpb.ListLogEntriesRequest{
        ResourceNames: []string{project},
        Filter:        "severity >= ERROR",
    })

    for {
        entry, err := it.Next()
        if err != nil {
            break
        }
        fmt.Printf("[%s] %s\n", entry.Severity, entry.GetTextPayload())
    }
}

Features

Not yet supported