tessl install tessl/golang-cloud-google-com--go--logging@1.13.0Cloud Logging client library for Go that enables writing log entries to Google Cloud Logging service with buffered asynchronous and synchronous logging capabilities.
This document describes all constants defined in the Cloud Logging library, including OAuth scopes, default configuration values, and source location population options.
const (
ReadScope = "https://www.googleapis.com/auth/logging.read"
WriteScope = "https://www.googleapis.com/auth/logging.write"
AdminScope = "https://www.googleapis.com/auth/logging.admin"
)OAuth 2.0 scopes for accessing Cloud Logging APIs.
const ReadScope = "https://www.googleapis.com/auth/logging.read"Scope for reading from the logging service. Used by the logadmin package when reading logs. Allows:
Example:
import (
"google.golang.org/api/option"
"cloud.google.com/go/logging"
)
client, err := logging.NewClient(ctx, "my-project",
option.WithScopes(logging.ReadScope),
)const WriteScope = "https://www.googleapis.com/auth/logging.write"Scope for writing to the logging service. This is the default scope for logging.NewClient. Allows:
Example:
// WriteScope is the default, so this is automatic
client, err := logging.NewClient(ctx, "my-project")
// Explicitly specify if needed
client, err := logging.NewClient(ctx, "my-project",
option.WithScopes(logging.WriteScope),
)const AdminScope = "https://www.googleapis.com/auth/logging.admin"Scope for administrative actions on the logging service. This is the default scope for logadmin.NewClient. Allows:
Example:
import "cloud.google.com/go/logging/logadmin"
// AdminScope is the default for logadmin
client, err := logadmin.NewClient(ctx, "my-project")
// Explicitly specify if needed
client, err := logadmin.NewClient(ctx, "my-project",
option.WithScopes(logging.AdminScope),
)const (
DefaultDelayThreshold = time.Second
DefaultEntryCountThreshold = 1000
DefaultEntryByteThreshold = 8388608 // 8 MiB
DefaultBundleByteLimit = 9437184 // 9.5 MiB
DefaultBufferedByteLimit = 1073741824 // 1 GiB
)Default values for Logger buffering and batching configuration.
const DefaultDelayThreshold = time.SecondDefault value for the DelayThreshold LoggerOption. Maximum time a log entry remains buffered before triggering a write to Cloud Logging.
Value: 1 second
Usage:
// Use default (1 second)
logger := client.Logger("my-log")
// Override with custom value
logger := client.Logger("my-log",
logging.DelayThreshold(500*time.Millisecond), // 500ms
)const DefaultEntryCountThreshold = 1000Default value for the EntryCountThreshold LoggerOption. Maximum number of entries buffered before triggering a write to Cloud Logging.
Value: 1000 entries
Usage:
// Use default (1000 entries)
logger := client.Logger("my-log")
// Override with custom value
logger := client.Logger("my-log",
logging.EntryCountThreshold(500), // Flush at 500 entries
)const DefaultEntryByteThreshold = 1 << 23 // 8 MiBDefault value for the EntryByteThreshold LoggerOption. Maximum bytes of entries buffered before triggering a write to Cloud Logging.
Value: 8 MiB (8,388,608 bytes)
Usage:
// Use default (8 MiB)
logger := client.Logger("my-log")
// Override with custom value
logger := client.Logger("my-log",
logging.EntryByteThreshold(4<<20), // 4 MiB
)const DefaultBundleByteLimit = 9437184 // 9.5 MiBDefault value for the BundleByteLimit LoggerOption. Maximum bytes sent in a single call to the Cloud Logging service. This is close to the 10 MiB gRPC message size limit.
Value: 9.5 MiB (9,437,184 bytes)
Usage:
// Use default (9.5 MiB)
logger := client.Logger("my-log")
// Override with custom value (not recommended unless you have specific needs)
logger := client.Logger("my-log",
logging.EntryByteLimit(5<<20), // 5 MiB
)const DefaultBufferedByteLimit = 1 << 30 // 1 GiBDefault value for the BufferedByteLimit LoggerOption. Maximum memory (in bytes) a Logger will use for buffering entries before returning ErrOverflow.
Value: 1 GiB (1,073,741,824 bytes)
Usage:
// Use default (1 GiB)
logger := client.Logger("my-log")
// Override with custom value
logger := client.Logger("my-log",
logging.BufferedByteLimit(512<<20), // 512 MiB
)const DetectProjectID = "*detect-project-id*"Sentinel value that instructs NewClient to auto-detect the project ID from the environment. NewClient will use the project ID from:
Usage:
// Auto-detect project ID
client, err := logging.NewClient(ctx, logging.DetectProjectID)
// Equivalent to explicitly providing the project ID
client, err := logging.NewClient(ctx, "my-project")Auto-detection sources:
const (
DoNotPopulateSourceLocation = 0
PopulateSourceLocationForDebugEntries = 1
AlwaysPopulateSourceLocation = 2
)Constants for controlling automatic population of the SourceLocation field in log entries.
const DoNotPopulateSourceLocation = 0Default value. Disables automatic source location population. The SourceLocation field will only be set if explicitly provided in the Entry.
Usage:
// Default behavior - no source location
logger := client.Logger("my-log")
// Explicitly specify (same as default)
logger := client.Logger("my-log",
logging.SourceLocationPopulation(logging.DoNotPopulateSourceLocation),
)const PopulateSourceLocationForDebugEntries = 1Populates the SourceLocation field only for log entries with Debug severity. The source location includes:
Performance impact: Moderate. Only affects Debug-level logs.
Usage:
logger := client.Logger("my-log",
logging.SourceLocationPopulation(logging.PopulateSourceLocationForDebugEntries),
)
// This entry WILL have source location
logger.Log(logging.Entry{
Payload: "debug information",
Severity: logging.Debug,
})
// This entry will NOT have source location
logger.Log(logging.Entry{
Payload: "info message",
Severity: logging.Info,
})const AlwaysPopulateSourceLocation = 2Always populates the SourceLocation field for all log entries, regardless of severity.
Performance impact: Significant. Can decrease performance by a factor of 2 or more, as it requires runtime stack inspection for every log call.
Usage:
logger := client.Logger("my-log",
logging.SourceLocationPopulation(logging.AlwaysPopulateSourceLocation),
)
// ALL entries will have source location
logger.Log(logging.Entry{
Payload: "any message",
Severity: logging.Info,
})Source location includes:
type LogEntrySourceLocation struct {
File string // Source file: "main.go"
Line int64 // Line number: 42
Function string // Full function name: "main.handleRequest"
}The default buffering thresholds work together to control when log entries are sent:
+-------------------+---------------------------+
| Threshold | Default Value |
+-------------------+---------------------------+
| Time | 1 second |
| Entry Count | 1000 entries |
| Buffered Bytes | 8 MiB |
+-------------------+---------------------------+
| Bundle Size Limit | 9.5 MiB per RPC |
| Buffer Memory Max | 1 GiB (before overflow) |
+-------------------+---------------------------+Flushing behavior:
Entries are sent when any threshold is met:
Example scenarios:
// High-volume logging: likely flushes on entry count or byte threshold
for i := 0; i < 10000; i++ {
logger.Log(logging.Entry{Payload: "message"})
}
// Flushes when 1000 entries or 8 MiB buffered
// Low-volume logging: likely flushes on time threshold
logger.Log(logging.Entry{Payload: "occasional message"})
// Flushes after 1 secondpackage main
import (
"context"
"log"
"time"
"cloud.google.com/go/logging"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
// Auto-detect project ID
client, err := logging.NewClient(ctx, logging.DetectProjectID)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
defer client.Close()
// Logger with custom thresholds
logger := client.Logger("custom-log",
// Flush more aggressively than defaults
logging.DelayThreshold(500*time.Millisecond), // vs 1 second default
logging.EntryCountThreshold(500), // vs 1000 default
logging.EntryByteThreshold(4<<20), // 4 MiB vs 8 MiB default
// Increase buffer to prevent overflow
logging.BufferedByteLimit(2<<30), // 2 GiB vs 1 GiB default
// Enable source location for debug logs
logging.SourceLocationPopulation(logging.PopulateSourceLocationForDebugEntries),
)
// Log with source location (Debug severity)
logger.Log(logging.Entry{
Payload: "debug with source location",
Severity: logging.Debug,
})
// Log without source location (Info severity)
logger.Log(logging.Entry{
Payload: "info without source location",
Severity: logging.Info,
})
logger.Flush()
}| Scope | Read Logs | Write Logs | Read Sinks/Metrics | Manage Sinks/Metrics | Delete Logs |
|---|---|---|---|---|---|
| ReadScope | ✓ | ✗ | ✓ | ✗ | ✗ |
| WriteScope | ✗ | ✓ | ✗ | ✗ | ✗ |
| AdminScope | ✓ | ✓ | ✓ | ✓ | ✓ |
// For applications that only write logs
client, _ := logging.NewClient(ctx, "my-project")
// Uses WriteScope by default
// For applications that read and write logs
client, _ := logging.NewClient(ctx, "my-project",
option.WithScopes(logging.AdminScope),
)
// For read-only log analysis tools
client, _ := logadmin.NewClient(ctx, "my-project",
option.WithScopes(logging.ReadScope),
)// High-volume, high-throughput
logger := client.Logger("high-volume",
logging.DelayThreshold(100*time.Millisecond), // Flush quickly
logging.EntryCountThreshold(10000), // Large batches
logging.ConcurrentWriteLimit(10), // More concurrent RPCs
)
// Low-volume, latency-sensitive
logger := client.Logger("low-volume",
logging.DelayThreshold(5*time.Second), // Wait longer
logging.EntryCountThreshold(100), // Small batches
)
// Memory-constrained environment
logger := client.Logger("constrained",
logging.BufferedByteLimit(64<<20), // 64 MiB max buffer
logging.EntryByteThreshold(2<<20), // Flush at 2 MiB
)// Development/debugging - always populate
logger := client.Logger("dev-log",
logging.SourceLocationPopulation(logging.AlwaysPopulateSourceLocation),
)
// Production - only for debug logs
logger := client.Logger("prod-log",
logging.SourceLocationPopulation(logging.PopulateSourceLocationForDebugEntries),
)
// Production high-performance - disabled
logger := client.Logger("perf-log",
logging.SourceLocationPopulation(logging.DoNotPopulateSourceLocation),
)