ipfs, log
1. ENV variables for ipfs log behaviours
// Logging environment variables
const (
// TODO these env names should be more general, IPFS is not the only project to
// use go-log
envLogging = "IPFS_LOGGING"
envLoggingFmt = "IPFS_LOGGING_FMT"
envLoggingFile = "GOLOG_FILE" // /path/to/file
envTracingFile = "GOLOG_TRACING_FILE" // /path/to/file
)
initilization of log
// 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() {
// colorful or plain
lfmt := LogFormats[os.Getenv(envLoggingFmt)] if lfmt == "" {
lfmt = LogFormats[defaultLogFormat] }
// check if we log to a file
var lgbe []logging.Backend
if logfp := os.Getenv(envLoggingFile); len(logfp) > 0 {
f, err := os.Create(logfp)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR go-log: %s: failed to set logging file backend", err)
} else {
lgbe = append(lgbe, logging.NewLogBackend(f, "", 0))
}
}
// logs written to stderr
lgbe = append(lgbe, logging.NewLogBackend(colorable.NewColorableStderr(), "", 0))
// set the backend(s)
logging.SetBackend(lgbe...)
logging.SetFormatter(logging.MustStringFormatter(lfmt))
lvl := logging.ERROR
if logenv := os.Getenv(envLogging); logenv != "" {
var err error
lvl, err = logging.LogLevel(logenv)
if err != nil {
fmt.Println("error setting log levels", err)
}
}
// 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)
SetAllLoggers(lvl)
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)
}
}
}
评论0
最新资源