loggeterrapi

package
v1.124.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2025 License: MIT Imports: 5 Imported by: 1

README

Some notes:

    Should we use NSTIC() or SpanBegin()/SpanEnd()?

        If your subject is potentially creating parallel processes, or you're sure it does,
        or it's some third-party lib and you're unsure what it does underneath - then the
        NSTIC() would be the choice.

        If you want to make a sub-span in the code under your control, then use SpanBegin()/SpanEnd().

        The Span*() methods modify existing tracer, the same one. This is not intended for concurrent use.
        The NSTIC() creates new tracer, which can be freely used concurrently.

    If we have a recursive algorithm, should we make sub-span or NSTIC inside the called subroutines,
    or just outside?

        Both options are okay, but it's important to stick with one, and not mix them, otherwise you'll
        get a decent portion of mess in your tagging. The recommended way is to make sub-spans and NSTICs
        on the calling side of the subroutines, the practice showed this is better. Because? Take nutz/up
        as an example, if you make a sub in the callee, then you have to do that in every user-defined
        method WindUp(), and if the user forgets that - you tagging gets broken. If you do this on the
        calling side, a user may forget to make a sub at the root, but otherwise the risks of bad tagging
        are minimized.

        The exception is constructors with explicitly specified object name - in that case it looks okay
        if it makes a sub tracer on its own.

    JUST REMEMBER:

        One tracer - one thread.

        NSTIC() creates another tracer. SpanBegin() doesn not.

===

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IEventInHook

type IEventInHook interface {
	GetForwardtrace() *trace.Forwardtrace
	GetUserObject() (uo any)
}

type ILogEvent

type ILogEvent interface {
	GetForwardtrace() *trace.Forwardtrace // doesn't increment ETC, used in callbacks
	GetUserObject() (uo any)

	// Inactivation applies to real events only, all data set ops will be bypassed.
	// However, if applied to sub-logger init chain, nothing is bypassed.
	Inactive() ILogEvent

	// This is useful to do some work only if the event is activated, e.g. do some data prep
	// or JSON marshalling, and waste no time otherwise.
	//
	//	     logger.LogEvent("1212").Inactive().IfActive(func(ev loggerapi.IEvent) {
	//				// do some heavy work, then
	//				ev.RawJSON(b)
	//			}).Title("my event").Send()
	IfActive(func(ILogEvent)) ILogEvent

	Tag(msgtag string) ILogEvent // adds extra tag to forward trace of event // max length is 21

	Caller(skip ...int) ILogEvent
	Str(string, string) ILogEvent
	Strf(string, string, ...any) ILogEvent
	Strs(string, []string) ILogEvent
	Time(string, time.Time) ILogEvent
	Int(string, int) ILogEvent
	Ints(string, []int) ILogEvent
	Float64(string, float64) ILogEvent
	Floats64(string, []float64) ILogEvent
	Array(k string, v *zerolog.Array) ILogEvent
	Dict(k string, v *zerolog.Event) ILogEvent
	RawStruct(string, []byte) ILogEvent // sets data as is, the structural correctness is responsibility of user
	Bytes(string, []byte) ILogEvent     // sets data as valid literal

	// It's like Msg(), but sets the value to "title" field, and also
	// can be used to report to metrics. Must be low-cardinality string.
	// DO NOT put in it any variable strings, e.g. Sprintf()-formatted or
	// containing requestId or any other Id or counters!
	// Can't be applied for sub-logger chains, only for events.
	// In typical idiom, must be used BEFORE the Msgtag() - it's specififcally
	// designed to not bypass in inactive events.
	Title(string) ILogEvent

	AddControlflowEvent(key string, event *controlflow.Event) ILogEvent

	SendMsgf(string, ...any)
	SendMsg(...any)
	Send()
}

type ISubTracerInitChain

type ISubTracerInitChain interface {

	// Attach any user object to the event object, to be used later
	// inside the callback hooks. Intended to pass user-level runtime data to the
	// hook(s). Doesn't add anything to the logline serializations.
	// Normally, the UO must be a pointer, and at the receiving side in the hook
	// you do something along the lines:
	//
	//	myObject, _ := e.GetUserObject().(*MyObject)
	//	if myObject !=  nil {
	//		...
	//	}
	SetUserObject(uo any) ISubTracerInitChain

	// Can be used to use custom/external trace
	SetForwardtrace(ft *trace.Forwardtrace) ISubTracerInitChain

	// Inactivation applies to real events only, all data set ops will be bypassed.
	// However, if applied to sub-logger init chain, nothing is bypassed.
	Inactive() ISubTracerInitChain

	Caller(skip ...int) ISubTracerInitChain
	Str(string, string) ISubTracerInitChain
	Strf(string, string, ...any) ISubTracerInitChain
	Strs(string, []string) ISubTracerInitChain
	Time(string, time.Time) ISubTracerInitChain
	Int(string, int) ISubTracerInitChain
	Ints(string, []int) ISubTracerInitChain
	Float64(string, float64) ISubTracerInitChain
	Floats64(string, []float64) ISubTracerInitChain
	Array(k string, v *zerolog.Array) ISubTracerInitChain
	Dict(k string, v *zerolog.Event) ISubTracerInitChain
	RawStruct(string, []byte) ISubTracerInitChain // sets data as is, the structural correctness is responsibility of user
	Bytes(string, []byte) ISubTracerInitChain     // sets data as valid literal

	WithClonedRootContext(optFuncs ...func(rctx *RootContext)) ISubTracerInitChain
	ITracer() ITracer // (!) Each ILogger must be used in single thread, otherwise you'll get inconsistent tagging
}

type ITracer

type ITracer interface {
	GetForwardtrace() *trace.Forwardtrace // also increments ETC
	GetUserObject() (uo any)

	EventEvent(msgtag string, e any) ILogEvent // can accept error, *controlflow.Event, *trace.BacktracePoint // max tag length is 21
	LogEvent(msgtag string) ILogEvent          // pure log event, without legacy semantic level // max tag length is 21

	// Creates new sub-logger, suitable for another thread.
	// By default, all new sub-loggers are active, even if parent was inactivated.
	NewSubTracerInitChain(msgtag string) ISubTracerInitChain // max tag length is 32
	NSTIC(msgtag string) ISubTracerInitChain                 // synonym for NewSubLoggerInitChain()

	// Faster single-thread-only alternative to creating a sub-logger; avoids unnecessary
	// allocations, and calls hooks. Doesn't create another logger, modifies existing one.
	SpanBegin(msgtag string) ITracer // max tag length is 21
	SpanEnd()                        // counterpart for SpanBegin()

	Reset() // resets the tracer's anti-concurrency protection, if previous event wasn't sent

	// example use: tracer.RaiseCause(trace.BacktracePointf("123123", "the reason"))
	RaiseCause(first_backtrace_point *trace.BacktracePoint)
	NewControlflowEvent(first_backtrace_point *trace.BacktracePoint) *controlflow.Event
}

type RootContext

type RootContext struct {
	MainWriter          io.Writer
	ForwardtraceTagsKey string
	ForwardtraceETCKey  string // event tracing counters
	MessageKey          string
	IfSendHook          func(e IEventInHook) (doSend bool)
	EventOnWireHook     func(e IEventInHook, p []byte)
	ActivationHook      func(e IEventInHook) (inactivate bool) // If the ActicationHook is set, it is called, and it can reactivate the event.
	NewSubLoggerHook    func(ft *trace.Forwardtrace)
	SpanBeginHook       func(ft *trace.Forwardtrace)
	SpanEndHook         func(ft *trace.Forwardtrace)
	ZeroTime            time.Time
}

func (*RootContext) Clone

func (rctx0 *RootContext) Clone() *RootContext

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL