Documentation
¶
Overview ¶
Package clog provides a Google Cloud Logging "adapter" for log/slog.
Index ¶
Constants ¶
const ( TimestampKey = "timestamp" MessageKey = "message" SeverityKey = "severity" HttpRequestKey = "httpRequest" )
Standard JSON log fields as per https://cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields
const ( LabelsKey = "logging.googleapis.com/labels" OperationKey = "logging.googleapis.com/operation" SourceLocationKey = "logging.googleapis.com/sourceLocation" TraceKey = trace.TraceKey SpanIDKey = trace.SpanIDKey TraceSampledKey = trace.TraceSampledKey )
Extended JSON log fields as per https://cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields
const ( LevelDefault slog.Level = -5 LevelDebug slog.Level = slog.LevelDebug // -4 LevelInfo slog.Level = slog.LevelInfo // 0 LevelNotice slog.Level = 2 LevelWarning slog.Level = slog.LevelWarn // 4 ⚠️ Warning vs Warn LevelError slog.Level = slog.LevelError // 8 LevelCritical slog.Level = 9 LevelAlert slog.Level = 10 LevelEmergency slog.Level = 11 )
The levels' name and order match https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity but the internal int values differ.
The internal int values are changed so that they can be used with log/slog.Log for logging at a custom level, for example:
slog.Log(ctx, clog.LevelCritical, "flux capacitor exploded")
Variables ¶
var ErrInvalidHandlerOptions = errors.New("invalid HandlerOptions")
Functions ¶
func LevelName ¶
LevelName returns the Google Cloud Logging name (in uppercase) for the level as per https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
Examples:
LevelName(LevelWarning) => "WARNING"
func ReplaceAttr ¶
ReplaceAttr replaces attr keys and values to fit Google Cloud Logging's expected structure as per: - https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry - https://cloud.google.com/functions/docs/monitoring/logging - https://cloud.google.com/functions/docs/monitoring/logging
Types ¶
type Handler ¶ added in v0.3.0
type Handler struct {
// contains filtered or unexported fields
}
Handler is a log/slog.JSONHandler preconfigured for Google Cloud Logging.
func NewHandler ¶
func NewHandler(w io.Writer, opts HandlerOptions) (*Handler, error)
func (*Handler) Enabled ¶ added in v0.3.0
Enabled implements log/slog.Handler.
func (*Handler) Handle ¶ added in v0.3.0
Handle implements log/slog.Handler.
func (*Handler) WithAttrs ¶ added in v0.3.0
WithAttrs implements log/slog.Handler.