Skip to content

Commit

Permalink
Merge pull request #341 from Jarily/master
Browse files Browse the repository at this point in the history
modify DefaultLogger default callerSkip to 2
  • Loading branch information
askuy authored Jun 11, 2023
2 parents e80a90f + cd87dbb commit 733d346
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/elog/elog_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func init() {
registry = make(map[string]WriterBuilder)
Register(&stderrWriterBuilder{})
Register(&rotateWriterBuilder{})
DefaultLogger = DefaultContainer().Build(WithFileName(DefaultLoggerName))
DefaultLogger = DefaultContainer().Build(WithFileName(DefaultLoggerName), WithCallSkip(2)) // DefaultLogger 默认为2层
EgoLogger = DefaultContainer().Build(WithFileName(EgoLoggerName))
}

Expand Down
7 changes: 7 additions & 0 deletions core/elog/elog_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ func WithEncoderConfig(encoderConfig *zapcore.EncoderConfig) Option {
c.config.encoderConfig = encoderConfig
}
}

// WithCallSkip 支持自定义调用层级
func WithCallSkip(callerSkip int) Option {
return func(c *Container) {
c.config.CallerSkip = callerSkip
}
}
2 changes: 1 addition & 1 deletion ego_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func loadConfig() error {
// initLogger init application and Ego logger
func (e *Ego) initLogger() error {
if econf.Get(e.opts.configPrefix+"logger.default") != nil {
elog.DefaultLogger = elog.Load(e.opts.configPrefix + "logger.default").Build()
elog.DefaultLogger = elog.Load(e.opts.configPrefix + "logger.default").Build(elog.WithCallSkip(2)) // DefaultLogger 默认为2层
elog.EgoLogger.Info("reinit default logger", elog.FieldComponent(elog.PackageName))
e.opts.afterStopClean = append(e.opts.afterStopClean, elog.DefaultLogger.Flush)
}
Expand Down
36 changes: 31 additions & 5 deletions ego_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package ego
import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/gotomicro/ego/core/constant"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/eflag"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/task/ejob"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"testing"

"github.com/gotomicro/ego/core/eflag"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/task/ejob"
"github.com/stretchr/testify/assert"
)

func Test_loadConfig(t *testing.T) {
Expand Down Expand Up @@ -129,3 +132,26 @@ func Test_runSerialFuncLogError(t *testing.T) {
assert.Nil(t, err)
assert.Contains(t, string(logged), `"Test_runSerialFuncLogError"`)
}

func Test_initLogger(t *testing.T) {
app := &Ego{}
err := os.Setenv(constant.EgoDebug, "true")
assert.Nil(t, err)
cfg := `
[logger.default]
debug = true
enableAddCaller = true
`
err = econf.LoadFromReader(strings.NewReader(cfg), toml.Unmarshal)
assert.NoError(t, err)

err = app.initLogger()
assert.Nil(t, err)
elog.Info("hello")
elog.DefaultLogger.Flush()
filePath := path.Join(elog.DefaultLogger.ConfigDir(), elog.DefaultLogger.ConfigName())
logged, err := os.ReadFile(filePath)
assert.Nil(t, err)
// 验证日志打印的caller是否正确 当前位置为ego/ego_function_test.go:150
assert.Contains(t, string(logged), "hello", `ego/ego_function_test.go:150`)
}

0 comments on commit 733d346

Please sign in to comment.