2022-05-30 02:14:14 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2023-02-06 04:34:27 +00:00
|
|
|
"io"
|
2022-05-30 02:14:14 +00:00
|
|
|
"log"
|
2023-02-06 04:34:27 +00:00
|
|
|
"os"
|
2022-06-01 20:57:35 +00:00
|
|
|
"sync"
|
2023-02-06 04:34:27 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-02-06 04:53:24 +00:00
|
|
|
// Defaults for package level variables
|
|
|
|
var (
|
2023-02-06 04:34:27 +00:00
|
|
|
DefaultLevel = InfoLevel
|
|
|
|
DefaultFormat = TextFormat
|
2023-02-06 04:53:24 +00:00
|
|
|
DefaultOutput = os.Stderr
|
2022-05-30 02:14:14 +00:00
|
|
|
)
|
|
|
|
|
2023-02-04 03:21:50 +00:00
|
|
|
var (
|
2023-02-06 04:53:24 +00:00
|
|
|
level = DefaultLevel
|
|
|
|
format = DefaultFormat
|
|
|
|
overrides = make(map[string]*levelOverride)
|
|
|
|
output io.Writer = DefaultOutput
|
|
|
|
mu = &sync.Mutex{}
|
2023-02-06 04:34:27 +00:00
|
|
|
)
|
|
|
|
|
2023-02-04 03:21:50 +00:00
|
|
|
// Fatal prints the given message, and exits the program
|
|
|
|
func Fatal(message string, v ...any) {
|
|
|
|
newEvent().Fatal(message, v...)
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 03:21:50 +00:00
|
|
|
// Error prints the given message, if the current log level is ERROR or lower
|
|
|
|
func Error(message string, v ...any) {
|
|
|
|
newEvent().Error(message, v...)
|
|
|
|
}
|
2022-05-30 02:14:14 +00:00
|
|
|
|
2023-02-04 03:21:50 +00:00
|
|
|
// Warn prints the given message, if the current log level is WARN or lower
|
|
|
|
func Warn(message string, v ...any) {
|
|
|
|
newEvent().Warn(message, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info prints the given message, if the current log level is INFO or lower
|
|
|
|
func Info(message string, v ...any) {
|
|
|
|
newEvent().Info(message, v...)
|
2022-06-02 03:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debug prints the given message, if the current log level is DEBUG or lower
|
2022-10-01 20:31:48 +00:00
|
|
|
func Debug(message string, v ...any) {
|
2023-02-04 03:21:50 +00:00
|
|
|
newEvent().Debug(message, v...)
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 03:21:50 +00:00
|
|
|
// Trace prints the given message, if the current log level is TRACE
|
|
|
|
func Trace(message string, v ...any) {
|
|
|
|
newEvent().Trace(message, v...)
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
// With creates a new log event and adds the fields of the given Contexter structs
|
|
|
|
func With(contexts ...Contexter) *Event {
|
|
|
|
return newEvent().With(contexts...)
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
// Field creates a new log event and adds a custom field and value to it
|
2023-02-04 03:21:50 +00:00
|
|
|
func Field(key string, value any) *Event {
|
|
|
|
return newEvent().Field(key, value)
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
// Fields creates a new log event and adds a map of fields to it
|
|
|
|
func Fields(fields Context) *Event {
|
2023-02-04 03:21:50 +00:00
|
|
|
return newEvent().Fields(fields)
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
// Tag creates a new log event and adds a "tag" field to it
|
2023-02-04 03:21:50 +00:00
|
|
|
func Tag(tag string) *Event {
|
|
|
|
return newEvent().Tag(tag)
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
// Time creates a new log event and sets the time field
|
|
|
|
func Time(time time.Time) *Event {
|
|
|
|
return newEvent().Time(time)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-04 03:21:50 +00:00
|
|
|
// SetLevelOverride adds a log override for the given field
|
|
|
|
func SetLevelOverride(field string, value any, level Level) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
overrides[field] = &levelOverride{value: value, level: level}
|
2022-06-02 14:50:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
// ResetLevelOverrides removes all log level overrides
|
|
|
|
func ResetLevelOverrides() {
|
2023-02-04 03:21:50 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
overrides = make(map[string]*levelOverride)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentFormat returns the current log formt
|
|
|
|
func CurrentFormat() Format {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
return format
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFormat sets a new log format
|
|
|
|
func SetFormat(newFormat Format) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
format = newFormat
|
|
|
|
if newFormat == JSONFormat {
|
|
|
|
DisableDates()
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
// SetOutput sets the log output writer
|
|
|
|
func SetOutput(w io.Writer) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
log.SetOutput(w)
|
|
|
|
output = w
|
|
|
|
}
|
|
|
|
|
|
|
|
// File returns the log file, if any, or an empty string otherwise
|
|
|
|
func File() string {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if f, ok := output.(*os.File); ok {
|
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsFile returns true if the output is a non-default file
|
|
|
|
func IsFile() bool {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if _, ok := output.(*os.File); ok && output != DefaultOutput {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-04 03:21:50 +00:00
|
|
|
// DisableDates disables the date/time prefix
|
|
|
|
func DisableDates() {
|
|
|
|
log.SetFlags(0)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|