2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-09-09 12:27:09 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2019-09-10 10:35:59 +01:00
|
|
|
"net/url"
|
2019-01-08 08:22:54 +00:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2018-09-09 12:27:09 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2019-06-13 17:43:39 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2018-09-09 12:27:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Error is a process error class
|
|
|
|
Error = errs.Class("process error")
|
|
|
|
|
2019-06-13 17:43:39 +01:00
|
|
|
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")
|
2018-09-09 12:27:09 +01:00
|
|
|
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")
|
2018-09-09 12:27:09 +01:00
|
|
|
)
|
|
|
|
|
2019-09-10 10:35:59 +01:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:43:39 +01:00
|
|
|
func isDev() bool { return cfgstruct.DefaultsType() != "release" }
|
|
|
|
|
2018-09-09 12:27:09 +01:00
|
|
|
func newLogger() (*zap.Logger, error) {
|
2019-01-08 08:22:54 +00:00
|
|
|
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 = ""
|
|
|
|
}
|
|
|
|
|
2018-09-09 12:27:09 +01:00
|
|
|
return zap.Config{
|
|
|
|
Level: zap.NewAtomicLevelAt(*logLevel),
|
|
|
|
Development: *logDev,
|
|
|
|
DisableCaller: !*logCaller,
|
|
|
|
DisableStacktrace: !*logStack,
|
|
|
|
Encoding: *logEncoding,
|
|
|
|
EncoderConfig: zapcore.EncoderConfig{
|
2019-01-08 08:22:54 +00:00
|
|
|
TimeKey: timeKey,
|
2018-09-09 12:27:09 +01:00
|
|
|
LevelKey: "L",
|
|
|
|
NameKey: "N",
|
|
|
|
CallerKey: "C",
|
|
|
|
MessageKey: "M",
|
|
|
|
StacktraceKey: "S",
|
|
|
|
LineEnding: zapcore.DefaultLineEnding,
|
2019-01-08 08:22:54 +00:00
|
|
|
EncodeLevel: levelEncoder,
|
2018-09-09 12:27:09 +01:00
|
|
|
EncodeTime: zapcore.ISO8601TimeEncoder,
|
|
|
|
EncodeDuration: zapcore.StringDurationEncoder,
|
|
|
|
EncodeCaller: zapcore.ShortCallerEncoder,
|
|
|
|
},
|
|
|
|
OutputPaths: []string{*logOutput},
|
|
|
|
ErrorOutputPaths: []string{*logOutput},
|
|
|
|
}.Build()
|
|
|
|
}
|