From 632c8a56bb96cdeb7db73b8d6a7413b6803648aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 2 Nov 2023 12:38:19 +0100 Subject: [PATCH] reinitialize logger for uploads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- .../unreleased/extract-log-initialization.md | 5 ++ cmd/revad/runtime/runtime.go | 81 +------------------ pkg/logger/logger.go | 77 ++++++++++++++++++ .../utils/decomposedfs/upload/processing.go | 20 +++-- 4 files changed, 96 insertions(+), 87 deletions(-) create mode 100644 changelog/unreleased/extract-log-initialization.md diff --git a/changelog/unreleased/extract-log-initialization.md b/changelog/unreleased/extract-log-initialization.md new file mode 100644 index 0000000000..7dfe1d6552 --- /dev/null +++ b/changelog/unreleased/extract-log-initialization.md @@ -0,0 +1,5 @@ +Enhancement: extract log initialization + +To prepare reinitializing a logger for uploads we refactored the loginitialization into its own package + +https://github.com/cs3org/reva/pull/4368 \ No newline at end of file diff --git a/cmd/revad/runtime/runtime.go b/cmd/revad/runtime/runtime.go index cc908d3e22..6bff8a9f79 100644 --- a/cmd/revad/runtime/runtime.go +++ b/cmd/revad/runtime/runtime.go @@ -20,7 +20,6 @@ package runtime import ( "fmt" - "io" "log" "net" "os" @@ -43,9 +42,8 @@ import ( // Run runs a reva server with the given config file and pid file. func Run(mainConf map[string]interface{}, pidFile, logLevel string) { - logConf := parseLogConfOrDie(mainConf["log"], logLevel) - logger := initLogger(logConf) - RunWithOptions(mainConf, pidFile, WithLogger(logger)) + log := logger.InitLoggerOrDie(mainConf["log"], logLevel) + RunWithOptions(mainConf, pidFile, WithLogger(log)) } // RunWithOptions runs a reva server with the given config file, pid file and options. @@ -180,15 +178,6 @@ func initCPUCount(conf *coreConf, log *zerolog.Logger) { log.Info().Msgf("running on %d cpus", ncpus) } -func initLogger(conf *logConf) *zerolog.Logger { - log, err := newLogger(conf) - if err != nil { - fmt.Fprintf(os.Stderr, "error creating logger, exiting ...") - os.Exit(1) - } - return log -} - func handlePIDFlag(l *zerolog.Logger, pidFile string, gracefulShutdownTimeout int) (*grace.Watcher, error) { w := grace.NewWatcher(grace.WithPIDFile(pidFile), grace.WithLogger(l.With().Str("pkg", "grace").Logger()), @@ -222,46 +211,6 @@ func start(mainConf map[string]interface{}, servers map[string]grace.Server, lis watcher.TrapSignals() } -func newLogger(conf *logConf) (*zerolog.Logger, error) { - // TODO(labkode): use debug level rather than info as default until reaching a stable version. - // Helps having smaller development files. - if conf.Level == "" { - conf.Level = zerolog.DebugLevel.String() - } - - var opts []logger.Option - opts = append(opts, logger.WithLevel(conf.Level)) - - w, err := getWriter(conf.Output) - if err != nil { - return nil, err - } - - opts = append(opts, logger.WithWriter(w, logger.Mode(conf.Mode))) - - l := logger.New(opts...) - sub := l.With().Int("pid", os.Getpid()).Logger() - return &sub, nil -} - -func getWriter(out string) (io.Writer, error) { - if out == "stderr" || out == "" { - return os.Stderr, nil - } - - if out == "stdout" { - return os.Stdout, nil - } - - fd, err := os.OpenFile(out, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) - if err != nil { - err = errors.Wrap(err, "error creating log file: "+out) - return nil, err - } - - return fd, nil -} - func getGRPCServer(conf interface{}, l *zerolog.Logger, tp trace.TracerProvider) (*rgrpc.Server, error) { sub := l.With().Str("pkg", "rgrpc").Logger() s, err := rgrpc.NewServer(conf, sub, tp) @@ -348,32 +297,6 @@ func parseSharedConfOrDie(v interface{}) { } } -func parseLogConfOrDie(v interface{}, logLevel string) *logConf { - c := &logConf{} - if err := mapstructure.Decode(v, c); err != nil { - fmt.Fprintf(os.Stderr, "error decoding log config: %s\n", err.Error()) - os.Exit(1) - } - - // if mode is not set, we use console mode, easier for devs - if c.Mode == "" { - c.Mode = "console" - } - - // Give priority to the log level passed through the command line. - if logLevel != "" { - c.Level = logLevel - } - - return c -} - -type logConf struct { - Output string `mapstructure:"output"` - Mode string `mapstructure:"mode"` - Level string `mapstructure:"level"` -} - func isEnabledHTTP(conf map[string]interface{}) bool { return isEnabled("http", conf) } diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 7c35a7ecd2..b2f82cdcfb 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -19,10 +19,13 @@ package logger import ( + "fmt" "io" "os" "time" + "github.com/mitchellh/mapstructure" + "github.com/pkg/errors" "github.com/rs/zerolog" ) @@ -85,3 +88,77 @@ func parseLevel(v string) zerolog.Level { return lvl } + +func InitLoggerOrDie(v interface{}, logLevel string) *zerolog.Logger { + conf := ParseLogConfOrDie(v, logLevel) + log, err := FromConfig(conf) + if err != nil { + fmt.Fprintf(os.Stderr, "error creating logger, exiting ...") + os.Exit(1) + } + return log +} + +func ParseLogConfOrDie(v interface{}, logLevel string) *LogConf { + c := &LogConf{} + if err := mapstructure.Decode(v, c); err != nil { + fmt.Fprintf(os.Stderr, "error decoding log config: %s\n", err.Error()) + os.Exit(1) + } + + // if mode is not set, we use console mode, easier for devs + if c.Mode == "" { + c.Mode = "console" + } + + // Give priority to the log level passed through the command line. + if logLevel != "" { + c.Level = logLevel + } + + return c +} + +type LogConf struct { + Output string `mapstructure:"output"` + Mode string `mapstructure:"mode"` + Level string `mapstructure:"level"` +} + +func FromConfig(conf *LogConf) (*zerolog.Logger, error) { + if conf.Level == "" { + conf.Level = zerolog.DebugLevel.String() + } + + var opts []Option + opts = append(opts, WithLevel(conf.Level)) + + w, err := getWriter(conf.Output) + if err != nil { + return nil, err + } + + opts = append(opts, WithWriter(w, Mode(conf.Mode))) + + l := New(opts...) + sub := l.With().Int("pid", os.Getpid()).Logger() + return &sub, nil +} + +func getWriter(out string) (io.Writer, error) { + if out == "stderr" || out == "" { + return os.Stderr, nil + } + + if out == "stdout" { + return os.Stdout, nil + } + + fd, err := os.OpenFile(out, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) + if err != nil { + err = errors.Wrap(err, "error creating log file: "+out) + return nil, err + } + + return fd, nil +} diff --git a/pkg/storage/utils/decomposedfs/upload/processing.go b/pkg/storage/utils/decomposedfs/upload/processing.go index d70f7f44a8..462716562e 100644 --- a/pkg/storage/utils/decomposedfs/upload/processing.go +++ b/pkg/storage/utils/decomposedfs/upload/processing.go @@ -224,17 +224,21 @@ func Get(ctx context.Context, id string, lu *lookup.Lookup, tp Tree, fsRoot stri } ctx = ctxpkg.ContextSetUser(ctx, u) - // TODO configure the logger the same way ... store and add traceid in file info - - var opts []logger.Option - opts = append(opts, logger.WithLevel(info.Storage["LogLevel"])) - opts = append(opts, logger.WithWriter(os.Stderr, logger.ConsoleMode)) - l := logger.New(opts...) - - sub := l.With().Int("pid", os.Getpid()).Logger() + // restore logger from file info + log, err := logger.FromConfig(&logger.LogConf{ + Output: "stderr", // TODO use config from decomposedfs + Mode: "json", // TODO use config from decomposedfs + Level: info.Storage["LogLevel"], + }) + if err != nil { + return nil, err + } + sub := log.With().Int("pid", os.Getpid()).Logger() ctx = appctx.WithLogger(ctx, &sub) + // TODO store and add traceid in file info + up := buildUpload(ctx, info, info.Storage["BinPath"], infoPath, lu, tp, pub, async, tknopts) up.versionsPath = info.MetaData["versionsPath"] up.SizeDiff, _ = strconv.ParseInt(info.MetaData["sizeDiff"], 10, 64)