forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RSDK-5481: Reduce surface area of log creation APIs. (viamrobotics#3136)
Zap has a very rich set of customization that would be expensive for a new implementation to keep parity with. Thankfully we only use a small subset of zap's configuration. This patch/refactor removes unused (or lightly used) code and consolidates the configuration down to (nearly) a single spot. A description of the existing logger configurations. There were four public `golog.New*Logger` API methods, mapping to three distinct configurations: * NewLogger -> NewProductionLogger * NewProductionLogger * NewDevelopmentLogger * NewDebugLogger There are now two logger creation APIs: `NewLogger` and `NewDebugLogger`. `NewLogger` maps to existing `NewDevelopmentLogger`. `NewDebugLogger` remained the same. The (prior) Development and Debug logger were exactly the same except for log level (info vs debug). That distinction is the same post-PR. The "ProductionLogger" logged at info and had a few differences: * Used lower-case uncolored text for error levels in logs. Development/debug used capitals + ascii color codes * Encoded time as "seconds since epoch" vs ISO 8601 dates * And `time.Duration`s were encoded as floats in seconds versus a string encoding. The main viam server only used the `NewDevelopmentLogger` and `NewDebugLogger`. There were only a handful of "production logger" instances. Relegated to some CLI programs and a few log lines in other code calling `NewLogger`. There wasn't a lot of meaningful log content in those usages, so I've deemed the usages incidental to using a "production logger" and not intentional. Other miscellaneous changes * `ReplaceGoabl` -> `ReplaceGlobal` * Remove GPC (google cloud provider) loggers. Those were from golog and only used from `viam-robotics/app`. * Removed Panic/DPanic APIs from Logger. * There was one usage of `logger.Panic` and it was confusing to say the least. The behavior was largely retained by separating logger calls + panic call.
- Loading branch information
Showing
14 changed files
with
165 additions
and
320 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
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,76 @@ | ||
package logging | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Level is an enum of log levels. Its value can be `DEBUG`, `INFO`, `WARN` or `ERROR`. | ||
type Level int | ||
|
||
const ( | ||
// This numbering scheme serves two purposes: | ||
// - A statement is logged if its log level matches or exceeds the configured level. I.e: | ||
// Statement(WARN) >= LogConfig(INFO) would be logged because "1" > "0". | ||
// - INFO is the default level. So we start counting at DEBUG=-1 such that INFO is given Go's | ||
// zero-value. | ||
|
||
// DEBUG log level. | ||
DEBUG Level = iota - 1 | ||
// INFO log level. | ||
INFO | ||
// WARN log level. | ||
WARN | ||
// ERROR log level. | ||
ERROR | ||
) | ||
|
||
func (level Level) String() string { | ||
switch level { | ||
case DEBUG: | ||
return "Debug" | ||
case INFO: | ||
return "Info" | ||
case WARN: | ||
return "Warn" | ||
case ERROR: | ||
return "Error" | ||
} | ||
|
||
panic(fmt.Sprintf("unreachable: %d", level)) | ||
} | ||
|
||
// LevelFromString parses an input string to a log level. The string must be one of `debug`, `info`, | ||
// `warn` or `error`. The parsing is case-insensitive. An error is returned if the input does not | ||
// match one of labeled cases. | ||
func LevelFromString(inp string) (Level, error) { | ||
switch strings.ToLower(inp) { | ||
case "debug": | ||
return DEBUG, nil | ||
case "info": | ||
return INFO, nil | ||
case "warn": | ||
return WARN, nil | ||
case "error": | ||
return ERROR, nil | ||
} | ||
|
||
return DEBUG, fmt.Errorf("unknown log level: %q", inp) | ||
} | ||
|
||
// MarshalJSON converts a log level to a json string. | ||
func (level Level) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(level.String()) | ||
} | ||
|
||
// UnmarshalJSON converts a json string to a log level. | ||
func (level *Level) UnmarshalJSON(data []byte) (err error) { | ||
var levelStr string | ||
if err := json.Unmarshal(data, &levelStr); err != nil { | ||
return err | ||
} | ||
|
||
*level, err = LevelFromString(levelStr) | ||
return | ||
} |
Oops, something went wrong.