Skip to content

Commit

Permalink
Merge pull request #14 from andreaskaris/remove-verbose-logging-level
Browse files Browse the repository at this point in the history
Remove Verbose logging level, return InvalidLevel from StringToLevel, replace level strings with constants
  • Loading branch information
Eoghan1232 authored Aug 1, 2023
2 parents 4554653 + c44f77c commit b6e062c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 108 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type customPrefix struct {
func main() {
// cni-log configuration
logging.SetLogFile("samplelog.log")
logging.SetLogLevel(logging.VerboseLevel)
logging.SetLogLevel(logging.DebugLevel)
logging.SetLogStderr(true)

// Creating the custom prefix object
Expand Down Expand Up @@ -172,9 +172,8 @@ Sets the log level. The valid log levels are:
| 3 | warning | WarningLevel |
| 4 | info | InfoLevel |
| 5 | debug | DebugLevel |
| 6 | verbose | VerboseLevel |

The log levels above are in ascending order of verbosity. For example, setting the log level to InfoLevel would mean "panic", "error", warning", and "info" messages will get logged while "debug", and "verbose" will not.
The log levels above are in ascending order of verbosity. For example, setting the log level to InfoLevel would mean "panic", "error", warning", and "info" messages will get logged while "debug" will not.

##### GetLogLevel

Expand Down Expand Up @@ -269,9 +268,6 @@ func Infof(format string, a ...interface{})

// Debugf prints logging if logging level >= debug
func Debugf(format string, a ...interface{})

// Verbosef prints logging if logging level >= verbose
func Verbosef(format string, a ...interface{})
```

Structured (crio logging style) functions:
Expand All @@ -290,9 +286,6 @@ func InfoStructured(msg string, args ...interface{})

// DebugStructured provides structured logging for log level >= debug.
func DebugStructured(msg string, args ...interface{})

// VerboseStructured provides structured logging for log level >= verbose.
func VerboseStructured(msg string, args ...interface{})
```

### Default values
Expand Down
63 changes: 30 additions & 33 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@ Common use of different level:
"warning": Unusual event occurred (invalid input or system issue), but continuing
"info": Basic information, indication of major code paths
"debug": Additional information, indication of minor code branches
"verbose": Output of larger variables in code and debug of low level functions
*/

const (
InvalidLevel Level = -1
PanicLevel Level = 1
ErrorLevel Level = 2
WarningLevel Level = 3
InfoLevel Level = 4
DebugLevel Level = 5
VerboseLevel Level = 6
maximumLevel Level = VerboseLevel
maximumLevel Level = DebugLevel

panicStr = "panic"
errorStr = "error"
warningStr = "warning"
infoStr = "info"
debugStr = "debug"
invalidStr = "invalid"
)

const (
Expand All @@ -64,12 +70,11 @@ const (
)

var levelMap = map[string]Level{
"panic": PanicLevel,
"error": ErrorLevel,
"warning": WarningLevel,
"info": InfoLevel,
"debug": DebugLevel,
"verbose": VerboseLevel,
panicStr: PanicLevel,
errorStr: ErrorLevel,
warningStr: WarningLevel,
infoStr: InfoLevel,
debugStr: DebugLevel,
}

var logger *lumberjack.Logger
Expand Down Expand Up @@ -275,9 +280,7 @@ func StringToLevel(level string) Level {
if l, found := levelMap[strings.ToLower(level)]; found {
return l
}

fmt.Fprintf(os.Stderr, setLevelFailMsg, level)
return -1
return InvalidLevel
}

// SetLogStderr sets flag for logging stderr output
Expand All @@ -292,19 +295,19 @@ func SetLogStderr(enable bool) {
func (l Level) String() string {
switch l {
case PanicLevel:
return "panic"
case VerboseLevel:
return "verbose"
return panicStr
case WarningLevel:
return "warning"
return warningStr
case InfoLevel:
return "info"
return infoStr
case ErrorLevel:
return "error"
return errorStr
case DebugLevel:
return "debug"
return debugStr
case InvalidLevel:
return invalidStr
default:
return "unknown"
return invalidStr
}
}

Expand Down Expand Up @@ -375,17 +378,6 @@ func DebugStructured(msg string, args ...interface{}) {
printWithPrefixf(DebugLevel, false, m)
}

// Verbosef prints logging if logging level >= verbose
func Verbosef(format string, a ...interface{}) {
printf(VerboseLevel, format, a...)
}

// VerboseStructured provides structured logging for log level >= verbose.
func VerboseStructured(msg string, args ...interface{}) {
m := structuredMessage(VerboseLevel, msg, args...)
printWithPrefixf(VerboseLevel, false, m)
}

// structuredMessage takes msg and an even list of args and returns a structured message.
func structuredMessage(loggingLevel Level, msg string, args ...interface{}) string {
prefixArgs := structuredPrefixer.CreateStructuredPrefix(loggingLevel, msg)
Expand All @@ -395,7 +387,7 @@ func structuredMessage(loggingLevel Level, msg string, args ...interface{}) stri

var output []string
for i := 0; i < len(prefixArgs)-1; i += 2 {
output = append(output, fmt.Sprintf("%s=%q", prefixArgs[i], prefixArgs[i+1]))
output = append(output, fmt.Sprintf("%s=%q", argToString(prefixArgs[i]), argToString(prefixArgs[i+1])))
}

if len(args)%2 != 0 {
Expand All @@ -404,12 +396,17 @@ func structuredMessage(loggingLevel Level, msg string, args ...interface{}) stri
}

for i := 0; i < len(args)-1; i += 2 {
output = append(output, fmt.Sprintf("%s=%q", args[i], args[i+1]))
output = append(output, fmt.Sprintf("%s=%q", argToString(args[i]), argToString(args[i+1])))
}

return strings.Join(output, " ")
}

// argToString returns the string representation of the provided interface{}.
func argToString(arg interface{}) string {
return fmt.Sprintf("%+v", arg)
}

// doWritef takes care of the low level writing to the output io.Writer.
func doWritef(writer io.Writer, format string, a ...interface{}) {
fmt.Fprintf(writer, format, a...)
Expand Down
Loading

0 comments on commit b6e062c

Please sign in to comment.