Documentation
¶
Index ¶
- Constants
- func FlushLogger(l *slog.Logger) error
- func NewDevelopmentLogger(ctx context.Context, config Config) (*slog.Logger, error)
- func NewGoogleCloudLogger(ctx context.Context, config Config) (*slog.Logger, error)
- type Config
- type DevelopmentHandler
- func (h *DevelopmentHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h *DevelopmentHandler) Flush() error
- func (h *DevelopmentHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *DevelopmentHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *DevelopmentHandler) WithGroup(name string) slog.Handler
- type GoogleCloudLoggingHandler
- func (h *GoogleCloudLoggingHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h *GoogleCloudLoggingHandler) Flush() error
- func (h *GoogleCloudLoggingHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *GoogleCloudLoggingHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *GoogleCloudLoggingHandler) WithGroup(name string) slog.Handler
Constants ¶
const ( DebugColor = "\033[36m" // Cyan for Debug InfoColor = "\033[32m" // Green for Info WarnColor = "\033[33m" // Yellow for Warn ErrorColor = "\033[31m" // Red for Error ResetColor = "\033[0m" // Reset to default terminal color )
ANSI color codes for different log levels
Variables ¶
This section is empty.
Functions ¶
func FlushLogger ¶
FlushLogger attempts to flush the logs for the provided slog.Logger. It supports flushing for loggers using either GoogleCloudLoggingHandler or DevelopmentHandler. If the logger does not support flushing, an error is returned.
func NewDevelopmentLogger ¶
NewDevelopmentLogger sets up a logger for development environments using a custom slog handler. This logger prints log entries to the console in a human-readable format. It includes: - Timestamps with millisecond precision. - Log levels (DEBUG, INFO, WARN, ERROR) in brackets for easy identification. - The log message itself. - Structured key-value data (attributes) when provided, appended after the message. - For ERROR-level logs, a stack trace is included, showing the file and line number of the error origin. This logging setup is useful for local development as it makes it easier to spot issues, read structured data, and debug errors directly from the console output.
func NewGoogleCloudLogger ¶
NewGoogleCloudLogger sets up a logger for Google Cloud Logging. It validates the provided configuration, initializes a Google Cloud Logging client, creates a custom slog handler for Google Cloud Logging, and returns an slog.Logger.
Types ¶
type Config ¶
type Config struct {
GCPProjectID string // GCPProjectID is the Google Cloud Project ID where logs will be sent.
LogName string // LogName is the name of the log stream where entries will be written.
Level slog.Level // Level is the minimum log level that will be captured (e.g., DEBUG, INFO).
}
Config holds configuration details for setting up logging.
type DevelopmentHandler ¶
type DevelopmentHandler struct {
// contains filtered or unexported fields
}
DevelopmentHandler is a custom handler for slog used in development environments. It outputs logs to the console with formatted messages and structured data.
func (*DevelopmentHandler) Enabled ¶
Enabled reports whether the provided log level is enabled for this handler.
func (*DevelopmentHandler) Flush ¶
func (h *DevelopmentHandler) Flush() error
Flush is a required handler method for the slog.Handler interface. In a development environment, there is no buffered output to flush, so this method simply returns nil.
func (*DevelopmentHandler) Handle ¶
Handle processes log records for development use, printing them to the console with a timestamp, the appropriate color based on log level, and a reset color afterward. It also includes any structured key-value data associated with the log record. For error logs, it attempts to append the relevant file and line number where the log was generated.
func (*DevelopmentHandler) WithAttrs ¶
func (h *DevelopmentHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs is required to satisfy the slog.Handler interface. This method would typically return a new handler with additional attributes, but since attribute handling is not needed, it returns the original handler unchanged.
func (*DevelopmentHandler) WithGroup ¶
func (h *DevelopmentHandler) WithGroup(name string) slog.Handler
WithGroup is required to satisfy the slog.Handler interface. This method would typically return a new handler that groups log attributes, but since grouping is not needed, it returns the original handler unchanged.
type GoogleCloudLoggingHandler ¶
type GoogleCloudLoggingHandler struct {
// contains filtered or unexported fields
}
GoogleCloudLoggingHandler is a custom handler for slog used to send logs to Google Cloud Logging.
func (*GoogleCloudLoggingHandler) Enabled ¶
Enabled reports whether the provided log level is enabled for this handler.
func (*GoogleCloudLoggingHandler) Flush ¶
func (h *GoogleCloudLoggingHandler) Flush() error
Flush sends any buffered log entries to Google Cloud Logging and waits for all logs to be fully processed. It ensures that logs are properly flushed before shutting down the service or completing operations that depend on log delivery.
func (*GoogleCloudLoggingHandler) Handle ¶
Handle processes a slog.Record by converting it into a Google Cloud Logging entry. It extracts the log message and any associated structured attributes (key-value pairs), maps the slog log level to Google Cloud Logging severity, and forwards the log entry to Google Cloud Logging.
func (*GoogleCloudLoggingHandler) WithAttrs ¶
func (h *GoogleCloudLoggingHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs is required to satisfy the slog.Handler interface. This method would typically return a new handler with additional attributes, but since attribute handling is not needed, it returns the original handler unchanged.
func (*GoogleCloudLoggingHandler) WithGroup ¶
func (h *GoogleCloudLoggingHandler) WithGroup(name string) slog.Handler
WithGroup is required to satisfy the slog.Handler interface. This method would typically return a new handler that groups log attributes, but since grouping is not needed, it returns the original handler unchanged.