Skip to content

Commit 05c9a0a

Browse files
committed
improve logger
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 1396152 commit 05c9a0a

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/webdevops/go-common
22

3-
go 1.25
3+
go 1.25.0
4+
5+
toolchain go1.25.5
46

57
require (
68
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.20.0

log/slogger/logger.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type LoggerOptionFunc func(*Options)
1212

13+
// WithLevelText sets the log level as string
1314
func WithLevelText(val string) LoggerOptionFunc {
1415
level, err := TranslateToLogLevel(val)
1516
if err != nil {
@@ -21,12 +22,14 @@ func WithLevelText(val string) LoggerOptionFunc {
2122
}
2223
}
2324

25+
// WithLevel sets the log level as slog.Level
2426
func WithLevel(level slog.Level) LoggerOptionFunc {
2527
return func(opt *Options) {
2628
opt.Level = level
2729
}
2830
}
2931

32+
// WithSourceMode sets the source (file, line) mode
3033
func WithSourceMode(mode SourceMode) LoggerOptionFunc {
3134
switch mode {
3235
case "":
@@ -47,6 +50,7 @@ func WithSourceMode(mode SourceMode) LoggerOptionFunc {
4750
}
4851
}
4952

53+
// WithFormat sets the mode (logfmt or json)
5054
func WithFormat(mode FormatMode) LoggerOptionFunc {
5155
switch mode {
5256
case "":
@@ -62,18 +66,32 @@ func WithFormat(mode FormatMode) LoggerOptionFunc {
6266
}
6367
}
6468

69+
// WithColor sets if logs should be colorful or not
6570
func WithColor(mode ColorMode) LoggerOptionFunc {
6671
return func(opt *Options) {
6772
opt.SetColorMode(mode)
6873
}
6974
}
7075

76+
// WithTime sets if logs lines should also include the time (not useful for containers)
7177
func WithTime(v bool) LoggerOptionFunc {
7278
return func(opt *Options) {
7379
opt.ShowTime = v
7480
}
7581
}
7682

83+
// NewFromSlog converts a slog.Logger to an slogger.Logger
84+
func NewFromSlog(logger *slog.Logger) *Logger {
85+
return &Logger{logger}
86+
}
87+
88+
// NewDiscardLogger creates a logger which discards all logs (send logs to /dev/null)
89+
func NewDiscardLogger() *Logger {
90+
loggerOptions := NewOptions(nil)
91+
return newLoggerHandler(io.Discard, loggerOptions)
92+
}
93+
94+
// NewDaemonLogger creates a logger with defaults for daemons
7795
func NewDaemonLogger(w io.Writer, opts ...LoggerOptionFunc) *Logger {
7896
loggerOptions := NewOptions(nil).
7997
SetLevel(LevelInfo).
@@ -83,6 +101,7 @@ func NewDaemonLogger(w io.Writer, opts ...LoggerOptionFunc) *Logger {
83101
return newLoggerHandler(w, loggerOptions, opts...)
84102
}
85103

104+
// NewCliLogger creates a cli logger with defaults for cli tasks
86105
func NewCliLogger(w io.Writer, opts ...LoggerOptionFunc) *Logger {
87106
loggerOptions := NewOptions(nil).
88107
SetLevel(LevelInfo).

0 commit comments

Comments
 (0)