storj/pkg/process/logging.go
Kaloyan Raev 958772467a cmd/storagenode-updater, pkg/process: Fix logging timestamp
After changing how we execute the storagenode-updater process we lost
timestamps in the log.

The fix is to start using zap logging.

The Windows Installer is changed to register the storagenode-updater
service in a way that the Windows Service Manager passes the
--log.output flag instead of the old --log.

The old --log flag is deprecated, but not removed. We will support it
for backward compatibility. This is required as the storagenode-updater
can auto-updated itself, but the Windows Service Manager of this old
installtion will continue passing the old --log flag when starting it.

Change-Id: I690dff27e01335e617aa314032ecbadc4ea8cbd5
Signed-off-by: Kaloyan Raev <kaloyan@storj.io>
2019-12-11 10:05:48 +00:00

90 lines
2.6 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package process
import (
"flag"
"net/url"
"os"
"runtime"
"github.com/zeebo/errs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"storj.io/storj/pkg/cfgstruct"
)
var (
// Error is a process error class
Error = errs.Class("process error")
logLevel = zap.LevelFlag("log.level", func() zapcore.Level {
if isDev() {
return zapcore.DebugLevel
}
return zapcore.InfoLevel
}(), "the minimum log level to log")
logDev = flag.Bool("log.development", isDev(), "if true, set logging to development mode")
logCaller = flag.Bool("log.caller", isDev(), "if true, log function filename and line number")
logStack = flag.Bool("log.stack", isDev(), "if true, log stack traces")
logEncoding = flag.String("log.encoding", "console", "configures log encoding. can either be 'console' or 'json'")
logOutput = flag.String("log.output", "stderr", "can be stdout, stderr, or a filename")
)
func init() {
winFileSink := func(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}
err := zap.RegisterSink("winfile", winFileSink)
if err != nil {
panic("Unable to register winfile sink: " + err.Error())
}
}
func isDev() bool { return cfgstruct.DefaultsType() != "release" }
// NewLogger creates new logger configured by the process flags.
func NewLogger() (*zap.Logger, error) {
return NewLoggerWithOutputPaths(*logOutput)
}
// NewLoggerWithOutputPaths is the same as NewLogger, but overrides the log output paths.
func NewLoggerWithOutputPaths(outputPaths ...string) (*zap.Logger, error) {
levelEncoder := zapcore.CapitalColorLevelEncoder
if runtime.GOOS == "windows" {
levelEncoder = zapcore.CapitalLevelEncoder
}
timeKey := "T"
if os.Getenv("STORJ_LOG_NOTIME") != "" {
// using environment variable STORJ_LOG_NOTIME to avoid additional flags
timeKey = ""
}
return zap.Config{
Level: zap.NewAtomicLevelAt(*logLevel),
Development: *logDev,
DisableCaller: !*logCaller,
DisableStacktrace: !*logStack,
Encoding: *logEncoding,
EncoderConfig: zapcore.EncoderConfig{
TimeKey: timeKey,
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: levelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: outputPaths,
ErrorOutputPaths: outputPaths,
}.Build()
}