3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 21:39:25 +01:00

logger: Initial colours

This commit is contained in:
Daniel Oaks 2017-03-06 16:28:38 +10:00
parent 29e89b16a0
commit 3d443339e5

View File

@ -8,6 +8,8 @@ import (
"fmt" "fmt"
"os" "os"
"time" "time"
"github.com/mgutz/ansi"
) )
// LogLevel represents the level to log messages at. // LogLevel represents the level to log messages at.
@ -139,11 +141,29 @@ func (logger *SingleLogger) Log(level LogLevel, logType, object, message string)
} }
// assemble full line // assemble full line
fullString := fmt.Sprintf("%s : %s : %s : %s : %s", time.Now().UTC().Format("2006-01-02T15:04:05Z"), logLevelDisplayNames[level], logType, object, message) grey := ansi.ColorFunc("8")
alert := ansi.ColorFunc("232+b:red")
warn := ansi.ColorFunc("black:214")
info := ansi.ColorFunc("33")
debug := ansi.ColorFunc("78")
levelDisplay := logLevelDisplayNames[level]
if level == LogError {
levelDisplay = alert(levelDisplay)
} else if level == LogWarn {
levelDisplay = warn(levelDisplay)
} else if level == LogInfo {
levelDisplay = info(levelDisplay)
} else if level == LogDebug {
levelDisplay = debug(levelDisplay)
}
fullStringFormatted := fmt.Sprintf("%s %s : %s : %s : %s", grey(time.Now().UTC().Format("2006-01-02T15:04:05Z")), levelDisplay, logType, object, message)
fullString := fmt.Sprintf("%s %s : %s : %s : %s", time.Now().UTC().Format("2006-01-02T15:04:05Z"), logLevelDisplayNames[level], logType, object, message)
// output // output
if logger.MethodSTDERR { if logger.MethodSTDERR {
fmt.Fprintln(os.Stderr, fullString) fmt.Fprintln(os.Stderr, fullStringFormatted)
} }
if logger.MethodFile.Enabled { if logger.MethodFile.Enabled {
logger.MethodFile.Writer.WriteString(fullString + "\n") logger.MethodFile.Writer.WriteString(fullString + "\n")