sloggcp

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2024 License: Apache-2.0 Imports: 4 Imported by: 2

README

sloggcp

A log/slog handler for Google Cloud Logging, enabling seamless integration with Google Cloud's logging infrastructure.

Quick Start

Installation

Ensure you have the Google Cloud SDK installed and authenticated. Then, add sloggcp and necessary dependencies:

go get github.com/Permify/sloggcp
Usage

Below is a minimal example of configuring and using sloggcp to send logs to Google Cloud Logging.

package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/Permify/sloggcp"
)

func main() {
	// Set up context and Google Cloud project ID
	ctx := context.Background()
	projectID := "YOUR_PROJECT_ID"
	logName := "my-application-log"

	// Create a new GoogleCloudSlogHandler
	handler, err := sloggcp.NewGoogleCloudSlogHandler(ctx, projectID, logName, slog.LevelInfo)
	if err != nil {
		slog.Error("Failed to create Google Cloud slog handler", slog.String("error", err.Error()))
		os.Exit(1)
	}
	defer handler.Close()

	// Set up the slog logger with the Google Cloud handler
	logger := slog.New(handler)
	slog.SetDefault(logger)

	// Log messages at different levels
	logger.Info("Application started", slog.String("env", "production"))
	logger.Debug("Debugging info", slog.String("module", "auth"))
	logger.Warn("Potential issue detected", slog.String("component", "database"))
	logger.Error("An error occurred", slog.String("error", "database connection failed"))

	// Optional: Close the handler when done to ensure logs are flushed
	if err := handler.Close(); err != nil {
		slog.Error("Failed to close the Google Cloud slog handler", slog.String("error", err.Error()))
	}
}
Explanation
  • Project ID and Log Name: Set projectID and logName to your Google Cloud Project ID and desired log name.
  • slog Handler: GoogleCloudSlogHandler sends logs directly to Google Cloud, handling attributes and severity levels automatically.
  • Logging Levels: Log messages can be sent with various levels (Info, Debug, Warn, Error), automatically converting to the corresponding Google Cloud severity level.
Environment Setup

Make sure the environment running this code has the necessary permissions to write to Google Cloud Logging. You can achieve this by:

  1. Setting up authentication with a service account key.
  2. Configuring the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the key file.
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
Testing Logs

After running the example, go to Google Cloud Logging and check for entries under the specified log name (my-application-log).

go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version is the current release version of GCP Cloud Logging Slog in use.

Types

type GoogleCloudSlogHandler

type GoogleCloudSlogHandler struct {
	// contains filtered or unexported fields
}

GoogleCloudSlogHandler wraps Google Cloud Logging's Logger for use with slog.

func NewGoogleCloudSlogHandler

func NewGoogleCloudSlogHandler(ctx context.Context, projectID, logName string, opts *slog.HandlerOptions) *GoogleCloudSlogHandler

NewGoogleCloudSlogHandler initializes a new GoogleCloudSlogHandler.

func (*GoogleCloudSlogHandler) Close

func (h *GoogleCloudSlogHandler) Close() error

Close closes the Google Cloud Logging client.

func (*GoogleCloudSlogHandler) Enabled

func (h *GoogleCloudSlogHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*GoogleCloudSlogHandler) Handle

Handle adapts slog.Record entries to Google Cloud Logging entries.

func (*GoogleCloudSlogHandler) WithAttrs

func (h *GoogleCloudSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*GoogleCloudSlogHandler) WithGroup

func (h *GoogleCloudSlogHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳