开源地址
说明: 设计参考 PHP PSR-3 规范,做了一些调整后实现。
安装
go get github.com/go-packagist/logger
教程
package main
import (
"fmt"
"github.com/go-packagist/logger"
"time"
)
type CustomLogger struct {
logger.Loggerable
}
var _ logger.Logger = (*CustomLogger)(nil)
func NewCustomLogger() *CustomLogger {
c := &CustomLogger{
Loggerable: func(level logger.Level, s string) {
fmt.Println(fmt.Sprintf("%s %s: %s", time.Now().Format(time.DateTime), level.UpperString(), s))
},
}
return c
}
func main() {
c := NewCustomLogger()
c.Emergencyf("Emergencyf: %s", "test")
c.Alertf("Alertf: %s", "test")
c.Criticalf("Criticalf: %s", "test")
c.Errorf("Errorf: %s", "test")
c.Warningf("Warningf: %s", "test")
c.Noticef("Noticef: %s", "test")
c.Infof("Infof: %s", "test")
c.Debugf("Debugf: %s", "test")
c.Emergency("Emergency: test")
c.Alert("Alert: test")
c.Critical("Critical: test")
c.Error("Error: test")
c.Warning("Warning: test")
c.Notice("Notice: test")
c.Info("Info: test")
c.Debug("Debug: test")
c.Log(logger.Emergency, "Log: Emergency: test")
// Output:
// 2023-03-28 23:18:13 EMERGENCY: Emergencyf: test
// 2023-03-28 23:18:13 ALERT: Alertf: test
// 2023-03-28 23:18:13 CRITICAL: Criticalf: test
// 2023-03-28 23:18:13 ERROR: Errorf: test
// 2023-03-28 23:18:13 WARNING: Warningf: test
// 2023-03-28 23:18:13 NOTICE: Noticef: test
// 2023-03-28 23:18:13 INFO: Infof: test
// 2023-03-28 23:18:13 DEBUG: Debugf: test
// 2023-03-28 23:18:13 EMERGENCY: Emergency: test
// 2023-03-28 23:18:13 ALERT: Alert: test
// 2023-03-28 23:18:13 CRITICAL: Critical: test
// 2023-03-28 23:18:13 ERROR: Error: test
// 2023-03-28 23:18:13 WARNING: Warning: test
// 2023-03-28 23:18:13 NOTICE: Notice: test
// 2023-03-28 23:18:13 INFO: Info: test
// 2023-03-28 23:18:13 DEBUG: Debug: test
// 2023-03-28 23:18:13 EMERGENCY: Log: Emergency: test
}
内置 Logger
logger.NewNullLogger()
- 啥也不干logger.NewPrintLogger()
- 打印到控制台
参考资料
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。