Select Git revision
log.go 2.77 KiB
package log
import (
"time"
pb "git.perx.ru/perxis/perxis-go/proto/log"
"github.com/mitchellh/mapstructure"
"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"`
LogLevel Level `json:"log_level,omitempty" bson:"log_level,omitempty" mapstructure:"log_level"`
Message string `json:"message,omitempty" bson:"message,omitempty" mapstructure:"message"`
Category string `json:"category,omitempty" bson:"category,omitempty" mapstructure:"category"`
Component string `json:"component,omitempty" bson:"component,omitempty" mapstructure:"component"`
Event string `json:"event,omitempty" bson:"event,omitempty" mapstructure:"event"`
Object string `json:"object,omitempty" bson:"object,omitempty" mapstructure:"object"`
Caller string `json:"caller,omitempty" bson:"caller,omitempty" mapstructure:"caller"`
Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty" mapstructure:"attr"`
Tags []string `json:"tags,omitempty" bson:"tags,omitempty" mapstructure:"tags"`
}
//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,
Object: entry.Object,
Caller: entry.Caller,
Attr: nil, //implement
Tags: entry.Tags,
}
return logEntry
}
func EntryFromPB(request *pb.LogEntry) *Entry {
return &Entry{
ID: request.Id,
Timestamp: request.Timestamp.AsTime(),
LogLevel: Level(request.Level),
Message: request.Message,
Category: request.Category,
Component: request.Component,
Event: request.Event,
Object: request.Object,
Caller: request.Caller,
Attr: request.Attr, // todo: как с этим работать?
Tags: request.Tags,
}
}
func (e *Entry) ToMap() map[string]string {
res := make(map[string]string)
mapstructure.Decode(e, &res)
return res
}