ntfy/log/log.go

130 lines
2.5 KiB
Go
Raw Normal View History

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