From ecfe5746d7eae0f6d79ff6759e115d601ac27e51 Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 00:48:05 +0800 Subject: [PATCH 01/18] add use_local_time_encoder and show_caller config --- config.go | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 39cce0f..3410cb1 100644 --- a/config.go +++ b/config.go @@ -95,15 +95,29 @@ type Config struct { // File logger options FileLogger *FileLoggerConfig `mapstructure:"file_logger_options"` + + // UseLocalTimeEncoder is used to set the encoder to use local time instead of UTC. + UseLocalTimeEncoder bool `mapstructure:"use_local_time_encoder"` + + // ShowCaller is used to set the encoder to show the caller. + ShowCaller bool `mapstructure:"show_caller"` } // BuildLogger converts config into Zap configuration. func (cfg *Config) BuildLogger() (*zap.Logger, error) { var zCfg zap.Config + callerKey := zapcore.OmitKey + if cfg.ShowCaller { + callerKey = "caller" + } switch Mode(strings.ToLower(string(cfg.Mode))) { case off, none: return zap.NewNop(), nil case production: + encodeTime := utcEpochTimeEncoder + if cfg.UseLocalTimeEncoder { + encodeTime = localTimeEncoder + } zCfg = zap.Config{ Level: zap.NewAtomicLevelAt(zap.InfoLevel), Development: false, @@ -112,13 +126,13 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { TimeKey: "ts", LevelKey: "level", NameKey: "logger", - CallerKey: zapcore.OmitKey, + CallerKey: callerKey, FunctionKey: zapcore.OmitKey, MessageKey: "msg", StacktraceKey: zapcore.OmitKey, LineEnding: cfg.LineEnding, EncodeLevel: zapcore.LowercaseLevelEncoder, - EncodeTime: utcEpochTimeEncoder, + EncodeTime: encodeTime, EncodeDuration: zapcore.SecondsDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, }, @@ -126,6 +140,10 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { ErrorOutputPaths: []string{"stderr"}, } case development: + encodeTime := utcISO8601TimeEncoder + if cfg.UseLocalTimeEncoder { + encodeTime = localTimeEncoder + } zCfg = zap.Config{ Level: zap.NewAtomicLevelAt(zap.DebugLevel), Development: true, @@ -134,14 +152,14 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { TimeKey: "ts", LevelKey: "level", NameKey: "logger", - CallerKey: zapcore.OmitKey, + CallerKey: callerKey, FunctionKey: zapcore.OmitKey, MessageKey: "msg", StacktraceKey: zapcore.OmitKey, LineEnding: cfg.LineEnding, EncodeLevel: ColoredLevelEncoder, EncodeName: ColoredNameEncoder, - EncodeTime: utcISO8601TimeEncoder, + EncodeTime: encodeTime, EncodeDuration: zapcore.StringDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, }, @@ -160,6 +178,10 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { ErrorOutputPaths: []string{"stderr"}, } default: + encodeTime := utcISO8601TimeEncoder + if cfg.UseLocalTimeEncoder { + encodeTime = localTimeEncoder + } zCfg = zap.Config{ Level: zap.NewAtomicLevelAt(zap.DebugLevel), Encoding: "console", @@ -167,14 +189,14 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { TimeKey: "T", LevelKey: "L", NameKey: "N", - CallerKey: zapcore.OmitKey, + CallerKey: callerKey, FunctionKey: zapcore.OmitKey, MessageKey: "M", StacktraceKey: zapcore.OmitKey, LineEnding: cfg.LineEnding, EncodeLevel: ColoredLevelEncoder, EncodeName: ColoredNameEncoder, - EncodeTime: utcISO8601TimeEncoder, + EncodeTime: encodeTime, EncodeDuration: zapcore.StringDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, }, @@ -251,3 +273,7 @@ func utcISO8601TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { func utcEpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { enc.AppendInt64(t.UTC().UnixNano()) } + +func localTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Local().Format("2006-01-02 15:04:05.000")) +} From 6d265f2a091f87b65874b8c5ed24739108eaee26 Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 00:52:02 +0800 Subject: [PATCH 02/18] modify module name --- go.mod | 2 +- go.work.sum | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 70cf999..cb12c02 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/roadrunner-server/logger/v5 +module github.com/shellphy/logger/v5 go 1.23 diff --git a/go.work.sum b/go.work.sum index 70b1cc6..a42c4bc 100644 --- a/go.work.sum +++ b/go.work.sum @@ -145,6 +145,8 @@ github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQs github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g= @@ -169,6 +171,7 @@ github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI= github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/onsi/gomega v1.34.2/go.mod h1:v1xfxRgk0KIsG+QOdm7p8UosrOzPYRo60fd3B/1Dukc= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.13.1 h1:I2qBYMChEhIjOgazfJmV3/mZM256btk6wkCDRmW7JYs= @@ -184,6 +187,7 @@ github.com/sagikazarmark/crypt v0.19.0 h1:WMyLTjHBo64UvNcWqpzY3pbZTYgnemZU8FBZig github.com/sagikazarmark/crypt v0.19.0/go.mod h1:c6vimRziqqERhtSe0MhIvzE1w54FrCHtrXb5NH/ja78= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc= github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU= github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= @@ -216,6 +220,8 @@ go.temporal.io/api v1.30.1 h1:73UCTi+8l+Qy3GdDypW2FB5rj995A3Pi0mXkSu/qedw= go.temporal.io/api v1.30.1/go.mod h1:xI9UdP3s07881dgWzG8idIBAnZq3/aop+O682EIDoT0= go.temporal.io/api v1.35.0 h1:+1o2zyBncLjnpjJt4FedKZMtuUav/LUgTB+mhQxx0zs= go.temporal.io/api v1.35.0/go.mod h1:OYkuupuCw6s/5TkcKHMb9EcIrOI+vTsbf/CGaprbzb0= +go.temporal.io/api v1.36.0 h1:WdntOw9m38lFvMdMXuOO+3BQ0R8HpVLgtk9+f+FwiDk= +go.temporal.io/api v1.36.0/go.mod h1:0nWIrFRVPlcrkopXqxir/UWOtz/NZCo+EE9IX4UwVxw= go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= @@ -232,6 +238,8 @@ golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg= golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8= golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= +golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 h1:zf5N6UOrA487eEFacMePxjXAJctxKmyjKUsjA11Uzuk= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU= @@ -241,6 +249,8 @@ golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8= golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= +golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24= +golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M= golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA= golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= @@ -270,6 +280,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa h1: google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:K4kfzHtI0kqWA79gecJarFtDn/Mls+GxQcg3Zox91Ac= google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d h1:Aqf0fiIdUQEj0Gn9mKFFXoQfTTEaNopWpfVyYADxiSg= google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Od4k8V1LQSizPRUK4OzZ7TBE/20k+jPczUDAEyvn69Y= +google.golang.org/genproto/googleapis/api v0.0.0-20240722135656-d784300faade h1:WxZOF2yayUHpHSbUE6NMzumUzBxYc3YGwo0YHnbzsJY= +google.golang.org/genproto/googleapis/api v0.0.0-20240722135656-d784300faade/go.mod h1:mw8MG/Qz5wfgYr6VqVCiZcHe/GJEfI+oGGDCohaVgB0= google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 h1:6GQBEOdGkX6MMTLT9V+TjtIRZCw9VPD5Z+yHY9wMgS0= @@ -278,6 +290,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa h1: google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk= google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade h1:oCRSWfwGXQsqlVdErcyTt4A93Y8fo0/9D4b1gnI++qo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= google.golang.org/grpc v1.58.0 h1:32JY8YpPMSR45K+c3o6b8VL73V+rR8k+DeMIr4vRH8o= google.golang.org/grpc v1.58.0/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= @@ -286,6 +300,8 @@ google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk= google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE= google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= +google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= +google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= From 583814d0a4a49a6bc0f87e30bc83da91d5421859 Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 01:16:12 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E5=9B=BA=E5=AE=9A=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encoder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encoder.go b/encoder.go index 7090cbe..a54f9ea 100644 --- a/encoder.go +++ b/encoder.go @@ -25,7 +25,7 @@ func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) // ColoredNameEncoder colorizes service names. func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) { - if len(s) < 12 { + if len(s) < 20 { s += strings.Repeat(" ", 12-len(s)) } From 2583ed95156ceba5ce8f5da73d79569fd744e7c8 Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 01:20:05 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E5=9B=BA=E5=AE=9A=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encoder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encoder.go b/encoder.go index a54f9ea..3fba68e 100644 --- a/encoder.go +++ b/encoder.go @@ -26,7 +26,7 @@ func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) // ColoredNameEncoder colorizes service names. func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) { if len(s) < 20 { - s += strings.Repeat(" ", 12-len(s)) + s += strings.Repeat(" ", 20-len(s)) } enc.AppendString(color.HiGreenString(s)) From 07267aa9864c5da960565fb99d199eaecd7dd2a2 Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 10:57:06 +0800 Subject: [PATCH 05/18] add test --- config.go | 13 ++++++++----- tests/configs/.rr-file-logger.yaml | 2 ++ tests/logger_test.go | 11 +++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 3410cb1..4cdbff8 100644 --- a/config.go +++ b/config.go @@ -96,8 +96,8 @@ type Config struct { // File logger options FileLogger *FileLoggerConfig `mapstructure:"file_logger_options"` - // UseLocalTimeEncoder is used to set the encoder to use local time instead of UTC. - UseLocalTimeEncoder bool `mapstructure:"use_local_time_encoder"` + // UseLocalTime is used to set the encoder to use local time instead of UTC. + UseLocalTime bool `mapstructure:"use_local_time"` // ShowCaller is used to set the encoder to show the caller. ShowCaller bool `mapstructure:"show_caller"` @@ -115,7 +115,7 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { return zap.NewNop(), nil case production: encodeTime := utcEpochTimeEncoder - if cfg.UseLocalTimeEncoder { + if cfg.UseLocalTime { encodeTime = localTimeEncoder } zCfg = zap.Config{ @@ -141,7 +141,7 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { } case development: encodeTime := utcISO8601TimeEncoder - if cfg.UseLocalTimeEncoder { + if cfg.UseLocalTime { encodeTime = localTimeEncoder } zCfg = zap.Config{ @@ -179,7 +179,7 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { } default: encodeTime := utcISO8601TimeEncoder - if cfg.UseLocalTimeEncoder { + if cfg.UseLocalTime { encodeTime = localTimeEncoder } zCfg = zap.Config{ @@ -246,6 +246,9 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { w, zCfg.Level, ) + if cfg.ShowCaller { + return zap.New(core, zap.AddCaller()), nil + } return zap.New(core), nil } diff --git a/tests/configs/.rr-file-logger.yaml b/tests/configs/.rr-file-logger.yaml index d7fb8a5..569b0cf 100644 --- a/tests/configs/.rr-file-logger.yaml +++ b/tests/configs/.rr-file-logger.yaml @@ -21,5 +21,7 @@ http: logs: mode: development level: debug + use_local_time: true + show_caller: true file_logger_options: log_output: "test.log" diff --git a/tests/logger_test.go b/tests/logger_test.go index 34eab36..e958cbb 100644 --- a/tests/logger_test.go +++ b/tests/logger_test.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "os/signal" + "regexp" "strings" "sync" "syscall" @@ -337,6 +338,16 @@ func TestFileLogger(t *testing.T) { strings.Contains(string(f), "worker constructed") strings.Contains(string(f), "201 GET") + // when use_local_time=true, check local time format + matched, err := regexp.MatchString(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}`, string(f)) + assert.NoError(t, err) + assert.True(t, matched) + + // when show_caller=true, check caller info + callerMatched, err := regexp.MatchString(`\w+\.go:\d+`, string(f)) + assert.NoError(t, err) + assert.True(t, callerMatched) + _ = os.Remove("test.log") stopCh <- struct{}{} From bb8c385ca9af8b6409f17c935f15724353f98c04 Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 11:00:08 +0800 Subject: [PATCH 06/18] merge --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index cb12c02..70cf999 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/shellphy/logger/v5 +module github.com/roadrunner-server/logger/v5 go 1.23 From 29bc270fc2188f042f741f20c09bca5cdf0fcc9a Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 11:19:42 +0800 Subject: [PATCH 07/18] add caller color --- config.go | 26 +++++++++++++++++++++----- encoder.go | 4 ++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 4cdbff8..99b9b40 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,7 @@ package logger import ( + "fmt" "os" "strings" "time" @@ -106,9 +107,11 @@ type Config struct { // BuildLogger converts config into Zap configuration. func (cfg *Config) BuildLogger() (*zap.Logger, error) { var zCfg zap.Config - callerKey := zapcore.OmitKey + var callerKey = zapcore.OmitKey + var encodeCaller zapcore.CallerEncoder = nil if cfg.ShowCaller { callerKey = "caller" + encodeCaller = ColoredShortCallerEncoder } switch Mode(strings.ToLower(string(cfg.Mode))) { case off, none: @@ -134,7 +137,7 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { EncodeLevel: zapcore.LowercaseLevelEncoder, EncodeTime: encodeTime, EncodeDuration: zapcore.SecondsDurationEncoder, - EncodeCaller: zapcore.ShortCallerEncoder, + EncodeCaller: encodeCaller, }, OutputPaths: []string{"stderr"}, ErrorOutputPaths: []string{"stderr"}, @@ -161,7 +164,7 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { EncodeName: ColoredNameEncoder, EncodeTime: encodeTime, EncodeDuration: zapcore.StringDurationEncoder, - EncodeCaller: zapcore.ShortCallerEncoder, + EncodeCaller: encodeCaller, }, OutputPaths: []string{"stderr"}, ErrorOutputPaths: []string{"stderr"}, @@ -198,7 +201,7 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { EncodeName: ColoredNameEncoder, EncodeTime: encodeTime, EncodeDuration: zapcore.StringDurationEncoder, - EncodeCaller: zapcore.ShortCallerEncoder, + EncodeCaller: encodeCaller, }, OutputPaths: []string{"stderr"}, ErrorOutputPaths: []string{"stderr"}, @@ -228,6 +231,11 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { // init it // otherwise - return standard config if cfg.FileLogger != nil { + fileEncoderConfig := zCfg.EncoderConfig + if cfg.ShowCaller { + fileEncoderConfig.EncodeCaller = ShortCallerEncoderWithPadding + } + // init absent options cfg.FileLogger.InitDefaults() @@ -242,7 +250,7 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { ) core := zapcore.NewCore( - zapcore.NewJSONEncoder(zCfg.EncoderConfig), + zapcore.NewJSONEncoder(fileEncoderConfig), w, zCfg.Level, ) @@ -280,3 +288,11 @@ func utcEpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { func localTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { enc.AppendString(t.Local().Format("2006-01-02 15:04:05.000")) } + +func ColoredShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(fmt.Sprintf("\x1b[35m%-40s\x1b[0m", caller.TrimmedPath())) +} + +func ShortCallerEncoderWithPadding(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(fmt.Sprintf("%-40s", caller.TrimmedPath())) +} diff --git a/encoder.go b/encoder.go index 3fba68e..7090cbe 100644 --- a/encoder.go +++ b/encoder.go @@ -25,8 +25,8 @@ func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) // ColoredNameEncoder colorizes service names. func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) { - if len(s) < 20 { - s += strings.Repeat(" ", 20-len(s)) + if len(s) < 12 { + s += strings.Repeat(" ", 12-len(s)) } enc.AppendString(color.HiGreenString(s)) From c1430a7d5e0c482c3edc4f933f21df52c651bada Mon Sep 17 00:00:00 2001 From: shellphy Date: Sun, 10 Nov 2024 21:30:12 +0800 Subject: [PATCH 08/18] increment logger name width --- encoder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/encoder.go b/encoder.go index 7090cbe..3fba68e 100644 --- a/encoder.go +++ b/encoder.go @@ -25,8 +25,8 @@ func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) // ColoredNameEncoder colorizes service names. func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) { - if len(s) < 12 { - s += strings.Repeat(" ", 12-len(s)) + if len(s) < 20 { + s += strings.Repeat(" ", 20-len(s)) } enc.AppendString(color.HiGreenString(s)) From 1305a3f719ac6e3c449113a6b900eae5667fcdf6 Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:34:21 +0800 Subject: [PATCH 09/18] Do not modify nameencoder --- encoder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/encoder.go b/encoder.go index 3fba68e..7090cbe 100644 --- a/encoder.go +++ b/encoder.go @@ -25,8 +25,8 @@ func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) // ColoredNameEncoder colorizes service names. func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) { - if len(s) < 20 { - s += strings.Repeat(" ", 20-len(s)) + if len(s) < 12 { + s += strings.Repeat(" ", 12-len(s)) } enc.AppendString(color.HiGreenString(s)) From 5c738a0e8dbcb9b6e4f000293156f9b8858342da Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:38:41 +0800 Subject: [PATCH 10/18] update --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 70cf999..cb12c02 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/roadrunner-server/logger/v5 +module github.com/shellphy/logger/v5 go 1.23 From cf91fbfe33a7c486ad4505737ef92380e508c897 Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:44:41 +0800 Subject: [PATCH 11/18] test --- config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.go b/config.go index 99b9b40..2ea6c29 100644 --- a/config.go +++ b/config.go @@ -286,7 +286,7 @@ func utcEpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { } func localTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(t.Local().Format("2006-01-02 15:04:05.000")) + enc.AppendString(t.Local().Format("2006-01-02 15:04:05.000 ")) } func ColoredShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { From 9b65581e77e866f8c0bee4920e6f3699c97f6239 Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:48:55 +0800 Subject: [PATCH 12/18] =?UTF-8?q?caller=E9=95=BF=E5=BA=A6=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 2 +- go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 2ea6c29..61c779a 100644 --- a/config.go +++ b/config.go @@ -294,5 +294,5 @@ func ColoredShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.Primitive } func ShortCallerEncoderWithPadding(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(fmt.Sprintf("%-40s", caller.TrimmedPath())) + enc.AppendString(fmt.Sprintf("%-30s", caller.TrimmedPath())) } diff --git a/go.mod b/go.mod index cb12c02..70cf999 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/shellphy/logger/v5 +module github.com/roadrunner-server/logger/v5 go 1.23 From 25d29bab2849412b8b2ead6a3d6701d02e7e2805 Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:50:04 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0mod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 70cf999..cb12c02 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/roadrunner-server/logger/v5 +module github.com/shellphy/logger/v5 go 1.23 From 48d6db1847a6fd4653276fd19865ee0a74e25d73 Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:52:08 +0800 Subject: [PATCH 14/18] update --- config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.go b/config.go index 61c779a..17cf141 100644 --- a/config.go +++ b/config.go @@ -294,5 +294,5 @@ func ColoredShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.Primitive } func ShortCallerEncoderWithPadding(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(fmt.Sprintf("%-30s", caller.TrimmedPath())) + enc.AppendString(fmt.Sprintf("%s", caller.TrimmedPath())) } From d8a41013749c8f1a04f08202199478743fd8a8ef Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:53:54 +0800 Subject: [PATCH 15/18] update module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index cb12c02..70cf999 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/shellphy/logger/v5 +module github.com/roadrunner-server/logger/v5 go 1.23 From e322c8c17afcc41c57f6969e4148714f5ec64c11 Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:56:46 +0800 Subject: [PATCH 16/18] update caller length --- config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 17cf141..327b2e0 100644 --- a/config.go +++ b/config.go @@ -290,9 +290,9 @@ func localTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { } func ColoredShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(fmt.Sprintf("\x1b[35m%-40s\x1b[0m", caller.TrimmedPath())) + enc.AppendString(fmt.Sprintf("\x1b[35m%-30s\x1b[0m", caller.TrimmedPath())) } func ShortCallerEncoderWithPadding(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(fmt.Sprintf("%s", caller.TrimmedPath())) + enc.AppendString(fmt.Sprintf("%-30s", caller.TrimmedPath())) } From b95253017bc626dc4ac35b41aea14cb505090b28 Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:58:44 +0800 Subject: [PATCH 17/18] update module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 70cf999..cb12c02 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/roadrunner-server/logger/v5 +module github.com/shellphy/logger/v5 go 1.23 From 571f8b66678e68f51bc5d68ec5565ac5c69b722f Mon Sep 17 00:00:00 2001 From: shellphy Date: Tue, 12 Nov 2024 14:59:02 +0800 Subject: [PATCH 18/18] update module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index cb12c02..70cf999 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/shellphy/logger/v5 +module github.com/roadrunner-server/logger/v5 go 1.23