func cloudLoggingAttrsEncoder()

in pkg/berglas/logging/logging.go [128:171]


func cloudLoggingAttrsEncoder() func([]string, slog.Attr) slog.Attr {
	const (
		keySeverity = "severity"
		keyError    = "error"
		keyMessage  = "message"
		keySource   = "logging.googleapis.com/sourceLocation"
	)

	return func(groups []string, a slog.Attr) slog.Attr {
		// Google Cloud Logging uses "severity" instead of "level":
		// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
		if a.Key == slog.LevelKey {
			a.Key = keySeverity

			// Use the custom level names to match Google Cloud logging.
			val := a.Value.Any()
			typ, ok := val.(slog.Level)
			if !ok {
				panic(fmt.Sprintf("level is not slog.Level (got %T)", val))
			}
			a.Value = LevelSlogValue(typ)
		}

		// Google Cloud Logging uses "message" instead of "msg":
		// https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
		if a.Key == slog.MessageKey {
			a.Key = keyMessage
		}

		// Google Cloud Logging uses "logging.google..." instead of "source":
		// https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
		if a.Key == slog.SourceKey {
			a.Key = keySource
		}

		// Re-format durations to be their string format.
		if a.Value.Kind() == slog.KindDuration {
			val := a.Value.Duration()
			a.Value = slog.StringValue(humanDuration(val))
		}

		return a
	}
}