log/entry.go

85 lines
1.8 KiB
Go

// Copyright 2023 Martin Riedl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package golog
import (
"fmt"
"runtime"
"time"
)
type Entry struct {
Logger *Logger
Content []Content
Message string
Level Level
Time time.Time
CallStack []runtime.Frame
}
type Content struct {
Key string `json:"key"`
Value any `json:"value"`
}
func NewEntry(logger *Logger) *Entry {
return &Entry{
Logger: logger,
Content: make([]Content, 0),
}
}
func (entry *Entry) With(key string, value any) *Entry {
entry.Content = append(entry.Content, Content{
Key: key,
Value: value,
})
return entry
}
func (entry *Entry) WithContent(content []Content) *Entry {
entry.Content = append(entry.Content, content...)
return entry
}
func (entry *Entry) Log(level Level, args ...any) {
entry.Message = fmt.Sprint(args...)
entry.Level = level
entry.Logger.LogEntry(entry)
}
func (entry *Entry) Fatal(args ...any) {
entry.Log(LevelFatal, args...)
}
func (entry *Entry) Error(args ...any) {
entry.Log(LevelError, args...)
}
func (entry *Entry) Warning(args ...any) {
entry.Log(LevelWarning, args...)
}
func (entry *Entry) Info(args ...any) {
entry.Log(LevelInfo, args...)
}
func (entry *Entry) Debug(args ...any) {
entry.Log(LevelDebug, args...)
}
func (entry *Entry) Trace(args ...any) {
entry.Log(LevelTrace, args...)
}