storj/pkg/process/logging.go

72 lines
2.0 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// 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'")
2019-01-02 18:07:49 +00:00
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()
}