Select Git revision
manager.proto
log.go 2.58 KiB
package log
import (
"encoding/json"
"time"
pb "git.perx.ru/perxis/perxis-go/proto/log"
"github.com/golang/protobuf/ptypes/any"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
wrappers "google.golang.org/protobuf/types/known/wrapperspb"
)
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"`
Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp,omitempty"`
LogLevel Level `json:"log_level,omitempty" bson:"log_level,omitempty"`
Message string `json:"message,omitempty" bson:"message,omitempty"`
Category string `json:"category,omitempty" bson:"category,omitempty"`
Component string `json:"component,omitempty" bson:"component,omitempty"`
Event string `json:"event,omitempty" bson:"event,omitempty"`
Object string `json:"object,omitempty" bson:"object,omitempty"`
Caller string `json:"caller,omitempty" bson:"caller,omitempty"`
Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty"`
Tags []string `json:"tags,omitempty" bson:"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,
Object: entry.Object,
Caller: entry.Caller,
Tags: entry.Tags,
}
logEntry.Attr, _ = convertInterfaceToAny(entry.Attr)
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,
}
}