storj/pkg/process/logging.go
JT Olio df2fad15d8
pkg/process/logging: different defaults for release/dev (#2191)
* pkg/process/logging: different defaults for release/dev

Change-Id: I55be80430a31668fededf479b052e106ab18d9ce

* linting

Change-Id: I4e50d4c9569b7324c4704c14df7dd3228dbb7dd5

* Trigger Jenkins

* fix lock file

* use dev=debug and prod=info
2019-06-13 10:43:39 -06:00

72 lines
2.0 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package process
import (
"flag"
"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 isDev() bool { return cfgstruct.DefaultsType() != "release" }
func newLogger() (*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: []string{*logOutput},
ErrorOutputPaths: []string{*logOutput},
}.Build()
}