|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// Project active2
|
|
|
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
|
|
|
//
|
|
|
|
|
// File name entry.go
|
|
|
|
|
// Created by DEV
|
|
|
|
|
// Modified 23/04/2024
|
|
|
|
|
//
|
|
|
|
|
// Copyright 2024 activething.com
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
package alogs
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"apacks"
|
|
|
|
|
"atypes/slices"
|
|
|
|
|
"time"
|
|
|
|
|
"vals/flds"
|
|
|
|
|
"vals/flds/pkflds"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Entry struct {
|
|
|
|
|
Logger Logger
|
|
|
|
|
|
|
|
|
|
Id uint64
|
|
|
|
|
Scope string
|
|
|
|
|
Scopes [][]byte
|
|
|
|
|
Level Level
|
|
|
|
|
|
|
|
|
|
Message string
|
|
|
|
|
Date time.Time
|
|
|
|
|
Error error
|
|
|
|
|
Data any
|
|
|
|
|
|
|
|
|
|
Fields flds.AFields
|
|
|
|
|
Excludes slices.Strings
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EntryFnc func (entry *Entry)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func AcquireEntry (logger Logger, level Level, message string, err error, options ...EntryFnc) *Entry {
|
|
|
|
|
return g.entryPool.Get().init(logger,level,message,err,options) }
|
|
|
|
|
|
|
|
|
|
func ReleaseEntry (entry *Entry) {
|
|
|
|
|
g.entryPool.Put(entry.reset()) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewEntry (logger Logger, level Level, message string, err error, options ...EntryFnc) *Entry {
|
|
|
|
|
return newEntry().init(logger,level,message,err,options) }
|
|
|
|
|
|
|
|
|
|
func newEntry () *Entry {
|
|
|
|
|
return &Entry{} }
|
|
|
|
|
|
|
|
|
|
func (e *Entry) init (logger Logger, level Level, message string, err error, options []EntryFnc) *Entry {
|
|
|
|
|
e.Id = g.idGen.Inc()
|
|
|
|
|
e.Logger = logger
|
|
|
|
|
e.Level = level
|
|
|
|
|
e.Error = err
|
|
|
|
|
e.Message= message
|
|
|
|
|
e.Date = time.Now()
|
|
|
|
|
return applyEntryOptions(e,options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (e *Entry) reset () *Entry {
|
|
|
|
|
if e == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
e.Logger = nil
|
|
|
|
|
e.Excludes = e.Excludes.Reset()
|
|
|
|
|
for _,s := range e.Scopes {
|
|
|
|
|
s = s[:0]
|
|
|
|
|
}
|
|
|
|
|
e.Scopes = e.Scopes[:0]
|
|
|
|
|
e.Error = nil
|
|
|
|
|
e.Data = nil
|
|
|
|
|
e.Message= ""
|
|
|
|
|
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (e *Entry) BuildPack(packer apacks.ValuePacker) error {
|
|
|
|
|
packer.PackObject(func (b apacks.ObjectPackBuilder, _ apacks.Flags){
|
|
|
|
|
b.FieldUInt64("id",e.Id,0)
|
|
|
|
|
b.FieldDate("date",e.Date,0)
|
|
|
|
|
pkflds.AddAFieldsToPack(b,e.Fields)
|
|
|
|
|
},0)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func EntryWithData (data any, options ...EntryFnc) EntryFnc {
|
|
|
|
|
return func(e *Entry) {
|
|
|
|
|
e.Data = data
|
|
|
|
|
applyEntryOptions(e,options)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func EntryWithExclude (exclude ... string) EntryFnc {
|
|
|
|
|
return func(e *Entry) {
|
|
|
|
|
for _, ex := range exclude {
|
|
|
|
|
if len(ex) > 0 {
|
|
|
|
|
e.Excludes = e.Excludes.Append(ex)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func EntryWithExtender(extender EntryExtender) EntryFnc {
|
|
|
|
|
return func(e *Entry) {
|
|
|
|
|
extender.ExtendEntry(e) } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func EntryWithField (name string, value any) EntryFnc {
|
|
|
|
|
return func (e *Entry) {
|
|
|
|
|
e.Fields = e.Fields.SetField(flds.NewAField(name,value)) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func EntryWithFields (field ...flds.AField) EntryFnc {
|
|
|
|
|
return func(e *Entry) {
|
|
|
|
|
for _,f := range field {
|
|
|
|
|
e.Fields = e.Fields.SetField(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func applyEntryOptions (entry *Entry, options []EntryFnc) *Entry {
|
|
|
|
|
if len(options) > 0 {
|
|
|
|
|
for _,o := range options {
|
|
|
|
|
o(entry)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return entry
|
|
|
|
|
}
|