gologger

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 13 Imported by: 0

README

gologger

My small logger library. This is not meant to be used by anyone, I just kept copy pasting this in my projects and thought it is time to have it versioned as dependency.

It has a global extensible logger, featuring 2 extensions:

  • console, basically just standard slog
  • loki, pushing logs in batches to a Loki instance# gologger

Usage

// with loki
ctx := context.Background()
lokiOpt, err := gologger.WithLoki(ctx, "http://loki:3100/", "eu-1", "my-service", gologger.WithLevels([]slog.Level{slog.LevelError}))
if err != nil {
    panic(err)
}
gologger.Init(slog.LevelWarn, lokiOpt)

gologger.Info("not logged at all")
gologger.Warn("not sent to loki, but printed to stdout")
gologger.Error("sent to loki and stdout")
func handleError(err error) {
    if err != nil {
        panic(err)
    }
}

// parsing config
type LogConfig struct {
	Level string
	Loki  string
}

ctx := context.Background()
cfg := LogConfig{Level: "warn", Loki: "http://loki:3100/"}
logLvl, err := gologger.ParseLogLevel(cfg.Level)
handleError(err)
loggerOpts := make([]gologger.Option, 0)
if cfg.Loki != "" {
	lokiOpt, err := gologger.WithLoki(ctx, "http://loki:3100/", "eu-1", "my-service", gologger.WithLevels([]slog.Level{slog.LevelError}))
	handleError(err)
	loggerOpts = append(loggerOpts, lokiOpt)
}
gologger.Init(logLvl, loggerOpts...)

gologger.Info("not logged at all")
gologger.Warn("not sent to loki, but printed to stdout")
gologger.Error("sent to loki and stdout")
// with alertmanager
ctx := context.Background()
lokiOpt, err := gologger.WithAlertManager(ctx, "http://loki:3100/", "eu-1", "my-service", gologger.WithLevels([]slog.Level{slog.LevelError}))
if err != nil {
    panic(err)
}
gologger.Init(slog.LevelWarn, lokiOpt)

gologger.Info("not logged at all")
gologger.Warn("not sent to loki, but printed to stdout")
gologger.Error("sent to loki and stdout")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert

func Alert(name string, summary string, startsAt, endsAt time.Time, labels, annotations map[string]string, generatorURL string) error

NewAlert creates a new alert with the given parameters name is the name of the alert. If it is empty, an error will be returned. summary is a short description of the alert. If it is empty, an error will be returned. startsAt is the time when the alert starts, endsAt is the time when the alert ends. If they are omitted the current time will be used for both. If only one is omitted, the other will be set to it as well. If startsAt is after endsAt an error will be returned. labels are necessary information that will be sent to the alertmanager. The name, instance and baseLabels will be added to the annotations and overwrite existing keys. annotations are additional information that will be sent to the alertmanager. The summary will be added to the annotations. A common and recommended annotation is "summary" with a short description of the alert. generatorURL is the URL of the service that sends the alert. It is optional.

func ApplyOptions

func ApplyOptions(options ...Option) error

func Debug

func Debug(message string, additionaValues map[string]any)

func Error

func Error(message string, additionaValues map[string]any)

func Info

func Info(message string, additionaValues map[string]any)

func Init

func Init(level slog.Level, options ...Option) error

func IsInitialized added in v0.1.3

func IsInitialized() bool

func Log

func Log(level slog.Level, message string, additionaValues map[string]any)

func MustParseLogLevel added in v0.1.3

func MustParseLogLevel(s string) slog.Level

MustParseLogLevel is like ParseLogLevel but panics if the input is invalid

func OnDebug

func OnDebug(callback Callback)

func OnErr

func OnErr(callback Callback)

func OnInfo

func OnInfo(callback Callback)

func OnWarn

func OnWarn(callback Callback)

func ParseLogLevel

func ParseLogLevel(s string) (slog.Level, error)

ParseLogLevel parses a string into a slog.Level

func SetLevel

func SetLevel(level slog.Level)

func Warn

func Warn(message string, additionaValues map[string]any)

func WithAlertManager

func WithAlertManager(ctx context.Context, alertmanagerHost string, instance, service string, baseLabels map[string]string) error

WithAlertManager sets up the logger to send alerts to an alertmanager instance the context is used to check if the alertmanager instance is reachable AND for the runtime if the context is cancelled, the alertmanager will stop sending alerts alertmanagerHost is the host of the alertmanager instance baseLabels are the labels that will be added to all alerts, e.g. {"instance": "my-service"} returns an error if the alertmanager instance is not reachable

Types

type AlertQueue

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

type Callback

type Callback func(msg string, additionalValues map[string]any)

type FileLoggerOption

type FileLoggerOption func(*fileLogger)

type LokiNotifier

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

type LokiOption

type LokiOption func(loki *LokiNotifier)

func WithBatchWait

func WithBatchWait(duration time.Duration) LokiOption

func WithLevels

func WithLevels(levels []slog.Level) LokiOption

type Option

type Option func(*logger)

func WithFile

func WithFile(filepath string, opts ...FileLoggerOption) (Option, error)

func WithLoki

func WithLoki(ctx context.Context, lokiHost, server, job string, opts ...LokiOption) (Option, error)

WithLoki sets up the logger to send logs to a loki instance the context is used to check if the loki instance is reachable AND for the runtime if the context is cancelled, the loki will stop sending logs lokiHost is the host of the loki instance server is the name of the server that sends the logs job is the name of the job that sends the logs returns an error if the loki instance is not reachable or if the server or job is not set

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL