forked from kuskoman/logstash-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted from log to slog (kuskoman#154)
- Loading branch information
1 parent
bd01426
commit cd0f569
Showing
7 changed files
with
129 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"golang.org/x/exp/slog" | ||
) | ||
|
||
var ( | ||
LogLevel = getEnvWithDefault("LOG_LEVEL", "info") | ||
LogFormat = getEnvWithDefault("LOG_FORMAT", "text") | ||
|
||
ErrUnknownLogFormat = fmt.Errorf("unknown log format") | ||
) | ||
|
||
func SetupSlog() (*slog.Logger, error) { | ||
level := slog.LevelInfo | ||
err := level.UnmarshalText([]byte(LogLevel)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var handler slog.Handler | ||
switch strings.ToLower(LogFormat) { | ||
case "text": | ||
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: level, | ||
}) | ||
case "json": | ||
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: level, | ||
}) | ||
default: | ||
return nil, ErrUnknownLogFormat | ||
} | ||
|
||
return slog.New(handler), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestSetupSlog(t *testing.T) { | ||
t.Run("Wrong type of log level", func(t *testing.T) { | ||
LogLevel = "infox" | ||
logger, err := SetupSlog() | ||
if logger != nil { | ||
t.Errorf("Expected logger to be nil, got %s\"", logger) | ||
} | ||
if err.Error() != "slog: level string \"infox\": unknown name" { | ||
t.Errorf("Expected error to be '%s', got %s\"", "slog: level string \"infox\": unknown name", err) | ||
} | ||
}) | ||
t.Run("Wrong type of log level", func(t *testing.T) { | ||
LogLevel = "warn" | ||
logger, err := SetupSlog() | ||
if err != nil { | ||
t.Errorf("Expected error to be nil, got %s\"", err) | ||
} | ||
if logger == nil { | ||
t.Errorf("Expected logger to be not nil, got %s\"", logger) | ||
} | ||
}) | ||
t.Run("Wrong type of log format", func(t *testing.T) { | ||
LogFormat = "test" | ||
logger, err := SetupSlog() | ||
if logger != nil { | ||
t.Errorf("Expected logger to be nil, got %s\"", logger) | ||
} | ||
if !errors.Is(err, ErrUnknownLogFormat) { | ||
t.Errorf("Expected error to be '%s', got %s\"", ErrUnknownLogFormat, err) | ||
} | ||
}) | ||
t.Run("Correct type of log format", func(t *testing.T) { | ||
LogFormat = "text" | ||
logger, err := SetupSlog() | ||
if err != nil { | ||
t.Errorf("Expected error to be nil, got %s\"", err) | ||
} | ||
if logger == nil { | ||
t.Errorf("Expected logger to be not nil, got %s\"", logger) | ||
} | ||
}) | ||
t.Run("Json type of log format", func(t *testing.T) { | ||
LogFormat = "json" | ||
logger, err := SetupSlog() | ||
if err != nil { | ||
t.Errorf("Expected error to be nil, got %s\"", err) | ||
} | ||
if logger == nil { | ||
t.Errorf("Expected logger to be not nil, got %s\"", logger) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters