-
Notifications
You must be signed in to change notification settings - Fork 326
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
8 changed files
with
124 additions
and
6 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.vscode/ | ||
.env | ||
.env | ||
go.sum |
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,106 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
) | ||
|
||
const ( | ||
// LevelError 错误 | ||
LevelError = iota | ||
// LevelWarning 警告 | ||
LevelWarning | ||
// LevelInformational 提示 | ||
LevelInformational | ||
// LevelDebug 除错 | ||
LevelDebug | ||
) | ||
|
||
var logger *Logger | ||
|
||
// Logger 日志 | ||
type Logger struct { | ||
level int | ||
} | ||
|
||
// Println 打印 | ||
func (ll *Logger) Println(msg string) { | ||
fmt.Printf("%s %s", time.Now().Format("2006-01-02 15:04:05 -0700"), msg) | ||
} | ||
|
||
// Panic 极端错误 | ||
func (ll *Logger) Panic(format string, v ...interface{}) { | ||
if LevelError > ll.level { | ||
return | ||
} | ||
msg := fmt.Sprintf("[Panic] "+format, v...) | ||
ll.Println(msg) | ||
os.Exit(0) | ||
} | ||
|
||
// Error 错误 | ||
func (ll *Logger) Error(format string, v ...interface{}) { | ||
if LevelError > ll.level { | ||
return | ||
} | ||
msg := fmt.Sprintf("[E] "+format, v...) | ||
ll.Println(msg) | ||
} | ||
|
||
// Warning 警告 | ||
func (ll *Logger) Warning(format string, v ...interface{}) { | ||
if LevelWarning > ll.level { | ||
return | ||
} | ||
msg := fmt.Sprintf("[W] "+format, v...) | ||
ll.Println(msg) | ||
} | ||
|
||
// Info 信息 | ||
func (ll *Logger) Info(format string, v ...interface{}) { | ||
if LevelInformational > ll.level { | ||
return | ||
} | ||
msg := fmt.Sprintf("[I] "+format, v...) | ||
ll.Println(msg) | ||
} | ||
|
||
// Debug 校验 | ||
func (ll *Logger) Debug(format string, v ...interface{}) { | ||
if LevelDebug > ll.level { | ||
return | ||
} | ||
msg := fmt.Sprintf("[D] "+format, v...) | ||
ll.Println(msg) | ||
} | ||
|
||
// BuildLogger 构建logger | ||
func BuildLogger(level string) { | ||
intLevel := LevelError | ||
switch level { | ||
case "error": | ||
intLevel = LevelError | ||
case "warning": | ||
intLevel = LevelWarning | ||
case "info": | ||
intLevel = LevelInformational | ||
case "debug": | ||
intLevel = LevelDebug | ||
} | ||
l := Logger{ | ||
level: intLevel, | ||
} | ||
logger = &l | ||
} | ||
|
||
// Log 返回日志对象 | ||
func Log() *Logger { | ||
if logger == nil { | ||
l := Logger{ | ||
level: LevelDebug, | ||
} | ||
logger = &l | ||
} | ||
return logger | ||
} |