Skip to content
Snippets Groups Projects
Select Git revision
  • 8217ecade7690c82e41b1db5aa8a7ffd89dc278d
  • 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

field.go

Blame
  • 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,
    	}
    }