Continued logging work
This commit is contained in:
parent
27bd79febf
commit
7cc8c81bd8
28 changed files with 287 additions and 171 deletions
45
log/event.go
45
log/event.go
|
@ -15,74 +15,95 @@ const (
|
|||
errorField = "error"
|
||||
)
|
||||
|
||||
// Event represents a single log event
|
||||
type Event struct {
|
||||
Time int64 `json:"time"`
|
||||
Level Level `json:"level"`
|
||||
Message string `json:"message"`
|
||||
fields map[string]any
|
||||
Timestamp int64 `json:"time"`
|
||||
Level Level `json:"level"`
|
||||
Message string `json:"message"`
|
||||
fields Context
|
||||
}
|
||||
|
||||
// newEvent creates a new log event
|
||||
func newEvent() *Event {
|
||||
return &Event{
|
||||
Time: time.Now().UnixMilli(),
|
||||
fields: make(map[string]any),
|
||||
Timestamp: time.Now().UnixMilli(),
|
||||
fields: make(Context),
|
||||
}
|
||||
}
|
||||
|
||||
// Fatal logs the event as FATAL, and exits the program with exit code 1
|
||||
func (e *Event) Fatal(message string, v ...any) {
|
||||
e.Log(FatalLevel, message, v...)
|
||||
e.Field("exit_code", 1).Log(FatalLevel, message, v...)
|
||||
fmt.Fprintf(os.Stderr, fmt.Sprintf(message+"\n", v...)) // Always output error to stderr
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Error logs the event with log level error
|
||||
func (e *Event) Error(message string, v ...any) {
|
||||
e.Log(ErrorLevel, message, v...)
|
||||
}
|
||||
|
||||
// Warn logs the event with log level warn
|
||||
func (e *Event) Warn(message string, v ...any) {
|
||||
e.Log(WarnLevel, message, v...)
|
||||
}
|
||||
|
||||
// Info logs the event with log level info
|
||||
func (e *Event) Info(message string, v ...any) {
|
||||
e.Log(InfoLevel, message, v...)
|
||||
}
|
||||
|
||||
// Debug logs the event with log level debug
|
||||
func (e *Event) Debug(message string, v ...any) {
|
||||
e.Log(DebugLevel, message, v...)
|
||||
}
|
||||
|
||||
// Trace logs the event with log level trace
|
||||
func (e *Event) Trace(message string, v ...any) {
|
||||
e.Log(TraceLevel, message, v...)
|
||||
}
|
||||
|
||||
// Tag adds a "tag" field to the log event
|
||||
func (e *Event) Tag(tag string) *Event {
|
||||
e.fields[tagField] = tag
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Event) Err(err error) *Event {
|
||||
e.fields[errorField] = err
|
||||
// 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
|
||||
func (e *Event) Err(err error) *Event {
|
||||
e.fields[errorField] = err.Error()
|
||||
return e
|
||||
}
|
||||
|
||||
// Field adds a custom field and value to the log event
|
||||
func (e *Event) Field(key string, value any) *Event {
|
||||
e.fields[key] = value
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Event) Fields(fields map[string]any) *Event {
|
||||
// Fields adds a map of fields to the log event
|
||||
func (e *Event) Fields(fields Context) *Event {
|
||||
for k, v := range fields {
|
||||
e.fields[k] = v
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Event) Context(contexts ...Contexter) *Event {
|
||||
// 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 {
|
||||
for _, c := range contexts {
|
||||
e.Fields(c.Context())
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// Log logs a message with the given log level
|
||||
func (e *Event) Log(l Level, message string, v ...any) {
|
||||
e.Message = fmt.Sprintf(message, v...)
|
||||
e.Level = l
|
||||
|
@ -110,6 +131,7 @@ func (e *Event) IsDebug() bool {
|
|||
return e.Loggable(DebugLevel)
|
||||
}
|
||||
|
||||
// JSON returns the event as a JSON representation
|
||||
func (e *Event) JSON() string {
|
||||
b, _ := json.Marshal(e)
|
||||
s := string(b)
|
||||
|
@ -120,6 +142,7 @@ func (e *Event) JSON() string {
|
|||
return s
|
||||
}
|
||||
|
||||
// String returns the event as a string
|
||||
func (e *Event) String() string {
|
||||
if len(e.fields) == 0 {
|
||||
return fmt.Sprintf("%s %s", e.Level.String(), e.Message)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue