Skip to content

Commit

Permalink
fix: log
Browse files Browse the repository at this point in the history
  • Loading branch information
liov committed Jul 5, 2024
1 parent 5d0084e commit 096b352
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 111 deletions.
2 changes: 1 addition & 1 deletion initialize/conf_dao/gormdb/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Config) Build(dialector gorm.Dialector) (*gorm.DB, error) {
// 默认日志
logger.Default = logger.New(stdlog.New(os.Stdout, "\r", stdlog.LstdFlags), c.Logger)
} else {
logger.Default = &loggeri.Logger{Logger: log.GetCallerSkipLogger(0).Logger, Config: &c.Logger}
logger.Default = &loggeri.Logger{Logger: log.GetNoCallerLogger().Logger, Config: &c.Logger}
}

db, err := gorm.Open(dialector, dbConfig)
Expand Down
2 changes: 1 addition & 1 deletion utils/dao/database/gorm/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

var (
DefaultV2 = New(logi.GetCallerSkipLogger(0).Logger, &logger.Config{
DefaultV2 = New(logi.GetNoCallerLogger().Logger, &logger.Config{
SlowThreshold: 100 * time.Millisecond,
LogLevel: logger.Warn,
Colorful: true,
Expand Down
3 changes: 1 addition & 2 deletions utils/log/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ func (lc *Config) initLogger(cores ...zapcore.Core) *zap.Logger {
//如果没有设置输出,默认控制台
if len(cores) == 0 {
consoleEncoder = zapcore.NewConsoleEncoder(lc.EncoderConfig)
cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), StdOutLevel(lc.Level)),
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stderr), StdErrLevel(lc.Level)))
cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), lc.Level))
}

core := zapcore.NewTee(cores...)
Expand Down
20 changes: 0 additions & 20 deletions utils/log/context.go

This file was deleted.

31 changes: 0 additions & 31 deletions utils/log/interface.go

This file was deleted.

22 changes: 12 additions & 10 deletions utils/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package log

import (
"fmt"
"github.com/hopeio/cherry/utils/log/output"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sync"
)

func init() {
output.RegisterSink()
SetDefaultLogger(&Config{Development: true, Level: zapcore.DebugLevel})
}

Expand Down Expand Up @@ -38,25 +36,29 @@ func SetDefaultLogger(lf *Config, cores ...zapcore.Core) {
defaultLogger = lf.NewLogger(cores...)
stackLogger = defaultLogger.WithOptions(zap.WithCaller(true), zap.AddStacktrace(zapcore.ErrorLevel))
noCallerLogger = defaultLogger.WithOptions(zap.WithCaller(false))
skipLoggers[0].Logger = noCallerLogger
for i := 1; i < len(skipLoggers); i++ {
for i := 0; i < len(skipLoggers); i++ {
if skipLoggers[i].Logger != nil {
skipLoggers[i].needUpdate = true
}
}
}

// range -3~6
func GetCallerSkipLogger(skip int) *Logger {
if skip > 10 {
panic("skip最大不超过10")
if skip < -3 {
panic("skip不小于-3")
}
if skipLoggers[skip].needUpdate || skipLoggers[skip].Logger == nil {
if skip > 6 {
panic("skip不大于6")
}
idx := skip + 3
if skipLoggers[idx].needUpdate || skipLoggers[idx].Logger == nil {
mu.Lock()
skipLoggers[skip].Logger = defaultLogger.AddSkip(skip)
skipLoggers[skip].needUpdate = false
skipLoggers[idx].Logger = defaultLogger.AddSkip(skip)
skipLoggers[idx].needUpdate = false
mu.Unlock()
}
return skipLoggers[skip].Logger
return skipLoggers[idx].Logger
}

func GetNoCallerLogger() *Logger {
Expand Down
4 changes: 2 additions & 2 deletions utils/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ func (ps invalidPairs) MarshalLogArray(enc zapcore.ArrayEncoder) error {
type StdOutLevel zapcore.Level

func (l StdOutLevel) Enabled(lvl zapcore.Level) bool {
return lvl >= zapcore.Level(l)
return lvl >= zapcore.Level(l) && lvl < zapcore.ErrorLevel
}

type StdErrLevel zapcore.Level

func (l StdErrLevel) Enabled(lvl zapcore.Level) bool {
return lvl >= zapcore.Level(l)
return lvl >= zapcore.Level(l) && lvl >= zapcore.ErrorLevel
}
4 changes: 4 additions & 0 deletions utils/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ func TestLog(t *testing.T) {
func TestLogStack(t *testing.T) {
ErrorS("test")
}

func TestLogNoCaller(t *testing.T) {
noCallerLogger.Debug("test")
}
4 changes: 3 additions & 1 deletion utils/log/output/loki/loki.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package loki

import "github.com/grafana/loki-client-go/loki"
import (
"github.com/grafana/loki-client-go/loki"
)

type Loki struct {
Client loki.Client
Expand Down
41 changes: 0 additions & 41 deletions utils/log/output/sink.go

This file was deleted.

2 changes: 1 addition & 1 deletion utils/log/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func (l *Logger) WithAttrs(attrs []slog.Attr) slog.Handler {
}

func (l *Logger) WithGroup(name string) slog.Handler {
return l
return l.Named(name)
}
57 changes: 57 additions & 0 deletions utils/math/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package math

import (
"math"
"sort"
)

// Calculate the median of a slice of floats
func median(data []float64) float64 {
sort.Float64s(data)
n := len(data)
if n%2 == 0 {
return (data[n/2-1] + data[n/2]) / 2
}
return data[n/2]
}

// Calculate the mean of a slice of floats
func mean(data []float64) float64 {
sum := 0.0
for _, value := range data {
sum += value
}
return sum / float64(len(data))
}

// Remove outliers using the MAD method and calculate the mean of the remaining data
func removeOutliersAndMean(data []float64) float64 {
if len(data) == 0 {
return 0
}

med := median(data)

// Calculate absolute deviations from the median
absDevs := make([]float64, len(data))
for i, value := range data {
absDevs[i] = math.Abs(value - med)
}

// Calculate the median of the absolute deviations
mad := median(absDevs)

// Define a threshold using the MAD; here we use 3 times the MAD
threshold := 3.0 * mad

// Filter out outliers
filteredData := make([]float64, 0)
for _, value := range data {
if math.Abs(value-med) <= threshold {
filteredData = append(filteredData, value)
}
}

// Calculate the mean of the remaining data
return mean(filteredData)
}
13 changes: 13 additions & 0 deletions utils/math/statistics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package math

import (
"fmt"
"testing"
)

func TestMean(t *testing.T) {
data := []float64{1, 2, 2, 2, 3, 10, 2, 2, 1, 2, 3, 2, 100}
fmt.Printf("Original data: %v\n", data)
result := removeOutliersAndMean(data)
fmt.Printf("Mean after removing outliers: %f\n", result)
}
2 changes: 1 addition & 1 deletion utils/scheduler/engine/conctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (e *Engine[KEY]) Run(tasks ...*Task[KEY]) {
}
e.lock.Unlock()
}
log.GetNoCallerLogger().Debugf("\r[Running] task:D:%d/T:%d/S:%d/H:%d/F:%d/E:%d,worker: %d/%d", e.taskDoneCount, e.taskTotalCount, e.taskSkipCount, e.taskErrHandleCount, e.taskFailedCount, e.taskErrorTimes, e.workingWorkerCount, e.currentWorkerCount)
fmt.Sprintf("\r[Running] task:D:%d/T:%d/S:%d/H:%d/F:%d/E:%d,worker: %d/%d", e.taskDoneCount, e.taskTotalCount, e.taskSkipCount, e.taskErrHandleCount, e.taskFailedCount, e.taskErrorTimes, e.workingWorkerCount, e.currentWorkerCount)
timer.Reset(e.monitorInterval)
case <-e.ctx.Done():
if err := e.ctx.Err(); err != nil {
Expand Down

0 comments on commit 096b352

Please sign in to comment.