Skip to content

Commit

Permalink
feat(all): add flags for logger (#447)
Browse files Browse the repository at this point in the history
### What this PR does

This PR adds common flags for logging to package `internal/flags`. Those flags include the
followings:

- `--logdir`
- `--logtostderr`
- `--logfile-max-backups`
- `--logfile-retention-days`
- `--logfile-max-size-mb`
- `--logfile-name-utc`
- `--logfile-compression`
- `--log-human-readable`
- `--loglevel`

Those are very common. Hence all daemons can use them.

### Which issue(s) this PR resolves

Resolves #439
  • Loading branch information
ijsong authored Jun 1, 2023
2 parents 60f2cfe + 7efe407 commit f2e1193
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 135 deletions.
57 changes: 18 additions & 39 deletions cmd/varlogadm/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package main

import (
"context"
"path/filepath"

"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/kakao/varlog/internal/admin"
"github.com/kakao/varlog/internal/admin/mrmanager"
"github.com/kakao/varlog/internal/admin/snmanager"
"github.com/kakao/varlog/internal/admin/snwatcher"
"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/pkg/types"
"github.com/kakao/varlog/pkg/util/log"
)
Expand Down Expand Up @@ -51,11 +50,16 @@ func newStartCommand() *cli.Command {
flagSNWatcherHeartbeatCheckDeadline.DurationFlag(false, snwatcher.DefaultHeartbeatDeadline),
flagSNWatcherReportDeadline.DurationFlag(false, snwatcher.DefaultReportDeadline),

flagLogDir.StringFlag(false, ""),
flagLogToStderr.BoolFlag(),
flagLogFileRetentionDays.IntFlag(false, 0),
flagLogFileCompression.BoolFlag(),
flagLogLevel.StringFlag(false, "info"),
// logger options
flags.LogDir,
flags.LogToStderr,
flags.LogFileMaxSizeMB,
flags.LogFileMaxBackups,
flags.LogFileRetentionDays,
flags.LogFileNameUTC,
flags.LogFileCompression,
flags.LogHumanReadable,
flags.LogLevel,
},
}
}
Expand All @@ -65,7 +69,13 @@ func start(c *cli.Context) error {
if err != nil {
return err
}
logger, err := newLogger(c)

logOpts, err := flags.ParseLoggerFlags(c, "varlogadm.log")
if err != nil {
return err
}
logOpts = append(logOpts, log.WithZapLoggerOptions(zap.AddStacktrace(zap.DPanicLevel)))
logger, err := log.New(logOpts...)
if err != nil {
return err
}
Expand Down Expand Up @@ -123,34 +133,3 @@ func start(c *cli.Context) error {
}
return Main(opts, logger)
}

func newLogger(c *cli.Context) (*zap.Logger, error) {
level, err := zapcore.ParseLevel(c.String(flagLogLevel.Name))
if err != nil {
return nil, err
}

opts := []log.Option{
log.WithHumanFriendly(),
log.WithLocalTime(),
log.WithZapLoggerOptions(zap.AddStacktrace(zap.DPanicLevel)),
log.WithLogLevel(level),
}
if !c.Bool(flagLogToStderr.Name) {
opts = append(opts, log.WithoutLogToStderr())
}
if logdir := c.String(flagLogDir.Name); len(logdir) != 0 {
absDir, err := filepath.Abs(logdir)
if err != nil {
return nil, err
}
opts = append(opts, log.WithPath(filepath.Join(absDir, "varlogadm.log")))
}
if c.Bool(flagLogFileCompression.Name) {
opts = append(opts, log.WithCompression())
}
if retention := c.Int(flagLogFileRetentionDays.Name); retention > 0 {
opts = append(opts, log.WithAgeDays(retention))
}
return log.New(opts...)
}
26 changes: 0 additions & 26 deletions cmd/varlogadm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,4 @@ var (
Name: "sn-watcher-report-deadline",
Envs: []string{"SN_WATCHER_REPORT_DEADLINE"},
}

flagLogDir = flags.FlagDesc{
Name: "logdir",
Aliases: []string{"log-dir"},
Envs: []string{"LOG_DIR", "LOGDIR"},
}
flagLogToStderr = flags.FlagDesc{
Name: "logtostderr",
Aliases: []string{"log-to-stderr"},
Envs: []string{"LOGTOSTDERR", "LOG_TO_STDERR"},
}
flagLogFileRetentionDays = flags.FlagDesc{
Name: "logfile-retention-days",
Aliases: []string{"log-file-retention-days"},
Envs: []string{"LOGFILE_RETENTION_DAYS", "LOG_FILE_RETENTION_DAYS"},
}
flagLogFileCompression = flags.FlagDesc{
Name: "logfile-compression",
Aliases: []string{"log-file-compression"},
Envs: []string{"LOGFILE_COMPRESSION", "LOG_FILE_COMPRESSION"},
}
flagLogLevel = flags.FlagDesc{
Name: "loglevel",
Aliases: []string{"log-level"},
Envs: []string{"LOGLEVEL", "LOG_LEVEL"},
}
)
7 changes: 0 additions & 7 deletions cmd/varlogmr/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,4 @@ var (
Usage: "Collector endpoint",
Envs: []string{"COLLECTOR_ENDPOINT"},
}

flagLogDir = flags.FlagDesc{
Name: "log-dir",
Aliases: []string{},
Usage: "Log Dir",
Envs: []string{"LOG_DIR"},
}
)
29 changes: 20 additions & 9 deletions cmd/varlogmr/metadata_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/urfave/cli/v2"
_ "go.uber.org/automaxprocs"
"go.uber.org/zap"

"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/internal/metarepos"
"github.com/kakao/varlog/pkg/types"
"github.com/kakao/varlog/pkg/util/log"
Expand All @@ -24,16 +25,14 @@ func main() {
}

func start(c *cli.Context) error {
logDir, err := filepath.Abs(c.String(flagLogDir.Name))
logOpts, err := flags.ParseLoggerFlags(c, "varlogmr.log")
if err != nil {
return fmt.Errorf("could not create abs path: %w", err)
return err
}
logger, err := log.New(
log.WithoutLogToStderr(),
log.WithPath(fmt.Sprintf("%s/log.txt", logDir)),
)
logOpts = append(logOpts, log.WithZapLoggerOptions(zap.AddStacktrace(zap.DPanicLevel)))
logger, err := log.New(logOpts...)
if err != nil {
return fmt.Errorf("could not create logger: %w", err)
return err
}
defer func() {
_ = logger.Sync()
Expand Down Expand Up @@ -125,7 +124,19 @@ func initCLI() *cli.App {
flagMaxLogStreamsCountPerTopic,
flagTelemetryCollectorName.StringFlag(false, metarepos.DefaultTelemetryCollectorName),
flagTelemetryCollectorEndpoint.StringFlag(false, metarepos.DefaultTelmetryCollectorEndpoint),
flagLogDir.StringFlag(false, metarepos.DefaultLogDir),

//flagLogDir.StringFlag(false, metarepos.DefaultLogDir),

// logger options
flags.LogDir,
flags.LogToStderr,
flags.LogFileMaxSizeMB,
flags.LogFileMaxBackups,
flags.LogFileRetentionDays,
flags.LogFileNameUTC,
flags.LogFileCompression,
flags.LogHumanReadable,
flags.LogLevel,
},
}},
}
Expand Down
16 changes: 11 additions & 5 deletions cmd/varlogsn/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/urfave/cli/v2"

"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/internal/storagenode"
"github.com/kakao/varlog/internal/storagenode/logstream"
"github.com/kakao/varlog/pkg/types"
Expand Down Expand Up @@ -72,11 +73,16 @@ func newStartCommand() *cli.Command {
flagStorageMetricsLogInterval,
flagStorageVerbose.BoolFlag(),

flagLogDir.StringFlag(false, ""),
flagLogToStderr.BoolFlag(),
flagLogFileRetentionDays.IntFlag(false, 0),
flagLogFileCompression.BoolFlag(),
flagLogLevel.StringFlag(false, "info"),
// logger options
flags.LogDir,
flags.LogToStderr,
flags.LogFileMaxSizeMB,
flags.LogFileMaxBackups,
flags.LogFileRetentionDays,
flags.LogFileNameUTC,
flags.LogFileCompression,
flags.LogHumanReadable,
flags.LogLevel,

flagExporterType.StringFlag(false, "noop"),
flagExporterStopTimeout.DurationFlag(false, 5*time.Second),
Expand Down
26 changes: 0 additions & 26 deletions cmd/varlogsn/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,32 +172,6 @@ var (
Value: storage.DefaultMetricsLogInterval,
}

// flags for logging.
flagLogDir = flags.FlagDesc{
Name: "log-dir",
Aliases: []string{"logdir"},
Envs: []string{"LOG_DIR", "LOGDIR"},
}
flagLogToStderr = flags.FlagDesc{
Name: "logtostderr",
Envs: []string{"LOGTOSTDERR"},
}
flagLogFileRetentionDays = flags.FlagDesc{
Name: "logfile-retention-days",
Aliases: []string{"log-file-retention-days"},
Envs: []string{"LOGFILE_RETENTION_DAYS", "LOG_FILE_RETENTION_DAYS"},
}
flagLogFileCompression = flags.FlagDesc{
Name: "logfile-compression",
Aliases: []string{"log-file-compression"},
Envs: []string{"LOGFILE_COMPRESSION", "LOG_FILE_COMPRESSION"},
}
flagLogLevel = flags.FlagDesc{
Name: "loglevel",
Aliases: []string{"log-level"},
Envs: []string{"LOGLEVEL", "LOG_LEVEL"},
}

// flags for telemetry.
flagExporterType = flags.FlagDesc{
Name: "exporter-type",
Expand Down
25 changes: 3 additions & 22 deletions cmd/varlogsn/varlogsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

Expand All @@ -21,9 +20,9 @@ import (
_ "go.uber.org/automaxprocs"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/sync/errgroup"

"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/internal/storage"
"github.com/kakao/varlog/internal/storagenode"
"github.com/kakao/varlog/internal/storagenode/logstream"
Expand All @@ -47,29 +46,11 @@ func run() int {
}

func start(c *cli.Context) error {
level, err := zapcore.ParseLevel(c.String(flagLogLevel.Name))
logOpts, err := flags.ParseLoggerFlags(c, "varlogsn.log")
if err != nil {
return err
}

logOpts := []log.Option{
log.WithHumanFriendly(),
log.WithZapLoggerOptions(zap.AddStacktrace(zap.DPanicLevel)),
log.WithLogLevel(level),
}
if c.Bool(flagLogFileCompression.Name) {
logOpts = append(logOpts, log.WithCompression())
}
if retention := c.Int(flagLogFileRetentionDays.Name); retention > 0 {
logOpts = append(logOpts, log.WithAgeDays(retention))
}
if logDir := c.String(flagLogDir.Name); len(logDir) != 0 {
absDir, err := filepath.Abs(logDir)
if err != nil {
return err
}
logOpts = append(logOpts, log.WithPath(filepath.Join(absDir, "storagenode.log")))
}
logOpts = append(logOpts, log.WithZapLoggerOptions(zap.AddStacktrace(zap.DPanicLevel)))
logger, err := log.New(logOpts...)
if err != nil {
return err
Expand Down
Loading

0 comments on commit f2e1193

Please sign in to comment.