mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
b55b308143
Commit 9f3d7534ea enabled logrus to show information about log
caller, if --debug is set. It is helpful in many scenarios, but does
not work very well when we are debugging runc init, for example:
# runc --debug run -d xx4557
DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: logging set up
DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: logging set up
DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: => nsexec container setup
DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: update /proc/self/oom_score_adj to '30'
As we're merely forwarding the logs here, printing out filename:line and
function is useless and clutters the logs a log.
To fix, create and use a copy of the standard logger with caller info
turned off.
With this in place, nsexec logs are sane again:
# runc --debug --log-format=text run -d xe34
DEBU[0000] nsexec[293595]: logging set up
DEBU[0000] nsexec[293595]: logging set up
DEBU[0000] nsexec[293595]: => nsexec container setup
DEBU[0000] nsexec[293595]: update /proc/self/oom_score_adj to '30'
This patch also changes Logf to Log in processEntry, as this is what it
should be.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package logs
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"io"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func ForwardLogs(logPipe io.ReadCloser) chan error {
|
|
done := make(chan error, 1)
|
|
s := bufio.NewScanner(logPipe)
|
|
|
|
logger := logrus.StandardLogger()
|
|
if logger.ReportCaller {
|
|
// Need a copy of the standard logger, but with ReportCaller
|
|
// turned off, as the logs are merely forwarded and their
|
|
// true source is not this file/line/function.
|
|
logNoCaller := *logrus.StandardLogger()
|
|
logNoCaller.ReportCaller = false
|
|
logger = &logNoCaller
|
|
}
|
|
|
|
go func() {
|
|
for s.Scan() {
|
|
processEntry(s.Bytes(), logger)
|
|
}
|
|
if err := logPipe.Close(); err != nil {
|
|
logrus.Errorf("error closing log source: %v", err)
|
|
}
|
|
// The only error we want to return is when reading from
|
|
// logPipe has failed.
|
|
done <- s.Err()
|
|
close(done)
|
|
}()
|
|
|
|
return done
|
|
}
|
|
|
|
func processEntry(text []byte, logger *logrus.Logger) {
|
|
if len(text) == 0 {
|
|
return
|
|
}
|
|
|
|
var jl struct {
|
|
Level logrus.Level `json:"level"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
if err := json.Unmarshal(text, &jl); err != nil {
|
|
logrus.Errorf("failed to decode %q to json: %v", text, err)
|
|
return
|
|
}
|
|
|
|
logger.Log(jl.Level, jl.Msg)
|
|
}
|