Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyTony committed Oct 10, 2023
1 parent 1e986cd commit 9dac38b
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 86 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
# Go workspace file
go.work

/logs/*

# idea
.idea
92 changes: 92 additions & 0 deletions level.go
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)
}
6 changes: 6 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package logger

import (
"context"
"errors"
)

var (
// ErrLogPathNotSet is an error that indicates the log path is not set.
ErrLogPathNotSet = errors.New("log path must be set")
)

// Logger is the interface for Logger types
Expand Down
Loading

0 comments on commit 9dac38b

Please sign in to comment.