-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1029 from synfinatic/console-logger
switch to console logger by default
- Loading branch information
Showing
15 changed files
with
91 additions
and
20 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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package logger | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/mattn/go-isatty" | ||
"github.com/synfinatic/flexlog" | ||
) | ||
|
||
var logger flexlog.FlexLogger | ||
var CreateLogger flexlog.NewLoggerFunc | ||
|
||
// initialize the default logger to log to stderr and log at the warn level | ||
func init() { | ||
w := os.Stderr | ||
color := isatty.IsTerminal(w.Fd()) | ||
addSource := false | ||
level := slog.LevelWarn | ||
|
||
logger = flexlog.NewLogger(flexlog.NewConsole, w, addSource, level, color) | ||
|
||
slog.SetDefault(logger.GetLogger()) | ||
} | ||
|
||
func SetLogger(l flexlog.FlexLogger) { | ||
logger = l | ||
} | ||
|
||
func GetLogger() flexlog.FlexLogger { | ||
return logger | ||
} | ||
|
||
func SetDefaultLogger(l flexlog.FlexLogger) { | ||
slog.SetDefault(l.GetLogger()) | ||
} | ||
|
||
// SwitchLogger changes the current logger to the specified type | ||
func SwitchLogger(name string) { | ||
var loggers = map[string]flexlog.NewLoggerFunc{ | ||
"console": flexlog.NewConsole, | ||
"json": flexlog.NewJSON, | ||
"tint": flexlog.NewTint, | ||
} | ||
var ok bool | ||
CreateLogger, ok = loggers[name] | ||
if !ok { | ||
logger.Fatal("Invalid logger", "name", name) | ||
} | ||
|
||
// switch the logger | ||
logger = flexlog.NewLogger(CreateLogger, logger.Writer(), logger.AddSource(), logger.Level(), logger.Color()) | ||
slog.SetDefault(logger.GetLogger()) | ||
} |
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
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