|
@@ -8,6 +8,7 @@ import (
|
|
|
"log"
|
|
|
"os"
|
|
|
"path"
|
|
|
+ "strings"
|
|
|
"sync"
|
|
|
)
|
|
|
|
|
@@ -23,7 +24,7 @@ var (
|
|
|
|
|
|
type logger struct {
|
|
|
*logs.BeeLogger
|
|
|
- filter string
|
|
|
+ filter Filter
|
|
|
}
|
|
|
|
|
|
type CustomLogger struct {
|
|
@@ -58,17 +59,17 @@ func Debug(msg string, v ...interface{}) {
|
|
|
loggerHandler.Debug(msg, v...)
|
|
|
}
|
|
|
func (c *CustomLogger) Debug(msg string, v ...interface{}) {
|
|
|
- for _, appender := range c.logs {
|
|
|
- if appender.GetLevel() >= logs.LevelInfo {
|
|
|
- appender.Debug(msg, v...)
|
|
|
+ for _, logger := range c.logs {
|
|
|
+ if logger.GetLevel() >= logs.LevelInfo && logger.filter.ShouldLog(msg) {
|
|
|
+ logger.Debug(msg, v...)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
func (c *CustomLogger) Info(msg string, v ...interface{}) {
|
|
|
- for _, appender := range c.logs {
|
|
|
- if appender.GetLevel() >= logs.LevelInfo {
|
|
|
- appender.Info(msg, v...)
|
|
|
+ for _, logger := range c.logs {
|
|
|
+ if logger.GetLevel() >= logs.LevelInfo && logger.filter.ShouldLog(msg) {
|
|
|
+ logger.Info(msg, v...)
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -76,7 +77,7 @@ func (c *CustomLogger) Info(msg string, v ...interface{}) {
|
|
|
|
|
|
func (c *CustomLogger) Error(msg string, v ...interface{}) {
|
|
|
for _, logger := range c.logs {
|
|
|
- if logger.GetLevel() >= logs.LevelError {
|
|
|
+ if logger.GetLevel() >= logs.LevelError && logger.filter.ShouldLog(msg) {
|
|
|
logger.Error(msg, v...)
|
|
|
}
|
|
|
}
|
|
@@ -84,7 +85,7 @@ func (c *CustomLogger) Error(msg string, v ...interface{}) {
|
|
|
|
|
|
func (c *CustomLogger) Warn(msg string, v ...interface{}) {
|
|
|
for _, logger := range c.logs {
|
|
|
- if logger.GetLevel() >= logs.LevelWarning {
|
|
|
+ if logger.GetLevel() >= logs.LevelWarning && logger.filter.ShouldLog(msg) {
|
|
|
logger.Warn(msg, v...)
|
|
|
}
|
|
|
}
|
|
@@ -163,7 +164,7 @@ func initLogger(logCfg logConfig) {
|
|
|
beeLogger.EnableFuncCallDepth(true)
|
|
|
}
|
|
|
loggerHandler.logs = append(loggerHandler.logs, &logger{BeeLogger: beeLogger,
|
|
|
- filter: appender.Filter})
|
|
|
+ filter: NewLevelFilter(appender.Filter)})
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -187,6 +188,26 @@ func convertAppenderToLog(a *appender) (*logProps, error) {
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
+// Filter 接口定义
|
|
|
+type Filter interface {
|
|
|
+ ShouldLog(message string) bool
|
|
|
+}
|
|
|
+
|
|
|
+// LevelFilter 实现 Filter 接口,根据日志级别过滤日志
|
|
|
+type ContentFilter struct {
|
|
|
+ filterStr string
|
|
|
+}
|
|
|
+
|
|
|
+// NewLevelFilter 创建一个新的 LevelFilter 实例
|
|
|
+func NewLevelFilter(filterStr string) *ContentFilter {
|
|
|
+ return &ContentFilter{filterStr: filterStr}
|
|
|
+}
|
|
|
+
|
|
|
+// ShouldLog 根据日志级别决定是否记录日志
|
|
|
+func (lf *ContentFilter) ShouldLog(message string) bool {
|
|
|
+ return strings.Contains(message, lf.filterStr)
|
|
|
+}
|
|
|
+
|
|
|
// logConfig 日志配置
|
|
|
type logConfig struct {
|
|
|
FilePath string `json:"filepath" description:"日志路径"`
|