mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-22 20:09:41 +01:00
pointless optimizations to the logger
This commit is contained in:
parent
847922e53d
commit
ec4f1c189a
@ -5,6 +5,7 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -32,6 +33,20 @@ const (
|
|||||||
LogError
|
LogError
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
colorTimeGrey = ansi.ColorFunc("243")
|
||||||
|
colorGrey = ansi.ColorFunc("8")
|
||||||
|
colorAlert = ansi.ColorFunc("232+b:red")
|
||||||
|
colorWarn = ansi.ColorFunc("black:214")
|
||||||
|
colorInfo = ansi.ColorFunc("117")
|
||||||
|
colorDebug = ansi.ColorFunc("78")
|
||||||
|
colorSection = ansi.ColorFunc("229")
|
||||||
|
separator = colorGrey(":")
|
||||||
|
|
||||||
|
colorableStdout = colorable.NewColorableStdout()
|
||||||
|
colorableStderr = colorable.NewColorableStderr()
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// LogLevelNames takes a config name and gives the real log level.
|
// LogLevelNames takes a config name and gives the real log level.
|
||||||
LogLevelNames = map[string]Level{
|
LogLevelNames = map[string]Level{
|
||||||
@ -230,51 +245,56 @@ func (logger *singleLogger) Log(level Level, logType string, messageParts ...str
|
|||||||
}
|
}
|
||||||
|
|
||||||
// assemble full line
|
// assemble full line
|
||||||
timeGrey := ansi.ColorFunc("243")
|
|
||||||
grey := ansi.ColorFunc("8")
|
|
||||||
alert := ansi.ColorFunc("232+b:red")
|
|
||||||
warn := ansi.ColorFunc("black:214")
|
|
||||||
info := ansi.ColorFunc("117")
|
|
||||||
debug := ansi.ColorFunc("78")
|
|
||||||
section := ansi.ColorFunc("229")
|
|
||||||
|
|
||||||
levelDisplay := LogLevelDisplayNames[level]
|
levelDisplay := LogLevelDisplayNames[level]
|
||||||
if level == LogError {
|
if level == LogError {
|
||||||
levelDisplay = alert(levelDisplay)
|
levelDisplay = colorAlert(levelDisplay)
|
||||||
} else if level == LogWarning {
|
} else if level == LogWarning {
|
||||||
levelDisplay = warn(levelDisplay)
|
levelDisplay = colorWarn(levelDisplay)
|
||||||
} else if level == LogInfo {
|
} else if level == LogInfo {
|
||||||
levelDisplay = info(levelDisplay)
|
levelDisplay = colorInfo(levelDisplay)
|
||||||
} else if level == LogDebug {
|
} else if level == LogDebug {
|
||||||
levelDisplay = debug(levelDisplay)
|
levelDisplay = colorDebug(levelDisplay)
|
||||||
}
|
}
|
||||||
|
|
||||||
sep := grey(":")
|
var formattedBuf, rawBuf bytes.Buffer
|
||||||
fullStringFormatted := fmt.Sprintf("%s %s %s %s %s %s ", timeGrey(time.Now().UTC().Format("2006-01-02T15:04:05.000Z")), sep, levelDisplay, sep, section(logType), sep)
|
fmt.Fprintf(&formattedBuf, "%s %s %s %s %s %s ", colorTimeGrey(time.Now().UTC().Format("2006-01-02T15:04:05.000Z")), separator, levelDisplay, separator, colorSection(logType), separator)
|
||||||
fullStringRaw := fmt.Sprintf("%s : %s : %s : ", time.Now().UTC().Format("2006-01-02T15:04:05Z"), LogLevelDisplayNames[level], logType)
|
if logger.MethodFile.Enabled {
|
||||||
|
fmt.Fprintf(&rawBuf, "%s : %s : %s : ", time.Now().UTC().Format("2006-01-02T15:04:05Z"), LogLevelDisplayNames[level], logType)
|
||||||
|
}
|
||||||
for i, p := range messageParts {
|
for i, p := range messageParts {
|
||||||
fullStringFormatted += p
|
formattedBuf.WriteString(p)
|
||||||
fullStringRaw += p
|
if logger.MethodFile.Enabled {
|
||||||
if i != len(messageParts)-1 {
|
rawBuf.WriteString(p)
|
||||||
fullStringFormatted += " " + sep + " "
|
|
||||||
fullStringRaw += " : "
|
|
||||||
}
|
}
|
||||||
|
if i != len(messageParts)-1 {
|
||||||
|
formattedBuf.WriteRune(' ')
|
||||||
|
formattedBuf.WriteString(separator)
|
||||||
|
formattedBuf.WriteRune(' ')
|
||||||
|
if logger.MethodFile.Enabled {
|
||||||
|
rawBuf.WriteString(" : ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
formattedBuf.WriteRune('\n')
|
||||||
|
if logger.MethodFile.Enabled {
|
||||||
|
rawBuf.WriteRune('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
// output
|
// output
|
||||||
if logger.MethodSTDOUT {
|
if logger.MethodSTDOUT {
|
||||||
logger.stdoutWriteLock.Lock()
|
logger.stdoutWriteLock.Lock()
|
||||||
fmt.Fprintln(colorable.NewColorableStdout(), fullStringFormatted)
|
colorableStdout.Write(formattedBuf.Bytes())
|
||||||
logger.stdoutWriteLock.Unlock()
|
logger.stdoutWriteLock.Unlock()
|
||||||
}
|
}
|
||||||
if logger.MethodSTDERR {
|
if logger.MethodSTDERR {
|
||||||
logger.stdoutWriteLock.Lock()
|
logger.stdoutWriteLock.Lock()
|
||||||
fmt.Fprintln(colorable.NewColorableStderr(), fullStringFormatted)
|
colorableStderr.Write(formattedBuf.Bytes())
|
||||||
logger.stdoutWriteLock.Unlock()
|
logger.stdoutWriteLock.Unlock()
|
||||||
}
|
}
|
||||||
if logger.MethodFile.Enabled {
|
if logger.MethodFile.Enabled {
|
||||||
logger.fileWriteLock.Lock()
|
logger.fileWriteLock.Lock()
|
||||||
logger.MethodFile.Writer.WriteString(fullStringRaw + "\n")
|
logger.MethodFile.Writer.Write(rawBuf.Bytes())
|
||||||
logger.MethodFile.Writer.Flush()
|
logger.MethodFile.Writer.Flush()
|
||||||
logger.fileWriteLock.Unlock()
|
logger.fileWriteLock.Unlock()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user