2018-07-04 10:51:47 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
tracer "github.com/ipfs/go-log/tracer"
|
2019-06-09 07:24:20 +00:00
|
|
|
lwriter "github.com/ipfs/go-log/writer"
|
2021-06-16 20:19:45 +00:00
|
|
|
"os"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
opentrace "github.com/opentracing/opentracing-go"
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
log2 "github.com/ipfs/go-log/v2"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SetupLogging()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logging environment variables
|
|
|
|
const (
|
2019-06-09 07:24:20 +00:00
|
|
|
envTracingFile = "GOLOG_TRACING_FILE" // /path/to/file
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetupLogging() {
|
2021-06-16 20:19:45 +00:00
|
|
|
// We're importing V2. Given that we setup logging on init, we should be
|
|
|
|
// fine skipping the rest of the initialization.
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// TracerPlugins are instantiated after this, so use loggable tracer
|
|
|
|
// by default, if a TracerPlugin is added it will override this
|
|
|
|
lgblRecorder := tracer.NewLoggableRecorder()
|
|
|
|
lgblTracer := tracer.New(lgblRecorder)
|
|
|
|
opentrace.SetGlobalTracer(lgblTracer)
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
if tracingfp := os.Getenv(envTracingFile); len(tracingfp) > 0 {
|
|
|
|
f, err := os.Create(tracingfp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to create tracing file: %s", tracingfp)
|
|
|
|
} else {
|
|
|
|
lwriter.WriterGroup.AddWriter(f)
|
|
|
|
}
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDebugLogging calls SetAllLoggers with logging.DEBUG
|
|
|
|
func SetDebugLogging() {
|
2021-06-16 20:19:45 +00:00
|
|
|
log2.SetDebugLogging()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// SetAllLoggers changes the logging level of all loggers to lvl
|
|
|
|
func SetAllLoggers(lvl LogLevel) {
|
|
|
|
log2.SetAllLoggers(lvl)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogLevel changes the log level of a specific subsystem
|
|
|
|
// name=="*" changes all subsystems
|
|
|
|
func SetLogLevel(name, level string) error {
|
2021-06-16 20:19:45 +00:00
|
|
|
return log2.SetLogLevel(name, level)
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// 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 {
|
|
|
|
return log2.SetLogLevelRegex(e, l)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubsystems returns a slice containing the
|
|
|
|
// names of the current loggers
|
|
|
|
func GetSubsystems() []string {
|
2021-06-16 20:19:45 +00:00
|
|
|
return log2.GetSubsystems()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|