-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andreas Karis <[email protected]>
- Loading branch information
1 parent
90d0146
commit 780f460
Showing
9 changed files
with
453 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// 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.ErrorLevel | ||
containerID = "" | ||
netNS = "" | ||
ifName = "" | ||
) | ||
|
||
func init() { | ||
SetLogFile("") | ||
SetLogLevel("") | ||
} | ||
|
||
// 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) | ||
return | ||
} | ||
cnilog.SetLogFile(fileName) | ||
cnilog.SetLogStderr(false) | ||
} | ||
|
||
// SetContainerID sets the ContainerID to prepend to every log message. | ||
func SetContainerID(cID string) { | ||
containerID = cID | ||
} | ||
|
||
// SetNetNS sets the NetNS to prepend to every log message. | ||
func SetNetNS(ns string) { | ||
netNS = ns | ||
} | ||
|
||
// SetIFName sets the ifName to prepend to every log message. | ||
func SetIFName(in string) { | ||
ifName = in | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
// 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)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
}) |
Oops, something went wrong.