wangpl_study/day06/mylogger_test/console.go

48 lines
799 B
Go
Raw Permalink Normal View History

2024-10-11 20:38:52 +08:00
package mylogger_test
2024-10-21 15:54:36 +08:00
import (
"fmt"
"time"
)
2024-10-11 20:38:52 +08:00
2024-10-21 15:54:36 +08:00
// logger 日志结构体
2024-10-11 20:38:52 +08:00
type Logger struct {
2024-10-15 20:05:52 +08:00
Level LogLevel
2024-10-11 20:38:52 +08:00
}
//Newlog 构造函数
2024-10-15 20:05:52 +08:00
func NewLog(levelStr string) Logger {
2024-10-21 15:54:36 +08:00
level, err := parseLogLevel(levelStr)
if err != nil {
2024-10-15 20:05:52 +08:00
panic(err)
}
return Logger{
2024-10-21 15:54:36 +08:00
Level: level,
2024-10-15 20:05:52 +08:00
}
2024-10-11 20:38:52 +08:00
}
2024-10-21 15:54:36 +08:00
func log(lv logLevel,msg string){
fmt.Printf()
}
2024-10-11 20:38:52 +08:00
func (l Logger) Debug(msg string) {
2024-10-21 15:54:36 +08:00
if l.enable(DEBUG) {
now := time.Now()
funcName, fileName, lineNo := getInfo(2)
fmt.Printf("[%s][DEBUG][%s:%s:%d]%s", now.Format("2006-01-02 15:04:05"), ,fileName,funcName,lineNumber,msg)
}
2024-10-11 20:38:52 +08:00
}
func (l Logger) Info(msg string) {
2024-10-21 15:54:36 +08:00
fmt.Println(msg)
2024-10-11 20:38:52 +08:00
}
func (l Logger) Warning(msg string) {
fmt.Println(msg)
}
func (l Logger) Error(msg string) {
fmt.Println(msg)
}
func (l Logger) Fatal(msg string) {
fmt.Println(msg)
}