Skip to content

Commit

Permalink
expose a function, for logc, instead of the array directly which didn…
Browse files Browse the repository at this point in the history
…'t change when Color mode changed
  • Loading branch information
ldemailly committed Aug 1, 2023
1 parent a05b03a commit 8d08e3f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 9 additions & 1 deletion console_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var (
Colors = ANSIColors

// Mapping of log levels to color.
LevelToColor = []string{
levelToColor = []string{
Colors.Gray,
Colors.Cyan,
Colors.Green,
Expand All @@ -76,6 +76,14 @@ var (
Color = false
)

// LevelToColor returns the color to use for the given level (or empty string when color mode is disabled).
func LevelToColor(level Level) string {
if !Color {
return ""
}
return levelToColor[level]

Check warning on line 84 in console_logging.go

View check run for this annotation

Codecov / codecov/patch

console_logging.go#L80-L84

Added lines #L80 - L84 were not covered by tests
}

// ConsoleLogging is a utility to check if the current logger output is a console (terminal).
func ConsoleLogging() bool {
f, ok := jsonWriter.(*os.File)
Expand Down
10 changes: 5 additions & 5 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func logUnconditionalf(logFileAndLine bool, lvl Level, format string, rest ...in
file = file[strings.LastIndex(file, "/")+1:]
if Color {
jsonWrite(fmt.Sprintf("%s%s%s%s %s:%d%s%s%s\n",
colorTimestamp(), colorGID(), LevelToColor[lvl], LevelToStrA[lvl][0:1],
colorTimestamp(), colorGID(), levelToColor[lvl], LevelToStrA[lvl][0:1],
file, line, Config.LogPrefix, fmt.Sprintf(format, rest...), Colors.Reset))
} else if Config.JSON {
jsonWrite(fmt.Sprintf("{%s\"level\":%s,%s\"file\":%q,\"line\":%d,\"msg\":%q}\n",
Expand All @@ -369,7 +369,7 @@ func logUnconditionalf(logFileAndLine bool, lvl Level, format string, rest ...in
} else {
if Color {
jsonWrite(fmt.Sprintf("%s%s%s%s %s%s%s\n",
colorTimestamp(), colorGID(), LevelToColor[lvl], LevelToStrA[lvl][0:1], Config.LogPrefix,
colorTimestamp(), colorGID(), levelToColor[lvl], LevelToStrA[lvl][0:1], Config.LogPrefix,
fmt.Sprintf(format, rest...), Colors.Reset))
} else if Config.JSON {
jsonWrite(fmt.Sprintf("{%s\"level\":%s,%s\"msg\":%q}\n",
Expand Down Expand Up @@ -529,7 +529,7 @@ func S(lvl Level, msg string, attrs ...KeyVal) {
buf := strings.Builder{}
var format string
if Color {
format = Colors.Reset + ", " + Colors.Blue + "%s" + Colors.Reset + "=" + LevelToColor[lvl] + "%q"
format = Colors.Reset + ", " + Colors.Blue + "%s" + Colors.Reset + "=" + levelToColor[lvl] + "%q"
} else if Config.JSON {
format = ",%q:%q"
} else {
Expand All @@ -544,7 +544,7 @@ func S(lvl Level, msg string, attrs ...KeyVal) {
file = file[strings.LastIndex(file, "/")+1:]
if Color {
jsonWrite(fmt.Sprintf("%s%s%s%s %s:%d%s%s%s%s\n",
colorTimestamp(), colorGID(), LevelToColor[lvl], LevelToStrA[lvl][0:1],
colorTimestamp(), colorGID(), levelToColor[lvl], LevelToStrA[lvl][0:1],
file, line, Config.LogPrefix, msg, buf.String(), Colors.Reset))
} else if Config.JSON {
jsonWrite(fmt.Sprintf("{%s\"level\":%s,%s\"file\":%q,\"line\":%d,\"msg\":%q%s}\n",
Expand All @@ -555,7 +555,7 @@ func S(lvl Level, msg string, attrs ...KeyVal) {
} else {
if Color {
jsonWrite(fmt.Sprintf("%s%s%s%s %s%s%s%s\n",
colorTimestamp(), colorGID(), LevelToColor[lvl], LevelToStrA[lvl][0:1], Config.LogPrefix, msg, buf.String(), Colors.Reset))
colorTimestamp(), colorGID(), levelToColor[lvl], LevelToStrA[lvl][0:1], Config.LogPrefix, msg, buf.String(), Colors.Reset))
} else if Config.JSON {
jsonWrite(fmt.Sprintf("{%s\"level\":%s,\"msg\":%q%s}\n",
jsonTimestamp(), LevelToJSON[lvl], msg, buf.String()))
Expand Down

0 comments on commit 8d08e3f

Please sign in to comment.