From 7fa973b26123532a12a3c2b08bec045038c602cf Mon Sep 17 00:00:00 2001 From: Roman Loginov Date: Thu, 9 Nov 2023 11:11:30 +0300 Subject: [PATCH] [#89] Add support zapjournald logger configuration Signed-off-by: Roman Loginov --- CHANGELOG.md | 1 + cmd/http-gw/main.go | 2 +- cmd/http-gw/settings.go | 57 ++++++++++++++++++++++++++++++++------ config/config.yaml | 1 + docs/gate-configuration.md | 9 +++--- go.mod | 2 ++ go.sum | 4 +++ 7 files changed, 63 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4618d35..fd19460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This document outlines major changes between releases. - Add `frostfs.buffer_max_size_for_put` config param - Add bucket/container caching - Disable homomorphic hash for PUT if it's disabled in container itself +- Add new `logger.destination` config param (#89) ### Changed - Update prometheus to v1.15.0 (#35) diff --git a/cmd/http-gw/main.go b/cmd/http-gw/main.go index 5762675..ea9fbd7 100644 --- a/cmd/http-gw/main.go +++ b/cmd/http-gw/main.go @@ -9,7 +9,7 @@ import ( func main() { globalContext, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) v := settings() - logger, atomicLevel := newLogger(v) + logger, atomicLevel := pickLogger(v) application := newApp(globalContext, WithLogger(logger, atomicLevel), WithConfig(v)) go application.Serve() diff --git a/cmd/http-gw/settings.go b/cmd/http-gw/settings.go index 7e53158..6e633ba 100644 --- a/cmd/http-gw/settings.go +++ b/cmd/http-gw/settings.go @@ -19,15 +19,22 @@ import ( grpctracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool" treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree" + "git.frostfs.info/TrueCloudLab/zapjournald" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/spf13/pflag" "github.com/spf13/viper" + "github.com/ssgreg/journald" "github.com/valyala/fasthttp" "go.uber.org/zap" "go.uber.org/zap/zapcore" "google.golang.org/grpc" ) +const ( + destinationStdout = "stdout" + destinationJournald = "journald" +) + const ( defaultRebalanceTimer = 60 * time.Second defaultRequestTimeout = 15 * time.Second @@ -74,7 +81,8 @@ const ( cfgPoolErrorThreshold = "pool_error_threshold" // Logger. - cfgLoggerLevel = "logger.level" + cfgLoggerLevel = "logger.level" + cfgLoggerDestination = "logger.destination" // Wallet. cfgWalletPassphrase = "wallet.passphrase" @@ -165,6 +173,7 @@ func settings() *viper.Viper { // logger: v.SetDefault(cfgLoggerLevel, "debug") + v.SetDefault(cfgLoggerDestination, "stdout") // pool: v.SetDefault(cfgPoolErrorThreshold, defaultPoolErrorThreshold) @@ -349,7 +358,25 @@ func mergeConfig(v *viper.Viper, fileName string) error { return v.MergeConfig(cfgFile) } -// newLogger constructs a zap.Logger instance for current application. +func pickLogger(v *viper.Viper) (*zap.Logger, zap.AtomicLevel) { + 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 zap.Logger instance for current application. // Panics on failure. // // Logger is built from zap's production logging configuration with: @@ -360,12 +387,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) (*zap.Logger, zap.AtomicLevel) { - lvl, err := getLogLevel(v) - if err != nil { - panic(err) - } - +func newStdoutLogger(lvl zapcore.Level) (*zap.Logger, zap.AtomicLevel) { c := zap.NewProductionConfig() c.Level = zap.NewAtomicLevelAt(lvl) c.Encoding = "console" @@ -381,6 +403,25 @@ func newLogger(v *viper.Viper) (*zap.Logger, zap.AtomicLevel) { return l, c.Level } +func newJournaldLogger(lvl zapcore.Level) (*zap.Logger, zap.AtomicLevel) { + 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 l, c.Level +} + func getLogLevel(v *viper.Viper) (zapcore.Level, error) { var lvl zapcore.Level lvlStr := v.GetString(cfgLoggerLevel) diff --git a/config/config.yaml b/config/config.yaml index 2cd20b5..6ab9994 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -16,6 +16,7 @@ tracing: logger: level: debug # Log level. + destination: stdout server: - address: 0.0.0.0:8080 diff --git a/docs/gate-configuration.md b/docs/gate-configuration.md index 65fe618..1b51848 100644 --- a/docs/gate-configuration.md +++ b/docs/gate-configuration.md @@ -160,12 +160,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` | # `web` section diff --git a/go.mod b/go.mod index 5f9b1b0..98abcb7 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-20230825064515-46a214d065f8 + git.frostfs.info/TrueCloudLab/zapjournald v0.0.0-20231018083019-2b6d84de9a3d github.com/bluele/gcache v0.0.2 github.com/fasthttp/router v1.4.1 github.com/nspcc-dev/neo-go v0.101.2-0.20230601131642-a0117042e8fc @@ -13,6 +14,7 @@ require ( github.com/prometheus/client_model v0.3.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/testcontainers/testcontainers-go v0.13.0 github.com/valyala/fasthttp v1.34.0 diff --git a/go.sum b/go.sum index dedb570..5d46822 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,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/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= @@ -873,6 +875,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= 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/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8= github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=