sequence
This article focuses on the klog header
println
K8s. IO/klog/[email protected] / klog. Go
func (l *loggingT) println(s severity, logr logr.Logger, filter LogFilter, args ... interface{}) { buf, file, line := l.header(s, 0) // if logr is set, we clear the generated header as we rely on the backing // logr implementation to print headers if logr ! = nil { l.putBuffer(buf) buf = l.getBuffer() } if filter ! = nil { args = filter.Filter(args) } fmt.Fprintln(buf, args...) l.output(s, logr, buf, file, line, false) }Copy the code
Println executes l.reader (s, 0), l.pubuffer (buf) if logr is not nil, and then resets buf
header
K8s. IO/klog/[email protected] / klog. Go
func (l *loggingT) header(s severity, depth int) (*buffer, string, int) { _, file, line, ok := runtime.Caller(3 + depth) if ! ok { file = "???" line = 1 } else { if slash := strings.LastIndex(file, "/"); slash >= 0 { path := file file = path[slash+1:] if l.addDirHeader { if dirsep := strings.LastIndex(path[:slash], "/"); dirsep >= 0 { file = path[dirsep+1:] } } } } return l.formatHeader(s, file, line), file, line }Copy the code
The header method ends with l.vormatheader
formatHeader
K8s. IO/klog/[email protected] / klog. Go
// formatHeader formats a log header using the provided file name and line number.
func (l *loggingT) formatHeader(s severity, file string, line int) *buffer {
now := timeNow()
if line < 0 {
line = 0 // not a real line number, but acceptable to someDigits
}
if s > fatalLog {
s = infoLog // for safety.
}
buf := l.getBuffer()
if l.skipHeaders {
return buf
}
// Avoid Fprintf, for speed. The format is so simple that we can do it quickly by hand.
// It's worth about 3X. Fprintf is hard.
_, month, day := now.Date()
hour, minute, second := now.Clock()
// Lmmdd hh:mm:ss.uuuuuu threadid file:line]
buf.tmp[0] = severityChar[s]
buf.twoDigits(1, int(month))
buf.twoDigits(3, day)
buf.tmp[5] = ' '
buf.twoDigits(6, hour)
buf.tmp[8] = ':'
buf.twoDigits(9, minute)
buf.tmp[11] = ':'
buf.twoDigits(12, second)
buf.tmp[14] = '.'
buf.nDigits(6, 15, now.Nanosecond()/1000, '0')
buf.tmp[21] = ' '
buf.nDigits(7, 22, pid, ' ') // TODO: should be TID
buf.tmp[29] = ' '
buf.Write(buf.tmp[:30])
buf.WriteString(file)
buf.tmp[0] = ':'
n := buf.someDigits(1, line)
buf.tmp[n+1] = ']'
buf.tmp[n+2] = ' '
buf.Write(buf.tmp[:n+3])
return buf
}
Copy the code
If l.skipheaders is set, format is not used
The instance
func headerDemo() {
flag.Set("skip_headers", "true")
klog.Info("hello by Info")
klog.InfoDepth(0, "hello by InfoDepth 0")
klog.InfoDepth(1, "hello by InfoDepth 1")
klog.Infoln("hello by Infoln")
klog.Infof("hello by %s", "Infof")
klog.InfoS("Pod status updated", "pod", "kubedns", "status", "ready")
}
Copy the code
The output
hello by Info
hello by InfoDepth 0
hello by InfoDepth 1
hello by Infoln
hello by Infof
"Pod status updated" pod="kubedns" status="ready"
Copy the code
The default output with header is as follows
I1229 23:45:57.827487 2176 klog_demo.go:41] Hello by Info I1229 23:45:57.827591 2176 klog_demo.go:42] Hello by InfoDepth 0 I1229 23:45:57.827600 2176 klog_demo.go:31] Hello by InfoDepth 1 I1229 23:45:57.827605 2176 klog_demo.go:44] Hello by InfoDepth 1 I1229 23:45:57.827605 2176 klog_demo.go:44] Hello by InfoDepth 1 I1229 23:45:57.827605 2176 klog_demo Infoln I1229 23:45:57.827608 2176 klog_demo.go:45] "Infoln I1229 23:45:57.827608 2176 klog_demo.go:45]" Infoln I1229 23:45:57.827617 2176 klog_demo.go:46 status updated" pod="kubedns" status="ready"Copy the code
summary
If skip_headers of klog is set to true, l.skipheaders of klog is true, and header information is not formatted.
doc
- klog