Skip to content
Snippets Groups Projects
Select Git revision
  • d605c9375a6ae299c1fe0d1380d46db71e6b02ce
  • master default protected
  • feature/PRXS-3383-CollectionsRankSortAPI
  • fix/PRXS-3401-ValidateValidationOpts
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • feature/PRXS-3218-HideTemplateActions
  • feature/PRXS-3234-PruneIdents
  • feature/3146-UpdateItemStorageInterface
  • feature/3274-ObjectIndexesFixes
  • feature/PRXS-3143-3235-ReferenceOptions
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

log.go

Blame
  • user avatar
    ko_oler authored
    d605c937
    History
    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
    }