ntfy/log/event.go

178 lines
4.0 KiB
Go
Raw Normal View History

2023-02-04 06:21:50 +03:00
package log
import (
"encoding/json"
"fmt"
"log"
"os"
"sort"
"strings"
"time"
)
const (
tagField = "tag"
errorField = "error"
)
2023-02-06 07:34:27 +03:00
// Event represents a single log event
2023-02-04 06:21:50 +03:00
type Event struct {
2023-02-06 07:34:27 +03:00
Timestamp int64 `json:"time"`
Level Level `json:"level"`
Message string `json:"message"`
fields Context
2023-02-04 06:21:50 +03:00
}
2023-02-06 07:34:27 +03:00
// newEvent creates a new log event
2023-02-04 06:21:50 +03:00
func newEvent() *Event {
return &Event{
2023-02-06 07:34:27 +03:00
Timestamp: time.Now().UnixMilli(),
fields: make(Context),
2023-02-04 06:21:50 +03:00
}
}
2023-02-06 07:34:27 +03:00
// Fatal logs the event as FATAL, and exits the program with exit code 1
2023-02-04 06:21:50 +03:00
func (e *Event) Fatal(message string, v ...any) {
2023-02-06 07:34:27 +03:00
e.Field("exit_code", 1).Log(FatalLevel, message, v...)
2023-02-06 07:53:24 +03:00
fmt.Fprintf(os.Stderr, message+"\n", v...) // Always output error to stderr
2023-02-04 06:21:50 +03:00
os.Exit(1)
}
2023-02-06 07:34:27 +03:00
// Error logs the event with log level error
2023-02-04 06:21:50 +03:00
func (e *Event) Error(message string, v ...any) {
e.Log(ErrorLevel, message, v...)
}
2023-02-06 07:34:27 +03:00
// Warn logs the event with log level warn
2023-02-04 06:21:50 +03:00
func (e *Event) Warn(message string, v ...any) {
e.Log(WarnLevel, message, v...)
}
2023-02-06 07:34:27 +03:00
// Info logs the event with log level info
2023-02-04 06:21:50 +03:00
func (e *Event) Info(message string, v ...any) {
e.Log(InfoLevel, message, v...)
}
2023-02-06 07:34:27 +03:00
// Debug logs the event with log level debug
2023-02-04 06:21:50 +03:00
func (e *Event) Debug(message string, v ...any) {
e.Log(DebugLevel, message, v...)
}
2023-02-06 07:34:27 +03:00
// Trace logs the event with log level trace
2023-02-04 06:21:50 +03:00
func (e *Event) Trace(message string, v ...any) {
e.Log(TraceLevel, message, v...)
}
2023-02-06 07:34:27 +03:00
// Tag adds a "tag" field to the log event
2023-02-04 06:21:50 +03:00
func (e *Event) Tag(tag string) *Event {
e.fields[tagField] = tag
return e
}
2023-02-06 07:34:27 +03:00
// Time sets the time field
func (e *Event) Time(time time.Time) *Event {
e.Timestamp = time.UnixMilli()
return e
}
// Err adds an "error" field to the log event
2023-02-04 06:21:50 +03:00
func (e *Event) Err(err error) *Event {
2023-02-07 00:01:32 +03:00
if c, ok := err.(Contexter); ok {
e.Fields(c.Context())
} else {
e.fields[errorField] = err.Error()
}
2023-02-04 06:21:50 +03:00
return e
}
2023-02-06 07:34:27 +03:00
// Field adds a custom field and value to the log event
2023-02-04 06:21:50 +03:00
func (e *Event) Field(key string, value any) *Event {
e.fields[key] = value
return e
}
2023-02-06 07:34:27 +03:00
// Fields adds a map of fields to the log event
func (e *Event) Fields(fields Context) *Event {
2023-02-04 06:21:50 +03:00
for k, v := range fields {
e.fields[k] = v
}
return e
}
2023-02-06 07:34:27 +03:00
// With adds the fields of the given Contexter structs to the log event by calling their With method
func (e *Event) With(contexts ...Contexter) *Event {
2023-02-04 06:21:50 +03:00
for _, c := range contexts {
e.Fields(c.Context())
}
return e
}
2023-02-06 07:34:27 +03:00
// Log logs a message with the given log level
2023-02-04 06:21:50 +03:00
func (e *Event) Log(l Level, message string, v ...any) {
e.Message = fmt.Sprintf(message, v...)
e.Level = l
if e.shouldPrint() {
if CurrentFormat() == JSONFormat {
log.Println(e.JSON())
} else {
log.Println(e.String())
}
}
}
// Loggable returns true if the given log level is lower or equal to the current log level
func (e *Event) Loggable(l Level) bool {
return e.globalLevelWithOverride() <= l
}
// IsTrace returns true if the current log level is TraceLevel
func (e *Event) IsTrace() bool {
return e.Loggable(TraceLevel)
}
// IsDebug returns true if the current log level is DebugLevel or below
func (e *Event) IsDebug() bool {
return e.Loggable(DebugLevel)
}
2023-02-06 07:34:27 +03:00
// JSON returns the event as a JSON representation
2023-02-04 06:21:50 +03:00
func (e *Event) JSON() string {
b, _ := json.Marshal(e)
s := string(b)
if len(e.fields) > 0 {
b, _ := json.Marshal(e.fields)
s = fmt.Sprintf("{%s,%s}", s[1:len(s)-1], string(b[1:len(b)-1]))
}
return s
}
2023-02-06 07:34:27 +03:00
// String returns the event as a string
2023-02-04 06:21:50 +03:00
func (e *Event) String() string {
if len(e.fields) == 0 {
return fmt.Sprintf("%s %s", e.Level.String(), e.Message)
}
fields := make([]string, 0)
for k, v := range e.fields {
fields = append(fields, fmt.Sprintf("%s=%v", k, v))
}
sort.Strings(fields)
return fmt.Sprintf("%s %s (%s)", e.Level.String(), e.Message, strings.Join(fields, ", "))
}
func (e *Event) shouldPrint() bool {
return e.globalLevelWithOverride() <= e.Level
}
func (e *Event) globalLevelWithOverride() Level {
mu.Lock()
l, ov := level, overrides
mu.Unlock()
for field, override := range ov {
value, exists := e.fields[field]
if exists && value == override.value {
return override.level
}
}
return l
}