Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewAutoHandler ¶
func NewAutoHandler(w io.Writer, opts *HandlerOptions) slog.Handler
NewAutoHandler returns slog.Handler that writes to w using GCP structured logging format. It automatically detects GCP project ID. If the program is not running on GCE, it returns console handler.
Example ¶
package main
import (
"fmt"
"log/slog"
"os"
"github.com/mycujoo/go-stdlib/pkg/gcplog"
)
func main() {
h := gcplog.NewAutoHandler(os.Stderr, &gcplog.HandlerOptions{
AddSource: true,
ServiceName: "some-service",
ReportErrors: true,
})
err := fmt.Errorf("storage.Get: %w", os.ErrNotExist)
logger := slog.New(h)
logger.Error("operation failed", gcplog.Error(err))
}
Output:
func NewReportContext ¶
NewReportContext creates a new report context. see: https://cloud.google.com/error-reporting/docs/formatting-error-messages
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
func NewHandler(w io.Writer, opts *HandlerOptions) *Handler
NewHandler returns slog.Handler that writes to w using GCP structured logging format.
type HandlerOptions ¶
type HandlerOptions struct {
// AddSource causes the handler to compute the source code position
// of the log statement and add a SourceKey attribute to the output.
AddSource bool
// Minimal log level to log, defaults to slog.LevelInfo
Level slog.Leveler
// Service name and version to add to the log
ServiceName string
ServiceVersion string
// If this is set to true, errors will be reported to GCP error reporting.
ReportErrors bool
// GCP project ID to use for trace context
GCPProjectID string
}
Click to show internal directories.
Click to hide internal directories.