From 672320a55a156b78e6c518202a19fa15c5fee8de Mon Sep 17 00:00:00 2001 From: huofei <68298506@qq.com> Date: Thu, 1 Sep 2022 00:48:02 +0800 Subject: [PATCH] [log] optimize console log format (#3499) --- pkg/log/log.go | 25 +++++++++++---- pkg/log/redirectStderr_armOS.go | 26 --------------- pkg/log/redirectStderr_darwin_arm64.go | 26 --------------- pkg/log/redirectStderr_other.go | 26 --------------- pkg/log/redirectStderr_windows.go | 44 -------------------------- server/main.go | 2 +- 6 files changed, 19 insertions(+), 130 deletions(-) delete mode 100644 pkg/log/redirectStderr_armOS.go delete mode 100644 pkg/log/redirectStderr_darwin_arm64.go delete mode 100644 pkg/log/redirectStderr_other.go delete mode 100644 pkg/log/redirectStderr_windows.go diff --git a/pkg/log/log.go b/pkg/log/log.go index bb992f3c38..7b903f642c 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -85,19 +85,30 @@ func InitLoggers(globalCfg GlobalConfig, subCfgs map[string]GlobalConfig, opts . if globalCfg.EcsIntegration { cfg.Zap.EncoderConfig = ecszap.ECSCompatibleEncoderConfig(cfg.Zap.EncoderConfig) } - logger, err := cfg.Zap.Build(opts...) - if err != nil { - return err - } + + var cores []zapcore.Core if cfg.StderrRedirectFile != nil { stderrF, err := os.OpenFile(*cfg.StderrRedirectFile, os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0600) if err != nil { return err } - if err := redirectStderr(stderrF); err != nil { - return err - } + + cores = append(cores, zapcore.NewCore( + zapcore.NewJSONEncoder(cfg.Zap.EncoderConfig), + zapcore.AddSync(stderrF), + cfg.Zap.Level)) } + + consoleCfg := zap.NewDevelopmentConfig() + consoleCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + cores = append(cores, zapcore.NewCore( + zapcore.NewConsoleEncoder(consoleCfg.EncoderConfig), + zapcore.AddSync(os.Stdout), + cfg.Zap.Level)) + + core := zapcore.NewTee(cores...) + logger := zap.New(core, opts...) + _logMu.Lock() if name == _globalLoggerName { _globalCfg = cfg diff --git a/pkg/log/redirectStderr_armOS.go b/pkg/log/redirectStderr_armOS.go deleted file mode 100644 index 68af079cb0..0000000000 --- a/pkg/log/redirectStderr_armOS.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2019 IoTeX Foundation -// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no -// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent -// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache -// License 2.0 that can be found in the LICENSE file. - -//go:build arm || (arm64 && !darwin) -// +build arm arm64,!darwin - -package log - -import ( - "log" - "os" - "syscall" -) - -// redirectStderr to the file passed in -func redirectStderr(f *os.File) error { - err := syscall.Dup3(int(f.Fd()), 2, 0) - if err != nil { - log.Fatalf("Failed to redirect stderr to file: %v", err) - return err - } - return nil -} diff --git a/pkg/log/redirectStderr_darwin_arm64.go b/pkg/log/redirectStderr_darwin_arm64.go deleted file mode 100644 index af296d193b..0000000000 --- a/pkg/log/redirectStderr_darwin_arm64.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2020 IoTeX Foundation -// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no -// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent -// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache -// License 2.0 that can be found in the LICENSE file. - -//go:build arm64 && darwin -// +build arm64,darwin - -package log - -import ( - "log" - "os" - "syscall" -) - -// redirectStderr to the file passed in -func redirectStderr(f *os.File) error { - err := syscall.Dup2(int(f.Fd()), 2) - if err != nil { - log.Fatalf("Failed to redirect stderr to file: %v", err) - return err - } - return nil -} diff --git a/pkg/log/redirectStderr_other.go b/pkg/log/redirectStderr_other.go deleted file mode 100644 index 4376770df7..0000000000 --- a/pkg/log/redirectStderr_other.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2019 IoTeX Foundation -// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no -// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent -// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache -// License 2.0 that can be found in the LICENSE file. - -//go:build !windows && !arm && !arm64 -// +build !windows,!arm,!arm64 - -package log - -import ( - "log" - "os" - "syscall" -) - -// redirectStderr to the file passed in -func redirectStderr(f *os.File) error { - err := syscall.Dup2(int(f.Fd()), 2) - if err != nil { - log.Fatalf("Failed to redirect stderr to file: %v", err) - return err - } - return nil -} diff --git a/pkg/log/redirectStderr_windows.go b/pkg/log/redirectStderr_windows.go deleted file mode 100644 index c9286622d0..0000000000 --- a/pkg/log/redirectStderr_windows.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2022 IoTeX Foundation -// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no -// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent -// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache -// License 2.0 that can be found in the LICENSE file. - -//go:build windows -// +build windows - -package log - -import ( - "log" - "os" - "syscall" -) - -var ( - _kernel32 = syscall.MustLoadDLL("_kernel32.dll") - _procSetStdHandle = _kernel32.MustFindProc("SetStdHandle") -) - -func setStdHandle(stdhandle int32, handle syscall.Handle) error { - r0, _, e1 := syscall.Syscall(_procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0) - if r0 == 0 { - if e1 != 0 { - return error(e1) - } - return syscall.EINVAL - } - return nil -} - -// redirectStderr to the file passed in -func redirectStderr(f *os.File) error { - err := setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd())) - if err != nil { - log.Fatalf("Failed to redirect stderr to file: %v", err) - return err - } - // SetStdHandle does not affect prior references to stderr - os.Stderr = f - return nil -} diff --git a/server/main.go b/server/main.go index a405da4e8a..2bb316173c 100644 --- a/server/main.go +++ b/server/main.go @@ -169,7 +169,7 @@ func main() { func initLogger(cfg config.Config) error { addr := cfg.Chain.ProducerAddress() - return log.InitLoggers(cfg.Log, cfg.SubLogs, zap.Fields( + return log.InitLoggers(cfg.Log, cfg.SubLogs, zap.AddCaller(), zap.Fields( zap.String("ioAddr", addr.String()), )) }