From 406075aebbacdac1daa185034e593abae7df6c35 Mon Sep 17 00:00:00 2001 From: Roman Loginov Date: Thu, 9 Nov 2023 09:07:11 +0300 Subject: [PATCH] [#236] Add support zapjournald logger configuration Signed-off-by: Roman Loginov --- CHANGELOG.md | 1 + cmd/s3-gw/app_settings.go | 60 +++++++++++++++++++++++++++++++++------ cmd/s3-gw/main.go | 2 +- config/config.yaml | 1 + docs/configuration.md | 8 ++++-- go.mod | 2 ++ go.sum | 4 +++ 7 files changed, 66 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a781693..14d5cb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ This document outlines major changes between releases. - Add new `frostfs.buffer_max_size_for_put` config param and sync TZ hash for PUT operations (#197) - Add `X-Amz-Version-Id` header after complete multipart upload (#227) - Add handling of `X-Amz-Copy-Source-Server-Side-Encryption-Customer-*` headers during copy (#217) +- Add new `logger.destination` config param (#236) ### Changed - Update prometheus to v1.15.0 (#94) diff --git a/cmd/s3-gw/app_settings.go b/cmd/s3-gw/app_settings.go index 0d28c7a..e0b7b42 100644 --- a/cmd/s3-gw/app_settings.go +++ b/cmd/s3-gw/app_settings.go @@ -20,12 +20,19 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/version" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool" + "git.frostfs.info/TrueCloudLab/zapjournald" "github.com/spf13/pflag" "github.com/spf13/viper" + "github.com/ssgreg/journald" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) +const ( + destinationStdout = "stdout" + destinationJournald = "journald" +) + const ( defaultRebalanceInterval = 60 * time.Second defaultHealthcheckTimeout = 15 * time.Second @@ -49,7 +56,8 @@ var defaultCopiesNumbers = []uint32{0} const ( // Settings. // Logger. - cfgLoggerLevel = "logger.level" + cfgLoggerLevel = "logger.level" + cfgLoggerDestination = "logger.destination" // Wallet. cfgWalletPath = "wallet.path" @@ -533,6 +541,7 @@ func newSettings() *viper.Viper { // logger: v.SetDefault(cfgLoggerLevel, "debug") + v.SetDefault(cfgLoggerDestination, "stdout") // pool: v.SetDefault(cfgPoolErrorThreshold, defaultPoolErrorThreshold) @@ -737,7 +746,25 @@ func mergeConfig(v *viper.Viper, fileName string) error { return v.MergeConfig(cfgFile) } -// newLogger constructs a Logger instance for the current application. +func pickLogger(v *viper.Viper) *Logger { + lvl, err := getLogLevel(v) + if err != nil { + panic(err) + } + + dest := v.GetString(cfgLoggerDestination) + + switch dest { + case destinationStdout: + return newStdoutLogger(lvl) + case destinationJournald: + return newJournaldLogger(lvl) + default: + panic(fmt.Sprintf("wrong destination for logger: %s", dest)) + } +} + +// newStdoutLogger constructs a Logger instance for the current application. // Panics on failure. // // Logger contains a logger is built from zap's production logging configuration with: @@ -750,12 +777,7 @@ func mergeConfig(v *viper.Viper, fileName string) error { // Logger records a stack trace for all messages at or above fatal level. // // See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace. -func newLogger(v *viper.Viper) *Logger { - lvl, err := getLogLevel(v) - if err != nil { - panic(err) - } - +func newStdoutLogger(lvl zapcore.Level) *Logger { c := zap.NewProductionConfig() c.Level = zap.NewAtomicLevelAt(lvl) c.Encoding = "console" @@ -774,6 +796,28 @@ func newLogger(v *viper.Viper) *Logger { } } +func newJournaldLogger(lvl zapcore.Level) *Logger { + c := zap.NewProductionConfig() + c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + c.Level = zap.NewAtomicLevelAt(lvl) + + encoder := zapcore.NewConsoleEncoder(c.EncoderConfig) + + core := zapjournald.NewCore(zap.NewAtomicLevelAt(lvl), encoder, &journald.Journal{}, zapjournald.SyslogFields) + coreWithContext := core.With([]zapcore.Field{ + zapjournald.SyslogFacility(zapjournald.LogDaemon), + zapjournald.SyslogIdentifier(), + zapjournald.SyslogPid(), + }) + + l := zap.New(coreWithContext, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel))) + + return &Logger{ + logger: l, + lvl: c.Level, + } +} + func getLogLevel(v *viper.Viper) (zapcore.Level, error) { var lvl zapcore.Level lvlStr := v.GetString(cfgLoggerLevel) diff --git a/cmd/s3-gw/main.go b/cmd/s3-gw/main.go index 5a1957a..f76bced 100644 --- a/cmd/s3-gw/main.go +++ b/cmd/s3-gw/main.go @@ -10,7 +10,7 @@ func main() { g, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) v := newSettings() - l := newLogger(v) + l := pickLogger(v) a := newApp(g, l, v) diff --git a/config/config.yaml b/config/config.yaml index bac2103..1f7ea6f 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -43,6 +43,7 @@ listen_domains: logger: level: debug + destination: stdout # RPC endpoint and order of resolving of bucket names rpc_endpoint: http://morph-chain.frostfs.devenv:30333 diff --git a/docs/configuration.md b/docs/configuration.md index 2a11153..d1a834b 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -355,11 +355,13 @@ server: ```yaml logger: level: debug + destination: stdout ``` -| Parameter | Type | SIGHUP reload | Default value | Description | -|-----------|----------|---------------|---------------|----------------------------------------------------------------------------------------------------| -| `level` | `string` | yes | `debug` | Logging level.
Possible values: `debug`, `info`, `warn`, `error`, `dpanic`, `panic`, `fatal`. | +| Parameter | Type | SIGHUP reload | Default value | Description | +|---------------|----------|---------------|---------------|----------------------------------------------------------------------------------------------------| +| `level` | `string` | yes | `debug` | Logging level.
Possible values: `debug`, `info`, `warn`, `error`, `dpanic`, `panic`, `fatal`. | +| `destination` | `string` | no | `stdout` | Destination for logger: `stdout` or `journald` | ### `cache` section diff --git a/go.mod b/go.mod index 17237a0..da3394e 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( git.frostfs.info/TrueCloudLab/frostfs-api-go/v2 v2.15.1-0.20230802075510-964c3edb3f44 git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20230531082742-c97d21411eb6 git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20231003164722-60463871dbc2 + git.frostfs.info/TrueCloudLab/zapjournald v0.0.0-20231018083019-2b6d84de9a3d github.com/aws/aws-sdk-go v1.44.6 github.com/bluele/gcache v0.0.2 github.com/go-chi/chi/v5 v5.0.8 @@ -19,6 +20,7 @@ require ( github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.15.0 + github.com/ssgreg/journald v1.0.0 github.com/stretchr/testify v1.8.3 github.com/urfave/cli/v2 v2.3.0 go.opentelemetry.io/otel v1.16.0 diff --git a/go.sum b/go.sum index 33888e8..d4a8c8e 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ git.frostfs.info/TrueCloudLab/rfc6979 v0.4.0 h1:M2KR3iBj7WpY3hP10IevfIB9MURr4O9m git.frostfs.info/TrueCloudLab/rfc6979 v0.4.0/go.mod h1:okpbKfVYf/BpejtfFTfhZqFP+sZ8rsHrP8Rr/jYPNRc= git.frostfs.info/TrueCloudLab/tzhash v1.8.0 h1:UFMnUIk0Zh17m8rjGHJMqku2hCgaXDqjqZzS4gsb4UA= git.frostfs.info/TrueCloudLab/tzhash v1.8.0/go.mod h1:dhY+oy274hV8wGvGL4MwwMpdL3GYvaX1a8GQZQHvlF8= +git.frostfs.info/TrueCloudLab/zapjournald v0.0.0-20231018083019-2b6d84de9a3d h1:Z9UuI+jxzPtwQZUMmATdTuA8/8l2jzBY1rVh/gwBDsw= +git.frostfs.info/TrueCloudLab/zapjournald v0.0.0-20231018083019-2b6d84de9a3d/go.mod h1:rQFJJdEOV7KbbMtQYR2lNfiZk+ONRDJSbMCTWxKt8Fw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/CityOfZion/neo-go v0.62.1-pre.0.20191114145240-e740fbe708f8/go.mod h1:MJCkWUBhi9pn/CrYO1Q3P687y2KeahrOPS9BD9LDGb0= @@ -443,6 +445,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU= github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA= +github.com/ssgreg/journald v1.0.0 h1:0YmTDPJXxcWDPba12qNMdO6TxvfkFSYpFIJ31CwmLcU= +github.com/ssgreg/journald v1.0.0/go.mod h1:RUckwmTM8ghGWPslq2+ZBZzbb9/2KgjzYZ4JEP+oRt0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=