2021-06-16 20:19:45 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2021-10-19 13:43:41 +00:00
|
|
|
"strings"
|
2021-06-16 20:19:45 +00:00
|
|
|
"sync"
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
2021-06-16 20:19:45 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
var config Config
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
func init() {
|
|
|
|
SetupLogging(configFromEnv())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logging environment variables
|
|
|
|
const (
|
|
|
|
// IPFS_* prefixed env vars kept for backwards compatibility
|
|
|
|
// for this release. They will not be available in the next
|
|
|
|
// release.
|
|
|
|
//
|
|
|
|
// GOLOG_* env vars take precedences over IPFS_* env vars.
|
|
|
|
envIPFSLogging = "IPFS_LOGGING"
|
|
|
|
envIPFSLoggingFmt = "IPFS_LOGGING_FMT"
|
|
|
|
|
|
|
|
envLogging = "GOLOG_LOG_LEVEL"
|
|
|
|
envLoggingFmt = "GOLOG_LOG_FMT"
|
|
|
|
|
|
|
|
envLoggingFile = "GOLOG_FILE" // /path/to/file
|
2021-10-19 13:43:41 +00:00
|
|
|
envLoggingURL = "GOLOG_URL" // url that will be processed by sink in the zap
|
|
|
|
|
|
|
|
envLoggingOutput = "GOLOG_OUTPUT" // possible values: stdout|stderr|file combine multiple values with '+'
|
|
|
|
envLoggingLabels = "GOLOG_LOG_LABELS" // comma-separated key-value pairs, i.e. "app=example_app,dc=sjc-1"
|
2021-06-16 20:19:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LogFormat int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ColorizedOutput LogFormat = iota
|
|
|
|
PlaintextOutput
|
|
|
|
JSONOutput
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
// Format overrides the format of the log output. Defaults to ColorizedOutput
|
|
|
|
Format LogFormat
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
// Level is the default minimum enabled logging level.
|
2021-06-16 20:19:45 +00:00
|
|
|
Level LogLevel
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
// SubsystemLevels are the default levels per-subsystem. When unspecified, defaults to Level.
|
|
|
|
SubsystemLevels map[string]LogLevel
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// Stderr indicates whether logs should be written to stderr.
|
|
|
|
Stderr bool
|
|
|
|
|
|
|
|
// Stdout indicates whether logs should be written to stdout.
|
|
|
|
Stdout bool
|
|
|
|
|
|
|
|
// File is a path to a file that logs will be written to.
|
|
|
|
File string
|
2021-10-19 13:43:41 +00:00
|
|
|
|
|
|
|
// URL with schema supported by zap. Use zap.RegisterSink
|
|
|
|
URL string
|
|
|
|
|
|
|
|
// Labels is a set of key-values to apply to all loggers
|
|
|
|
Labels map[string]string
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrNoSuchLogger is returned when the util pkg is asked for a non existant logger
|
2021-10-19 13:43:41 +00:00
|
|
|
var ErrNoSuchLogger = errors.New("error: No such logger")
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
var loggerMutex sync.RWMutex // guards access to global logger state
|
|
|
|
|
|
|
|
// loggers is the set of loggers in the system
|
|
|
|
var loggers = make(map[string]*zap.SugaredLogger)
|
|
|
|
var levels = make(map[string]zap.AtomicLevel)
|
|
|
|
|
|
|
|
// primaryFormat is the format of the primary core used for logging
|
|
|
|
var primaryFormat LogFormat = ColorizedOutput
|
|
|
|
|
|
|
|
// defaultLevel is the default log level
|
|
|
|
var defaultLevel LogLevel = LevelError
|
|
|
|
|
|
|
|
// primaryCore is the primary logging core
|
|
|
|
var primaryCore zapcore.Core
|
|
|
|
|
|
|
|
// loggerCore is the base for all loggers created by this package
|
|
|
|
var loggerCore = &lockedMultiCore{}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
// GetConfig returns a copy of the saved config. It can be inspected, modified,
|
|
|
|
// and re-applied using a subsequent call to SetupLogging().
|
|
|
|
func GetConfig() Config {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// SetupLogging will initialize the logger backend and set the flags.
|
|
|
|
// TODO calling this in `init` pushes all configuration to env variables
|
|
|
|
// - move it out of `init`? then we need to change all the code (js-ipfs, go-ipfs) to call this explicitly
|
|
|
|
// - have it look for a config file? need to define what that is
|
|
|
|
func SetupLogging(cfg Config) {
|
|
|
|
loggerMutex.Lock()
|
|
|
|
defer loggerMutex.Unlock()
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
config = cfg
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
primaryFormat = cfg.Format
|
|
|
|
defaultLevel = cfg.Level
|
|
|
|
|
|
|
|
outputPaths := []string{}
|
|
|
|
|
|
|
|
if cfg.Stderr {
|
|
|
|
outputPaths = append(outputPaths, "stderr")
|
|
|
|
}
|
|
|
|
if cfg.Stdout {
|
|
|
|
outputPaths = append(outputPaths, "stdout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we log to a file
|
|
|
|
if len(cfg.File) > 0 {
|
|
|
|
if path, err := normalizePath(cfg.File); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to resolve log path '%q', logging to %s\n", cfg.File, outputPaths)
|
|
|
|
} else {
|
|
|
|
outputPaths = append(outputPaths, path)
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
if len(cfg.URL) > 0 {
|
|
|
|
outputPaths = append(outputPaths, cfg.URL)
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
ws, _, err := zap.Open(outputPaths...)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to open logging output: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
newPrimaryCore := newCore(primaryFormat, ws, LevelDebug) // the main core needs to log everything.
|
2021-10-19 13:43:41 +00:00
|
|
|
|
|
|
|
for k, v := range cfg.Labels {
|
|
|
|
newPrimaryCore = newPrimaryCore.With([]zap.Field{zap.String(k, v)})
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
setPrimaryCore(newPrimaryCore)
|
2021-06-16 20:19:45 +00:00
|
|
|
setAllLoggers(defaultLevel)
|
2021-10-19 13:43:41 +00:00
|
|
|
|
|
|
|
for name, level := range cfg.SubsystemLevels {
|
|
|
|
if leveler, ok := levels[name]; ok {
|
|
|
|
leveler.SetLevel(zapcore.Level(level))
|
|
|
|
} else {
|
|
|
|
levels[name] = zap.NewAtomicLevelAt(zapcore.Level(level))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrimaryCore changes the primary logging core. If the SetupLogging was
|
|
|
|
// called then the previously configured core will be replaced.
|
|
|
|
func SetPrimaryCore(core zapcore.Core) {
|
|
|
|
loggerMutex.Lock()
|
|
|
|
defer loggerMutex.Unlock()
|
|
|
|
|
|
|
|
setPrimaryCore(core)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setPrimaryCore(core zapcore.Core) {
|
|
|
|
if primaryCore != nil {
|
|
|
|
loggerCore.ReplaceCore(primaryCore, core)
|
|
|
|
} else {
|
|
|
|
loggerCore.AddCore(core)
|
|
|
|
}
|
|
|
|
primaryCore = core
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDebugLogging calls SetAllLoggers with logging.DEBUG
|
|
|
|
func SetDebugLogging() {
|
|
|
|
SetAllLoggers(LevelDebug)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAllLoggers changes the logging level of all loggers to lvl
|
|
|
|
func SetAllLoggers(lvl LogLevel) {
|
|
|
|
loggerMutex.RLock()
|
|
|
|
defer loggerMutex.RUnlock()
|
|
|
|
|
|
|
|
setAllLoggers(lvl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setAllLoggers(lvl LogLevel) {
|
|
|
|
for _, l := range levels {
|
|
|
|
l.SetLevel(zapcore.Level(lvl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogLevel changes the log level of a specific subsystem
|
|
|
|
// name=="*" changes all subsystems
|
|
|
|
func SetLogLevel(name, level string) error {
|
|
|
|
lvl, err := LevelFromString(level)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wildcard, change all
|
|
|
|
if name == "*" {
|
|
|
|
SetAllLoggers(lvl)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
loggerMutex.RLock()
|
|
|
|
defer loggerMutex.RUnlock()
|
|
|
|
|
|
|
|
// Check if we have a logger by that name
|
|
|
|
if _, ok := levels[name]; !ok {
|
|
|
|
return ErrNoSuchLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
levels[name].SetLevel(zapcore.Level(lvl))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogLevelRegex sets all loggers to level `l` that match expression `e`.
|
|
|
|
// An error is returned if `e` fails to compile.
|
|
|
|
func SetLogLevelRegex(e, l string) error {
|
|
|
|
lvl, err := LevelFromString(l)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rem, err := regexp.Compile(e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
loggerMutex.Lock()
|
|
|
|
defer loggerMutex.Unlock()
|
|
|
|
for name := range loggers {
|
|
|
|
if rem.MatchString(name) {
|
|
|
|
levels[name].SetLevel(zapcore.Level(lvl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubsystems returns a slice containing the
|
|
|
|
// names of the current loggers
|
|
|
|
func GetSubsystems() []string {
|
|
|
|
loggerMutex.RLock()
|
|
|
|
defer loggerMutex.RUnlock()
|
|
|
|
subs := make([]string, 0, len(loggers))
|
|
|
|
|
|
|
|
for k := range loggers {
|
|
|
|
subs = append(subs, k)
|
|
|
|
}
|
|
|
|
return subs
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogger(name string) *zap.SugaredLogger {
|
|
|
|
loggerMutex.Lock()
|
|
|
|
defer loggerMutex.Unlock()
|
|
|
|
log, ok := loggers[name]
|
|
|
|
if !ok {
|
2021-10-19 13:43:41 +00:00
|
|
|
level, ok := levels[name]
|
|
|
|
if !ok {
|
|
|
|
level = zap.NewAtomicLevelAt(zapcore.Level(defaultLevel))
|
|
|
|
levels[name] = level
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
log = zap.New(loggerCore).
|
2021-10-19 13:43:41 +00:00
|
|
|
WithOptions(
|
|
|
|
zap.IncreaseLevel(level),
|
|
|
|
zap.AddCaller(),
|
|
|
|
).
|
2021-06-16 20:19:45 +00:00
|
|
|
Named(name).
|
|
|
|
Sugar()
|
|
|
|
|
|
|
|
loggers[name] = log
|
|
|
|
}
|
|
|
|
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
|
|
|
// configFromEnv returns a Config with defaults populated using environment variables.
|
|
|
|
func configFromEnv() Config {
|
|
|
|
cfg := Config{
|
2021-10-19 13:43:41 +00:00
|
|
|
Format: ColorizedOutput,
|
|
|
|
Stderr: true,
|
|
|
|
Level: LevelError,
|
|
|
|
SubsystemLevels: map[string]LogLevel{},
|
|
|
|
Labels: map[string]string{},
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format := os.Getenv(envLoggingFmt)
|
|
|
|
if format == "" {
|
|
|
|
format = os.Getenv(envIPFSLoggingFmt)
|
|
|
|
}
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
var noExplicitFormat bool
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
switch format {
|
2021-10-19 13:43:41 +00:00
|
|
|
case "color":
|
|
|
|
cfg.Format = ColorizedOutput
|
2021-06-16 20:19:45 +00:00
|
|
|
case "nocolor":
|
|
|
|
cfg.Format = PlaintextOutput
|
|
|
|
case "json":
|
|
|
|
cfg.Format = JSONOutput
|
2021-10-19 13:43:41 +00:00
|
|
|
default:
|
|
|
|
if format != "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "ignoring unrecognized log format '%s'\n", format)
|
|
|
|
}
|
|
|
|
noExplicitFormat = true
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lvl := os.Getenv(envLogging)
|
|
|
|
if lvl == "" {
|
|
|
|
lvl = os.Getenv(envIPFSLogging)
|
|
|
|
}
|
|
|
|
if lvl != "" {
|
2021-10-19 13:43:41 +00:00
|
|
|
for _, kvs := range strings.Split(lvl, ",") {
|
|
|
|
kv := strings.SplitN(kvs, "=", 2)
|
|
|
|
lvl, err := LevelFromString(kv[len(kv)-1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error setting log level %q: %s\n", kvs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch len(kv) {
|
|
|
|
case 1:
|
|
|
|
cfg.Level = lvl
|
|
|
|
case 2:
|
|
|
|
cfg.SubsystemLevels[kv[0]] = lvl
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.File = os.Getenv(envLoggingFile)
|
|
|
|
// Disable stderr logging when a file is specified
|
|
|
|
// https://github.com/ipfs/go-log/issues/83
|
|
|
|
if cfg.File != "" {
|
|
|
|
cfg.Stderr = false
|
|
|
|
}
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
cfg.URL = os.Getenv(envLoggingURL)
|
|
|
|
output := os.Getenv(envLoggingOutput)
|
|
|
|
outputOptions := strings.Split(output, "+")
|
|
|
|
for _, opt := range outputOptions {
|
|
|
|
switch opt {
|
|
|
|
case "stdout":
|
|
|
|
cfg.Stdout = true
|
|
|
|
case "stderr":
|
|
|
|
cfg.Stderr = true
|
|
|
|
case "file":
|
|
|
|
if cfg.File == "" {
|
|
|
|
fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to")
|
|
|
|
}
|
|
|
|
case "url":
|
|
|
|
if cfg.URL == "" {
|
|
|
|
fmt.Fprint(os.Stderr, "please specify a GOLOG_URL value to write to")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
// Check that neither of the requested Std* nor the file are TTYs
|
|
|
|
// At this stage (configFromEnv) we do not have a uniform list to examine yet
|
2021-10-19 13:43:41 +00:00
|
|
|
if noExplicitFormat &&
|
2022-04-01 16:16:46 +00:00
|
|
|
!(cfg.Stdout && isTerm(os.Stdout)) &&
|
|
|
|
!(cfg.Stderr && isTerm(os.Stderr)) &&
|
|
|
|
// check this last: expensive
|
|
|
|
!(cfg.File != "" && pathIsTerm(cfg.File)) {
|
2021-10-19 13:43:41 +00:00
|
|
|
cfg.Format = PlaintextOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := os.Getenv(envLoggingLabels)
|
|
|
|
if labels != "" {
|
|
|
|
labelKVs := strings.Split(labels, ",")
|
|
|
|
for _, label := range labelKVs {
|
|
|
|
kv := strings.Split(label, "=")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
fmt.Fprint(os.Stderr, "invalid label k=v: ", label)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cfg.Labels[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
return cfg
|
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
|
|
|
|
func isTerm(f *os.File) bool {
|
|
|
|
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
|
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
func pathIsTerm(p string) bool {
|
|
|
|
// !!!no!!! O_CREAT, if we fail - we fail
|
|
|
|
f, err := os.OpenFile(p, os.O_WRONLY, 0)
|
|
|
|
if f != nil {
|
|
|
|
defer f.Close() // nolint:errcheck
|
|
|
|
}
|
|
|
|
return err == nil && isTerm(f)
|
|
|
|
}
|