diff --git a/examples/go.mod b/examples/go.mod new file mode 100644 index 0000000..9813918 --- /dev/null +++ b/examples/go.mod @@ -0,0 +1,12 @@ +module github.com/fogfish/logger/examples + +go 1.23.1 + +replace github.com/fogfish/logger/v3 => ../ + +replace github.com/fogfish/logger/x/xlog => ../x/xlog + +require ( + github.com/fogfish/logger/v3 v3.2.0 + github.com/fogfish/logger/x/xlog v0.0.0-00010101000000-000000000000 +) diff --git a/examples/go.sum b/examples/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/examples/observability/main.go b/examples/observability/main.go new file mode 100644 index 0000000..cc9a970 --- /dev/null +++ b/examples/observability/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "log/slog" + "time" + + log "github.com/fogfish/logger/v3" + "github.com/fogfish/logger/x/xlog" +) + +func do() { + defer slog.Info("done something", slog.Any("duration", xlog.SinceNow())) + + time.Sleep(100 * time.Millisecond) +} + +func rate() { + ops := xlog.PerSecondNow() + defer slog.Info("done something", slog.Any("op/sec", ops)) + + time.Sleep(100 * time.Millisecond) + ops.Acc += 100 +} + +func demand() { + ops := xlog.MillisecondOpNow() + defer slog.Info("done something", slog.Any("ms/op", ops)) + + time.Sleep(100 * time.Millisecond) + ops.Acc += 10 +} + +func main() { + slog.SetDefault(log.New()) + do() + rate() + demand() +} diff --git a/examples/slog/main.go b/examples/slog/main.go new file mode 100644 index 0000000..6868251 --- /dev/null +++ b/examples/slog/main.go @@ -0,0 +1,32 @@ +// +// Copyright (C) 2021 - 2025 Dmitry Kolesnikov +// +// This file may be modified and distributed under the terms +// of the MIT license. See the LICENSE file for details. +// https://github.com/fogfish/logger +// + +package main + +import ( + "context" + "log/slog" + + log "github.com/fogfish/logger/v3" +) + +func main() { + slog.SetDefault(log.New(log.WithLogLevel(log.DEBUG))) + + obj := slog.Group("obj", + slog.Any("key", "val"), + ) + + slog.Debug("debug status about system.", obj) + slog.Info("informative status about system.", obj) + slog.Log(context.Background(), log.NOTICE, "system is failed, error is recovered, no impact.", obj) + slog.Warn("system is failed, unable to recover, degraded functionality.", obj) + slog.Error("system is failed, unable to recover from error.", obj) + slog.Log(context.Background(), log.CRITICAL, "system is failed, response actions must be taken immediately. Doing graceful exit.", obj) + slog.Log(context.Background(), log.EMERGENCY, "system is unusable, panic execution of app.", obj) +} diff --git a/examples/xlog/main.go b/examples/xlog/main.go new file mode 100644 index 0000000..c571eba --- /dev/null +++ b/examples/xlog/main.go @@ -0,0 +1,33 @@ +// +// Copyright (C) 2021 - 2025 Dmitry Kolesnikov +// +// This file may be modified and distributed under the terms +// of the MIT license. See the LICENSE file for details. +// https://github.com/fogfish/logger +// + +package main + +import ( + "io" + "log/slog" + + log "github.com/fogfish/logger/v3" + "github.com/fogfish/logger/x/xlog" +) + +func main() { + slog.SetDefault(log.New(log.WithLogLevel(log.DEBUG))) + + obj := slog.Group("obj", + slog.Any("key", "val"), + ) + + slog.Debug("debug status about system.", obj) + slog.Info("informative status about system.", obj) + xlog.Notice("system is failed, error is recovered, no impact.", obj) + xlog.Warn("system is failed, unable to recover, degraded functionality.", io.EOF, obj) + xlog.Error("system is failed, unable to recover from error.", io.EOF, obj) + xlog.Critical("system is failed, response actions must be taken immediately. Doing graceful exit.", io.EOF, obj) + xlog.Emergency("system is unusable, panic execution of app.", io.EOF, obj) +}