mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
refactored log
This commit is contained in:
parent
f04df0678d
commit
ca2521e9ea
79
log/log.go
Normal file
79
log/log.go
Normal file
@ -0,0 +1,79 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LogLevel logging level enum
|
||||
type Level int
|
||||
|
||||
const (
|
||||
// LevelError an error - as bad as it gets
|
||||
LevelError Level = 0
|
||||
// LevelRecord put this to the logs in any case
|
||||
LevelRecord = 1
|
||||
// LevelWarning not that bad
|
||||
LevelWarning = 2
|
||||
// LevelNotice almost on debug level
|
||||
LevelNotice = 3
|
||||
// LevelDebug we are debugging
|
||||
LevelDebug = 4
|
||||
)
|
||||
|
||||
// SelectedLevel selected log level
|
||||
var SelectedLevel Level = LevelDebug
|
||||
|
||||
var prefices = map[Level]string{
|
||||
LevelRecord: "record : ",
|
||||
LevelError: "error : ",
|
||||
LevelWarning: "warning : ",
|
||||
LevelNotice: "notice : ",
|
||||
LevelDebug: "debug : ",
|
||||
}
|
||||
|
||||
func log(msg string, level Level) string {
|
||||
if level <= SelectedLevel {
|
||||
prefix := time.Now().Format(time.RFC3339Nano) + " " + prefices[level]
|
||||
lines := strings.Split(msg, "\n")
|
||||
for i := 0; i < len(lines); i++ {
|
||||
fmt.Println(prefix + lines[i])
|
||||
}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func logThings(msgs []interface{}, level Level) string {
|
||||
r := ""
|
||||
for _, msg := range msgs {
|
||||
r += "\n" + fmt.Sprint(msg)
|
||||
}
|
||||
r = strings.Trim(r, "\n")
|
||||
return log(r, level)
|
||||
}
|
||||
|
||||
// Debug write debug messages to the log
|
||||
func Debug(msgs ...interface{}) string {
|
||||
return logThings(msgs, LevelDebug)
|
||||
}
|
||||
|
||||
// Notice write notice messages to the log
|
||||
func Notice(msgs ...interface{}) string {
|
||||
return logThings(msgs, LevelNotice)
|
||||
}
|
||||
|
||||
// Warning write warning messages to the log
|
||||
func Warning(msgs ...interface{}) string {
|
||||
return logThings(msgs, LevelWarning)
|
||||
}
|
||||
|
||||
// Record write record messages to the log
|
||||
func Record(msgs ...interface{}) string {
|
||||
return logThings(msgs, LevelRecord)
|
||||
}
|
||||
|
||||
// Error write error messages to the log
|
||||
func Error(msgs ...interface{}) string {
|
||||
return logThings(msgs, LevelError)
|
||||
}
|
||||
@ -1,89 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var logLevel int = LOG_LEVEL_DEBUG
|
||||
var numErrors int64 = 0
|
||||
|
||||
const (
|
||||
LOG_LEVEL_ERROR = 0
|
||||
LOG_LEVEL_NAME_ERROR = "error"
|
||||
LOG_LEVEL_RECORD = 1
|
||||
LOG_LEVEL_NAME_RECORD = "record"
|
||||
LOG_LEVEL_WARNING = 2
|
||||
LOG_LEVEL_NAME_WARNING = "warning"
|
||||
LOG_LEVEL_NOTICE = 3
|
||||
LOG_LEVEL_NAME_NOTICE = "notice"
|
||||
LOG_LEVEL_DEBUG = 4
|
||||
LOG_LEVEL_NAME_DEBUG = "debug"
|
||||
)
|
||||
|
||||
var prefices = map[int]string{
|
||||
LOG_LEVEL_RECORD: "record : ",
|
||||
LOG_LEVEL_ERROR: "error : ",
|
||||
LOG_LEVEL_WARNING: "warning : ",
|
||||
LOG_LEVEL_NOTICE: "notice : ",
|
||||
LOG_LEVEL_DEBUG: "debug : ",
|
||||
}
|
||||
|
||||
var logLevelMap = map[string]int{
|
||||
LOG_LEVEL_NAME_ERROR: LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_NAME_RECORD: LOG_LEVEL_RECORD,
|
||||
LOG_LEVEL_NAME_WARNING: LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_NAME_NOTICE: LOG_LEVEL_NOTICE,
|
||||
LOG_LEVEL_NAME_DEBUG: LOG_LEVEL_DEBUG,
|
||||
}
|
||||
|
||||
func GetLogLevelByName(name string) int {
|
||||
if level, ok := logLevelMap[name]; ok {
|
||||
return level
|
||||
} else {
|
||||
return LOG_LEVEL_RECORD
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func log(msg string, level int) string {
|
||||
if level <= logLevel {
|
||||
prefix := time.Now().Format(time.RFC3339Nano) + " " + prefices[level]
|
||||
lines := strings.Split(msg, "\n")
|
||||
for i := 0; i < len(lines); i++ {
|
||||
fmt.Println(prefix + lines[i])
|
||||
}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func SetLogLevel(level int) bool {
|
||||
if level >= LOG_LEVEL_ERROR && level <= LOG_LEVEL_DEBUG {
|
||||
logLevel = level
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func Debug(msg string) string {
|
||||
return log(msg, LOG_LEVEL_DEBUG)
|
||||
}
|
||||
|
||||
func Notice(msg string) string {
|
||||
return log(msg, LOG_LEVEL_NOTICE)
|
||||
}
|
||||
|
||||
func Warning(msg string) string {
|
||||
return log(msg, LOG_LEVEL_WARNING)
|
||||
}
|
||||
|
||||
func Record(msg string) string {
|
||||
return log(msg, LOG_LEVEL_RECORD)
|
||||
}
|
||||
|
||||
func Error(msg string) string {
|
||||
numErrors++
|
||||
return log(fmt.Sprintf("(%d) ", numErrors)+msg, LOG_LEVEL_ERROR)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user