-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
88 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Change: Switch to official logging library | ||
|
||
Since there have been a structured logger part of the Go standard library we | ||
thought it's time to replace the library with that. Be aware that log messages | ||
should change a little bit. | ||
|
||
https://github.com/promhippie/scw_exporter/issues/104 |
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
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 |
---|---|---|
@@ -1,42 +1,40 @@ | ||
package command | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/promhippie/scw_exporter/pkg/config" | ||
) | ||
|
||
func setupLogger(cfg *config.Config) log.Logger { | ||
var logger log.Logger | ||
|
||
func setupLogger(cfg *config.Config) *slog.Logger { | ||
if cfg.Logs.Pretty { | ||
logger = log.NewSyncLogger( | ||
log.NewLogfmtLogger(os.Stdout), | ||
) | ||
} else { | ||
logger = log.NewSyncLogger( | ||
log.NewJSONLogger(os.Stdout), | ||
return slog.New( | ||
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: loggerLevel(cfg), | ||
}), | ||
) | ||
} | ||
|
||
return slog.New( | ||
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: loggerLevel(cfg), | ||
}), | ||
) | ||
} | ||
|
||
func loggerLevel(cfg *config.Config) slog.Leveler { | ||
switch strings.ToLower(cfg.Logs.Level) { | ||
case "error": | ||
logger = level.NewFilter(logger, level.AllowError()) | ||
return slog.LevelError | ||
case "warn": | ||
logger = level.NewFilter(logger, level.AllowWarn()) | ||
return slog.LevelWarn | ||
case "info": | ||
logger = level.NewFilter(logger, level.AllowInfo()) | ||
return slog.LevelInfo | ||
case "debug": | ||
logger = level.NewFilter(logger, level.AllowDebug()) | ||
default: | ||
logger = level.NewFilter(logger, level.AllowInfo()) | ||
return slog.LevelDebug | ||
} | ||
|
||
return log.With( | ||
logger, | ||
"ts", log.DefaultTimestampUTC, | ||
) | ||
return slog.LevelInfo | ||
} |
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
Oops, something went wrong.