-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logging to sriov-cni #276
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// package logging is a small wrapper around github.com/k8snetworkplumbingwg/cni-log | ||
|
||
package logging | ||
|
||
import ( | ||
cnilog "github.com/k8snetworkplumbingwg/cni-log" | ||
) | ||
|
||
const ( | ||
labelCNIName = "cniName" | ||
labelContainerID = "containerID" | ||
labelNetNS = "netns" | ||
labelIFName = "ifname" | ||
cniName = "sriov-cni" | ||
) | ||
|
||
var ( | ||
logLevelDefault = cnilog.InfoLevel | ||
containerID = "" | ||
netNS = "" | ||
ifName = "" | ||
) | ||
|
||
// Init initializes logging with the requested parameters in this order: log level, log file, container ID, | ||
// network namespace and interface name. | ||
func Init(logLevel, logFile, containerIdentification, networkNamespace, interfaceName string) { | ||
setLogLevel(logLevel) | ||
setLogFile(logFile) | ||
containerID = containerIdentification | ||
netNS = networkNamespace | ||
ifName = interfaceName | ||
} | ||
|
||
// setLogLevel sets the log level to either verbose, debug, info, warn, error or panic. If an invalid string is | ||
// provided, it uses error. | ||
func setLogLevel(l string) { | ||
ll := cnilog.StringToLevel(l) | ||
if ll == cnilog.InvalidLevel { | ||
ll = logLevelDefault | ||
} | ||
cnilog.SetLogLevel(ll) | ||
} | ||
|
||
// setLogFile sets the log file for logging. If the empty string is provided, it uses stderr. | ||
func setLogFile(fileName string) { | ||
if fileName == "" { | ||
cnilog.SetLogStderr(true) | ||
cnilog.SetLogFile("") | ||
return | ||
} | ||
cnilog.SetLogFile(fileName) | ||
cnilog.SetLogStderr(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, is it exclusive? only file or only stderr, but not both? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah that was my idea to have an "either log to file or log to stderr". The other model would be: "log to stderr and optionally log to file as well" .. or yet another option would be to expose the stderr switch to users as well and have 3 options. But I find the either/or one most intuitive. That said, I'm open to changing it if you strongly favor something else There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's fine like it is. Just wanted to check if it was really intended. |
||
} | ||
|
||
// Debug provides structured logging for log level >= debug. | ||
func Debug(msg string, args ...interface{}) { | ||
cnilog.DebugStructured(msg, prependArgs(args)...) | ||
} | ||
|
||
// Info provides structured logging for log level >= info. | ||
func Info(msg string, args ...interface{}) { | ||
cnilog.InfoStructured(msg, prependArgs(args)...) | ||
} | ||
|
||
// Warning provides structured logging for log level >= warning. | ||
func Warning(msg string, args ...interface{}) { | ||
cnilog.WarningStructured(msg, prependArgs(args)...) | ||
} | ||
|
||
// Error provides structured logging for log level >= error. | ||
func Error(msg string, args ...interface{}) { | ||
_ = cnilog.ErrorStructured(msg, prependArgs(args)...) | ||
} | ||
|
||
// Panic provides structured logging for log level >= panic. | ||
func Panic(msg string, args ...interface{}) { | ||
cnilog.PanicStructured(msg, prependArgs(args)...) | ||
} | ||
|
||
// prependArgs prepends cniName, containerID, netNS and ifName to the args of every log message. | ||
func prependArgs(args []interface{}) []interface{} { | ||
if ifName != "" { | ||
args = append([]interface{}{labelIFName, ifName}, args...) | ||
} | ||
if netNS != "" { | ||
args = append([]interface{}{labelNetNS, netNS}, args...) | ||
} | ||
if containerID != "" { | ||
args = append([]interface{}{labelContainerID, containerID}, args...) | ||
} | ||
args = append([]interface{}{labelCNIName, cniName}, args...) | ||
return args | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package logging | ||
|
||
import ( | ||
"testing" | ||
|
||
g "github.com/onsi/ginkgo/v2" | ||
o "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
o.RegisterFailHandler(g.Fail) | ||
g.RunSpecs(t, "Logging Suite") | ||
} | ||
|
||
var _ = g.BeforeSuite(func() { | ||
}) | ||
|
||
var _ = g.AfterSuite(func() { | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no needed if you remove it from init and set in main.go only if it is not empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you set the default level in init, then you can do:
if ll:= cnilog.StringToLevel(l), ll != cnilog.InvalidLevel {
cnilog.SetLogLevel(ll)
}