Select Git revision
log.go 3.59 KiB
package log
import (
"maps"
"time"
"git.perx.ru/perxis/perxis-go/id"
pb "git.perx.ru/perxis/perxis-go/proto/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
type Level int
const (
Info = Level(pb.LogLevel_INFO)
Warning = Level(pb.LogLevel_WARNING)
Error = Level(pb.LogLevel_ERROR)
Critical = Level(pb.LogLevel_CRITICAL)
Fatal = Level(pb.LogLevel_FATAL)
)
func (l Level) String() string {
s := pb.LogLevel_name[int32(l)]
if s == "" {
s = "UNKNOWN"
}
return s
}
type Entry struct {
ID string `json:"id" bson:"id" mapstructure:"id"`
Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp,omitempty" mapstructure:"timestamp,omitempty"`
LogLevel Level `json:"log_level,omitempty" bson:"log_level,omitempty" mapstructure:"log_level,omitempty"`
Message string `json:"message,omitempty" bson:"message,omitempty" mapstructure:"message,omitempty"`
Category string `json:"category,omitempty" bson:"category,omitempty" mapstructure:"category,omitempty"`
Component string `json:"component,omitempty" bson:"component,omitempty" mapstructure:"component,omitempty"`
Event string `json:"event,omitempty" bson:"event,omitempty" mapstructure:"event,omitempty"`
ObjectID *id.ID `json:"object_id,omitempty" bson:"object_id,omitempty" mapstructure:"object_id,omitempty"`
CallerID *id.ID `json:"caller_id,omitempty" bson:"caller_id,omitempty" mapstructure:"caller_id,omitempty"`
Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty" mapstructure:"attr,omitempty"`
Tags []string `json:"tags,omitempty" bson:"tags,omitempty" mapstructure:"tags,omitempty"`
}
//func convertInterfaceToAny(v interface{}) (*any.Any, error) {
// anyValue := &any.Any{}
// bytes, _ := json.Marshal(v)
// bytesValue := &wrappers.BytesValue{
// Value: bytes,
// }
// err := anypb.MarshalFrom(anyValue, bytesValue, proto.MarshalOptions{})
// return anyValue, err
//}
func EntryToPB(entry *Entry) *pb.LogEntry {
logEntry := &pb.LogEntry{
Id: entry.ID,
Timestamp: timestamppb.New(entry.Timestamp),
Level: pb.LogLevel(entry.LogLevel),
Message: entry.Message,
Category: entry.Category,
Component: entry.Component,
Event: entry.Event,
Attr: nil, //implement
Tags: entry.Tags,
}
if entry.ObjectID != nil {
logEntry.ObjectId = entry.ObjectID.String()
}
if entry.CallerID != nil {
logEntry.CallerId = entry.CallerID.String()
}
return logEntry
}
func EntryFromPB(request *pb.LogEntry) *Entry {
logEntry := &Entry{
ID: request.Id,
Timestamp: request.Timestamp.AsTime(),
LogLevel: Level(request.Level),
Message: request.Message,
Category: request.Category,
Component: request.Component,
Event: request.Event,
}
if request.ObjectId != "" {
logEntry.ObjectID, _ = id.Parse(request.ObjectId)
}
if request.CallerId != "" {
logEntry.CallerID, _ = id.Parse(request.CallerId)
}
if request.Attr != nil {
logEntry.Attr = request.Attr // todo: как с этим работать?
}
if request.Tags != nil {
logEntry.Tags = request.Tags
}
return logEntry
}
func (e *Entry) ToMap() map[string]any {
res := map[string]any{
"id": e.ID,
"timestamp": e.Timestamp,
"log_level": e.LogLevel,
"message": e.Message,
"category": e.Category,
"component": e.Component,
"event": e.Event,
}
if e.ObjectID != nil {
res["object_id"] = e.ObjectID.String()
maps.Copy(res, e.ObjectID.ToMap())
}
if e.CallerID != nil {
res["caller_id"] = e.CallerID.String()
}
if e.Attr != nil {
res["attr"] = e.Attr
}
if e.Tags != nil {
res["tags"] = e.Tags
}
return res
}