Skip to content

Commit

Permalink
feat: 新增日志截断、日志等级过滤、日志位置打印
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf committed Dec 19, 2024
1 parent 076cdd3 commit 4f449aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 6 additions & 1 deletion bcs-common/common/blog/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"log"
"regexp"
"strings"
"sync"
"time"

Expand All @@ -37,10 +38,14 @@ var once sync.Once

// InitLogs initializes logs the way we want for blog.
func InitLogs(logConfig conf.LogConfig) {
stdErrThreshold := logConfig.StdErrThreshold
if logConfig.Level != "" {
stdErrThreshold = strings.ToUpper(logConfig.Level)
}
glog.InitLogs(logConfig.ToStdErr,
logConfig.AlsoToStdErr,
logConfig.Verbosity,
logConfig.StdErrThreshold,
stdErrThreshold,
logConfig.VModule,
logConfig.TraceLocation,
logConfig.LogDir,
Expand Down
17 changes: 14 additions & 3 deletions bcs-common/common/blog/glog/glog.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ import (
"sync"
"sync/atomic"
"time"

"github.com/dustin/go-humanize"
)

// severity identifies the sort of log: info, warning etc. It also implements
Expand Down Expand Up @@ -694,9 +696,18 @@ func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoTo
buf.Write(stacks(false))
}
}
data := buf.Bytes()
body := buf.String()
// 最大打印 1024 个字符
if len(body) > 1024 {
body = fmt.Sprintf("%s...(Total %s)\n", body[:1024], humanize.Bytes(uint64(len(body))))
}
data := []byte(body)

if l.toStderr {
_, _ = os.Stderr.Write(data)
// print according to the configured log level
if l.stderrThreshold.get() <= s {
_, _ = os.Stderr.Write(data)
}
} else {
if alsoToStderr || l.alsoToStderr || s >= l.stderrThreshold.get() {
_, _ = os.Stderr.Write(data)
Expand Down Expand Up @@ -1276,7 +1287,7 @@ func (l LogKit) Log(keyvals ...interface{}) error {
logLevel = infoLog
}

logging.print(logLevel, keysAndValues...)
logging.printDepth(logLevel, 1, keysAndValues...)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions bcs-common/common/conf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type LogConfig struct {
LogMaxSize uint64 `json:"log_max_size" value:"500" usage:"Max size (MB) per log file." mapstructure:"log_max_size" yaml:"log_max_size"`
LogMaxNum int `json:"log_max_num" value:"10" usage:"Max num of log file. The oldest will be removed if there is a extra file created." mapstructure:"log_max_num" yaml:"log_max_num"`

Level string `json:"level" value:"info" usage:"log level: info、warning、error、fatal " mapstructure:"level" yaml:"level"`
ToStdErr bool `json:"logtostderr" value:"false" usage:"log to standard error instead of files" mapstructure:"logtostderr" yaml:"logtostderr"`
AlsoToStdErr bool `json:"alsologtostderr" value:"false" usage:"log to standard error as well as files" mapstructure:"alsologtostderr" yaml:"alsologtostderr"`
Verbosity int32 `json:"v" value:"0" usage:"log level for V logs" mapstructure:"v" yaml:"v"`
Expand Down

0 comments on commit 4f449aa

Please sign in to comment.