Skip to content

Commit

Permalink
reinitialize logger for uploads
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Nov 28, 2023
1 parent 1e3027b commit 632c8a5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 87 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/extract-log-initialization.md
Original file line number Diff line number Diff line change
@@ -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
81 changes: 2 additions & 79 deletions cmd/revad/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package runtime

import (
"fmt"
"io"
"log"
"net"
"os"
Expand All @@ -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.
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
77 changes: 77 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
package logger

import (
"fmt"
"io"
"os"
"time"

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -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
}
20 changes: 12 additions & 8 deletions pkg/storage/utils/decomposedfs/upload/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 632c8a5

Please sign in to comment.