Skip to content

Commit

Permalink
Merge pull request #239 from askuy/feature/maxprocs
Browse files Browse the repository at this point in the history
custom log time format
  • Loading branch information
askuy committed Jan 7, 2022
2 parents eec7ab3 + df7e3d4 commit 8a92ab4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
2 changes: 2 additions & 0 deletions core/constant/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
EgoLogExtraKeys = "EGO_LOG_EXTRA_KEYS"
// EgoLogWriter writer方式: file | stderr
EgoLogWriter = "EGO_LOG_WRITER"
// EgoLogTimeType 记录的时间类型,默认 second,millisecond,%Y-%m-%d %H:%M:%S
EgoLogTimeType = "EGO_LOG_TIME_TYPE"
// EgoTraceIDName 应用链路ID环境变量,不配置,默认x-trace-id
EgoTraceIDName = "EGO_TRACE_ID_NAME"
// EgoGovernorEnableConfig 是否开启查看config
Expand Down
16 changes: 15 additions & 1 deletion core/eapp/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
egoLogExtraKeys []string
egoLogWriter string
egoGovernorEnableConfig string
egoLogTimeType string
)

func initEnv() {
Expand All @@ -42,6 +43,14 @@ func initEnv() {
if egoLogWriter == "" {
egoLogWriter = "file"
}
egoLogTimeType = os.Getenv(constant.EgoLogTimeType)
if IsDevelopmentMode() {
egoLogTimeType = "%Y-%m-%d %H:%M:%S"
}
if egoLogTimeType == "" {
egoLogTimeType = "second"
}

}

// AppMode 获取应用运行的环境
Expand All @@ -64,7 +73,7 @@ func AppInstance() string {
return appInstance
}

// IsDevelopmentMode 判断是否是生产模式
// IsDevelopmentMode 判断是否是测试模式
func IsDevelopmentMode() bool {
return egoDebug == "true"
}
Expand Down Expand Up @@ -98,3 +107,8 @@ func EgoLogWriter() string {
func EgoGovernorEnableConfig() bool {
return egoGovernorEnableConfig == "true"
}

// EgoLogTimeType ...
func EgoLogTimeType() string {
return egoLogTimeType
}
19 changes: 13 additions & 6 deletions core/elog/component_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"
"time"

"github.com/gotomicro/ego/core/eapp"
"github.com/gotomicro/ego/core/util/xcolor"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -36,7 +37,7 @@ func defaultDebugConfig() *zapcore.EncoderConfig {
StacktraceKey: "stack",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: debugEncodeLevel,
EncodeTime: timeDebugEncoder,
EncodeTime: timeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
Expand All @@ -60,11 +61,17 @@ func debugEncodeLevel(lv zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
}

func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendInt64(t.Unix())
}

func timeDebugEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
switch eapp.EgoLogTimeType() {
case "second":
enc.AppendInt64(t.Unix())
case "millisecond":
enc.AppendInt64(t.UnixNano() / 1e6)
// 后期写个通用格式支持这种
case "%Y-%m-%d %H:%M:%S":
enc.AppendString(t.Format("2006-01-02 15:04:05"))
default:
enc.AppendInt64(t.Unix())
}
}

func panicDetail(msg string, fields ...Field) {
Expand Down
12 changes: 6 additions & 6 deletions core/elog/component_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func Test_timeEncoder(t *testing.T) {
assert.Equal(t, int64(1609495200), enc.elems[0].(int64))
}

func Test_timeDebugEncoder(t *testing.T) {
te, _ := time.Parse("2006-01-02 15:04:05", "2021-01-01 10:00:00")
enc := &sliceArrayEncoder{}
timeDebugEncoder(te, enc)
assert.Equal(t, "2021-01-01 10:00:00", enc.elems[0].(string))
}
//func Test_timeDebugEncoder(t *testing.T) {
// te, _ := time.Parse("2006-01-02 15:04:05", "2021-01-01 10:00:00")
// enc := &sliceArrayEncoder{}
// timeDebugEncoder(te, enc)
// assert.Equal(t, "2021-01-01 10:00:00", enc.elems[0].(string))
//}

func Test_debugEncodeLevel1(t *testing.T) {
type args struct {
Expand Down
8 changes: 1 addition & 7 deletions ego_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,13 @@ func initMaxProcs() error {
runtime.GOMAXPROCS(maxProcs)
} else {
if _, err := maxprocs.Set(); err != nil {
elog.EgoLogger.Panic("init max procs", elog.FieldComponent("app"), elog.FieldErr(err))
elog.EgoLogger.Error("init max procs", elog.FieldComponent("app"), elog.FieldErr(err))
}
}
elog.EgoLogger.Info("init app", elog.FieldComponent("app"), elog.Int("pid", os.Getpid()), elog.Int("coreNum", runtime.GOMAXPROCS(-1)))
return nil
}

//func printLogger() error {
// elog.EgoLogger.Info("init default logger", elog.FieldComponent(elog.PackageName))
// elog.EgoLogger.Info("init ego logger", elog.FieldComponent(elog.PackageName))
// return nil
//}

// printBanner init
func (e *Ego) printBanner() error {
if e.opts.disableBanner {
Expand Down

0 comments on commit 8a92ab4

Please sign in to comment.