ntfy/log/types.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
2.0 KiB
Go
Raw Normal View History

2023-02-04 06:21:50 +03:00
package log
import (
"encoding/json"
"strings"
)
// Level is a well-known log level, as defined below
type Level int
// Well known log levels
const (
TraceLevel Level = iota
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
)
func (l Level) String() string {
switch l {
case TraceLevel:
return "TRACE"
case DebugLevel:
return "DEBUG"
case InfoLevel:
return "INFO"
case WarnLevel:
return "WARN"
case ErrorLevel:
return "ERROR"
case FatalLevel:
return "FATAL"
}
return "unknown"
}
2023-02-06 07:53:24 +03:00
// MarshalJSON converts a level to a JSON string
2023-02-04 06:21:50 +03:00
func (l Level) MarshalJSON() ([]byte, error) {
return json.Marshal(l.String())
}
// ToLevel converts a string to a Level. It returns InfoLevel if the string
// does not match any known log levels.
func ToLevel(s string) Level {
switch strings.ToUpper(s) {
case "TRACE":
return TraceLevel
case "DEBUG":
return DebugLevel
case "INFO":
return InfoLevel
case "WARN", "WARNING":
return WarnLevel
case "ERROR":
return ErrorLevel
2023-02-09 06:57:10 +03:00
case "FATAL":
return FatalLevel
2023-02-04 06:21:50 +03:00
default:
return InfoLevel
}
}
// Format is a well-known log format
type Format int
// Log formats
const (
TextFormat Format = iota
JSONFormat
)
func (f Format) String() string {
switch f {
case TextFormat:
return "text"
case JSONFormat:
return "json"
}
return "unknown"
}
// ToFormat converts a string to a Format. It returns TextFormat if the string
// does not match any known log formats.
func ToFormat(s string) Format {
switch strings.ToLower(s) {
case "text":
return TextFormat
case "json":
return JSONFormat
default:
return TextFormat
}
}
2023-02-06 07:34:27 +03:00
// Contexter allows structs to export a key-value pairs in the form of a Context
2023-02-05 05:26:01 +03:00
type Contexter interface {
2023-02-06 07:34:27 +03:00
Context() Context
2023-02-04 06:21:50 +03:00
}
2023-02-06 07:34:27 +03:00
// Context represents an object's state in the form of key-value pairs
type Context map[string]any
2023-02-04 06:21:50 +03:00
2023-02-26 04:23:22 +03:00
// Merge merges other into this context
func (c Context) Merge(other Context) {
for k, v := range other {
c[k] = v
}
}
2023-02-04 06:21:50 +03:00
type levelOverride struct {
2023-02-09 06:57:10 +03:00
value string
2023-02-04 06:21:50 +03:00
level Level
}