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\n", 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)
		}
	}
}

log format

// LogFormats defines formats for logging (i.e. "color")
var LogFormats = map[string]string{
	"nocolor": "%{time:2006-01-02 15:04:05.000000} %{level} %{module} %{shortfile}: %{message}",
	"color": ansiGray + "%{time:15:04:05.000} %{color}%{level:5.5s} " + ansiBlue +
		"%{module:10.10s}: %{color:reset}%{message} " + ansiGray + "%{shortfile}%{color:reset}",
}

var defaultLogFormat = "color"

By default, format is ‘color’, even when specifying envLoggingFile. This can be changed by calling:

gologging.SetFormatter(gologging.MustStringFormatter(logging.LogFormats["nocolor"]))

2. log entities

blockstore
pubsub
p2p-mount
ipns-repub
command
mdns
routing/record
keystore
pathresolv
fsrepo
corerepo
cmds/files
core/server
mplex
addrutil
engine
discovery
autorelay
pin
ipns
core/coreapi
core/commands
cmd/ipfs
relay
autonat
p2p-config
filestore
provider.simple
mount
core
peerstore
nat
plugin/loader
reuseport-transport
bitswap
namesys
provider.queue
flatfs
ping
chunk
core/commands/ipns
lock
ulimit
reprovider.simple
table
peerqueue
dht
pubsub-valuestore
p2pnode
cmds/http
tcp-tpt
dht.pb
cmds
mfs
badger
tarfmt
swarm2
bitswap_network
basichost
routedhost
blockservice
coreunix
gc
metrics-prometheus
bootstrap
stream-upgrader
nsresolv
core/commands/cmdenv
secio
providers
connmgr
autonat-svc
socket-activation
cmds/cli
eventlog
net/identify

发布了11 篇原创文章 · 获赞 4 · 访问量 552

猜你喜欢

转载自blog.csdn.net/m0_37889044/article/details/104418678
log