1
0
Fork 0
forked from mirrors/ntfy
ntfy/log/log.go

130 lines
2.6 KiB
Go
Raw Normal View History

2022-05-30 02:14:14 +00:00
package log
import (
"log"
"strings"
2022-06-01 20:57:35 +00:00
"sync"
2022-05-30 02:14:14 +00:00
)
2022-06-01 20:57:35 +00:00
// Level is a well-known log level, as defined below
2022-05-30 02:14:14 +00:00
type Level int
2022-06-01 20:57:35 +00:00
// Well known log levels
2022-05-30 02:14:14 +00:00
const (
2022-06-02 03:24:44 +00:00
TraceLevel Level = iota
DebugLevel
2022-05-30 02:14:14 +00:00
InfoLevel
WarnLevel
ErrorLevel
)
func (l Level) String() string {
switch l {
2022-06-02 03:24:44 +00:00
case TraceLevel:
return "TRACE"
2022-05-30 02:14:14 +00:00
case DebugLevel:
return "DEBUG"
case InfoLevel:
return "INFO"
case WarnLevel:
return "WARN"
case ErrorLevel:
return "ERROR"
}
return "unknown"
}
var (
level = InfoLevel
2022-06-01 20:57:35 +00:00
mu = &sync.Mutex{}
2022-05-30 02:14:14 +00:00
)
2022-06-02 03:24:44 +00:00
// Trace prints the given message, if the current log level is TRACE
func Trace(message string, v ...interface{}) {
logIf(TraceLevel, message, v...)
}
// Debug prints the given message, if the current log level is DEBUG or lower
2022-05-30 02:14:14 +00:00
func Debug(message string, v ...interface{}) {
logIf(DebugLevel, message, v...)
}
2022-06-01 20:57:35 +00:00
// Info prints the given message, if the current log level is INFO or lower
2022-05-30 02:14:14 +00:00
func Info(message string, v ...interface{}) {
logIf(InfoLevel, message, v...)
}
2022-06-01 20:57:35 +00:00
// Warn prints the given message, if the current log level is WARN or lower
2022-05-30 02:14:14 +00:00
func Warn(message string, v ...interface{}) {
logIf(WarnLevel, message, v...)
}
2022-06-01 20:57:35 +00:00
// Error prints the given message, if the current log level is ERROR or lower
2022-05-30 02:14:14 +00:00
func Error(message string, v ...interface{}) {
logIf(ErrorLevel, message, v...)
}
2022-06-01 20:57:35 +00:00
// Fatal prints the given message, and exits the program
2022-05-30 02:14:14 +00:00
func Fatal(v ...interface{}) {
log.Fatalln(v...)
}
2022-06-01 20:57:35 +00:00
// CurrentLevel returns the current log level
func CurrentLevel() Level {
mu.Lock()
defer mu.Unlock()
return level
}
// SetLevel sets a new log level
2022-05-30 02:14:14 +00:00
func SetLevel(newLevel Level) {
2022-06-01 20:57:35 +00:00
mu.Lock()
defer mu.Unlock()
2022-05-30 02:14:14 +00:00
level = newLevel
}
// DisableDates disables the date/time prefix
func DisableDates() {
log.SetFlags(0)
}
2022-06-01 20:57:35 +00:00
// ToLevel converts a string to a Level. It returns InfoLevel if the string
// does not match any known log levels.
2022-05-30 02:14:14 +00:00
func ToLevel(s string) Level {
2022-06-02 03:24:44 +00:00
switch strings.ToUpper(s) {
case "TRACE":
return TraceLevel
case "DEBUG":
2022-05-30 02:14:14 +00:00
return DebugLevel
2022-06-02 03:24:44 +00:00
case "INFO":
2022-05-30 02:14:14 +00:00
return InfoLevel
2022-06-02 03:24:44 +00:00
case "WARN", "WARNING":
2022-05-30 02:14:14 +00:00
return WarnLevel
2022-06-02 03:24:44 +00:00
case "ERROR":
2022-05-30 02:14:14 +00:00
return ErrorLevel
default:
2022-06-01 20:57:35 +00:00
return InfoLevel
2022-05-30 02:14:14 +00:00
}
}
2022-06-02 03:24:44 +00:00
// Loggable returns true if the given log level is lower or equal to the current log level
func Loggable(l Level) bool {
return CurrentLevel() <= l
}
// IsTrace returns true if the current log level is TraceLevel
func IsTrace() bool {
return Loggable(TraceLevel)
}
// IsDebug returns true if the current log level is DebugLevel or below
func IsDebug() bool {
return Loggable(DebugLevel)
}
2022-05-30 02:14:14 +00:00
func logIf(l Level, message string, v ...interface{}) {
2022-06-01 20:57:35 +00:00
if CurrentLevel() <= l {
2022-05-30 02:14:14 +00:00
log.Printf(l.String()+" "+message, v...)
}
}