mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
feat: 引入 Logger 接口抽象,支持自定义日志实现
- 新增 Logger 接口定义,支持 zap/slog 等自定义实现 - 新增 CloserLogger 接口用于支持关闭操作 - Engine 新增 SetLogger/GetLogger 方法使用接口 - 新增 compat.go 兼容层,保留 reco 兼容方法 - 新增 slog 适配器示例 - 删除 zap 示例 - Context.GetLogger() 返回接口类型
This commit is contained in:
parent
58fd877ae2
commit
c8b14ef43a
7 changed files with 575 additions and 17 deletions
23
context.go
23
context.go
|
|
@ -26,7 +26,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/WJQSERVER/wanf"
|
||||
"github.com/fenthope/reco"
|
||||
"github.com/go-json-experiment/json"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/iox"
|
||||
|
|
@ -135,8 +134,8 @@ func (c *Context) writeResponseBody(data []byte, contextMsg string) {
|
|||
if _, err := c.Writer.Write(data); err != nil {
|
||||
wrapped := fmt.Errorf("%s: %w", contextMsg, err)
|
||||
c.AddError(wrapped)
|
||||
if c != nil && c.engine != nil && c.engine.LogReco != nil {
|
||||
c.engine.LogReco.Errorf("%s: %v", contextMsg, err)
|
||||
if c.engine != nil && c.engine.logger != nil {
|
||||
c.engine.logger.Errorf("%s: %v", contextMsg, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1136,9 +1135,9 @@ func (c *Context) GetHTTPC() *httpc.Client {
|
|||
return c.HTTPClient
|
||||
}
|
||||
|
||||
// GetLogger 获取engine的Logger
|
||||
func (c *Context) GetLogger() *reco.Logger {
|
||||
return c.engine.LogReco
|
||||
// GetLogger 获取engine的Logger接口
|
||||
func (c *Context) GetLogger() Logger {
|
||||
return c.engine.logger
|
||||
}
|
||||
|
||||
// GetReqQueryString
|
||||
|
|
@ -1297,25 +1296,25 @@ func (c *Context) DeleteCookie(name string) {
|
|||
|
||||
// === 日志记录 ===
|
||||
func (c *Context) Debugf(format string, args ...any) {
|
||||
c.engine.LogReco.Debugf(format, args...)
|
||||
c.engine.logger.Debugf(format, args...)
|
||||
}
|
||||
|
||||
func (c *Context) Infof(format string, args ...any) {
|
||||
c.engine.LogReco.Infof(format, args...)
|
||||
c.engine.logger.Infof(format, args...)
|
||||
}
|
||||
|
||||
func (c *Context) Warnf(format string, args ...any) {
|
||||
c.engine.LogReco.Warnf(format, args...)
|
||||
c.engine.logger.Warnf(format, args...)
|
||||
}
|
||||
|
||||
func (c *Context) Errorf(format string, args ...any) {
|
||||
c.engine.LogReco.Errorf(format, args...)
|
||||
c.engine.logger.Errorf(format, args...)
|
||||
}
|
||||
|
||||
func (c *Context) Fatalf(format string, args ...any) {
|
||||
c.engine.LogReco.Fatalf(format, args...)
|
||||
c.engine.logger.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
func (c *Context) Panicf(format string, args ...any) {
|
||||
c.engine.LogReco.Panicf(format, args...)
|
||||
c.engine.logger.Panicf(format, args...)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue