-
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.
- Loading branch information
JellyTony
committed
Oct 10, 2023
1 parent
1e986cd
commit 9dac38b
Showing
6 changed files
with
289 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,7 @@ | |
# Go workspace file | ||
go.work | ||
|
||
/logs/* | ||
|
||
# idea | ||
.idea |
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,92 @@ | ||
package logger | ||
|
||
import ( | ||
"strings" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type Level int8 | ||
|
||
const ( | ||
// DebugLevel level. Usually only enabled when debugging. Very verbose logging. | ||
DebugLevel = iota + 1 | ||
// InfoLevel is the default logging priority. | ||
// General operational entries about what's going on inside the application. | ||
InfoLevel | ||
// WarnLevel level. Non-critical entries that deserve eyes. | ||
WarnLevel | ||
// ErrorLevel level. Logs. Used for errors that should definitely be noted. | ||
ErrorLevel | ||
// FatalLevel level. Logs and then calls `logger.Exit(1)`. highest level of severity. | ||
FatalLevel | ||
) | ||
|
||
func (l Level) String() string { | ||
switch l { | ||
case DebugLevel: | ||
return "DEBUG" | ||
case InfoLevel: | ||
return "INFO" | ||
case WarnLevel: | ||
return "WARN" | ||
case ErrorLevel: | ||
return "ERROR" | ||
case FatalLevel: | ||
return "FATAL" | ||
} | ||
return "" | ||
} | ||
|
||
// ParseLevel parses a level string into a logger Level value. | ||
func ParseLevel(s string) Level { | ||
switch strings.ToUpper(s) { | ||
case "DEBUG": | ||
return DebugLevel | ||
case "INFO": | ||
return InfoLevel | ||
case "WARN": | ||
return WarnLevel | ||
case "ERROR": | ||
return ErrorLevel | ||
case "FATAL": | ||
return FatalLevel | ||
} | ||
return InfoLevel | ||
} | ||
|
||
func (l Level) unmarshalZapLevel() zapcore.Level { | ||
switch l { | ||
case DebugLevel: | ||
return zap.DebugLevel | ||
case InfoLevel: | ||
return zap.InfoLevel | ||
case WarnLevel: | ||
return zap.WarnLevel | ||
case ErrorLevel: | ||
return zap.ErrorLevel | ||
case FatalLevel: | ||
return zap.FatalLevel | ||
default: | ||
return zap.InfoLevel | ||
} | ||
} | ||
|
||
// Enabled returns true if the given level is at or above this level. | ||
func (l Level) Enabled(lvl Level) bool { | ||
return lvl >= l | ||
} | ||
|
||
// LevelEnablerFunc is a convenient way to implement zapcore.LevelEnabler with | ||
// an anonymous function. | ||
// | ||
// It's particularly useful when splitting log output between different | ||
// outputs (e.g., standard error and standard out). For sample code, see the | ||
// package-level AdvancedConfiguration example. | ||
type LevelEnablerFunc func(zapcore.Level) bool | ||
|
||
// Enabled calls the wrapped function. | ||
func (f LevelEnablerFunc) Enabled(lvl zapcore.Level) bool { | ||
return f(lvl) | ||
} |
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.