Documentation
¶
Overview ¶
Package logcore is the framework-agnostic logging engine behind the logfiber (Fiber v2) and logfiberv3 (Fiber v3) adapters.
It builds a zap logger pre-wired for APM:
- Error/Fatal log calls are auto-emitted as APM error events (via apmcore.WrapZapCore on top of apmzap.Core).
- logcore.LogCtx(ctx) returns a logger decorated with trace.id / transaction.id / span.id when ctx has an active APM transaction.
The package keeps a process-global logger (logcore.Log / Logy / LogCtx) so handlers can call it without plumbing — and exposes logcore.New + SetGlobal for tests and apps that want to inject their own.
It also exports HTTPClientHook(), an adapter that plugs straight into httpclient.SetHook(...) and produces the same "outgoing" structured-log schema the Fiber incoming middlewares use, so requests are searchable in Kibana by req/res/responseTime regardless of direction.
Index ¶
- func DecodeJSONBody(raw []byte) any
- func FlattenHeaders(h map[string][]string) any
- func HTTPClientHook() httpclient.Hook
- func HookFor(l *Logger) httpclient.Hook
- func Log() *zap.Logger
- func LogCtx(ctx context.Context) *zap.Logger
- func Logy() *zap.SugaredLogger
- func SetGlobal(l *Logger)
- type Incoming
- type Logger
- type Options
- type Outgoing
- type Req
- type Res
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSONBody ¶
DecodeJSONBody attempts to unmarshal raw as JSON. On failure (non-JSON payload, empty, malformed) it returns the raw bytes as a string so the log entry still carries something useful. nil bytes → nil.
func FlattenHeaders ¶
FlattenHeaders converts a map[string][]string headers shape into a flat map[string]string by joining multi-valued entries with ",". Returns nil when the input is empty so the JSON encoder drops it.
func HTTPClientHook ¶
func HTTPClientHook() httpclient.Hook
HTTPClientHook returns a httpclient.Hook that emits one structured log per attempt with the same shape the Fiber middlewares produce for incoming requests — Kibana queries like `outgoing.req.body.id` match regardless of direction.
Use it during bootstrap:
httpclient.SetHook(logcore.HTTPClientHook())
The log line uses the global logger decorated with apmcore trace fields (via LogCtx). For a custom logger, see HookFor.
func HookFor ¶
func HookFor(l *Logger) httpclient.Hook
HookFor returns the same hook bound to l. Nil falls back to the global logger.
func Log ¶
Log returns the global logger, lazily constructing a production one the first time it's called. Safe for concurrent use.
Types ¶
type Incoming ¶
type Incoming struct {
Req *Req `json:"req"`
Res *Res `json:"res"`
ResponseTime string `json:"responseTime"`
}
Incoming is the payload published under the "incoming" key by the Fiber middleware.
type Logger ¶
Logger wraps *zap.Logger so the package can attach helpers without shadowing zap's API.
func New ¶
New constructs a Logger configured per opts. The returned Logger is independent — call SetGlobal to make it the process-wide default.
func (*Logger) LogCtx ¶
LogCtx returns a child logger decorated with the APM trace fields pulled from ctx — useful for any handler/service log call so the resulting line jumps back to its trace in Kibana.
func (*Logger) Sugar ¶
func (l *Logger) Sugar() *zap.SugaredLogger
Sugar returns the sugared variant of the underlying logger.
type Options ¶
type Options struct {
// Level is the minimum log level. Default: InfoLevel.
Level zapcore.Level
// Encoding selects the zap encoder. "json" (default) or "console".
Encoding string
// DisableAPMCore turns off the apmzap.Core wrap. By default the
// logger's core is wrapped so .Error/.Fatal calls are auto-emitted
// as APM error events in Kibana → APM → Errors. Set to true to
// disable (e.g. in tests where you don't want APM noise).
DisableAPMCore bool
// Service / Version / Environment are added as permanent fields on
// every log line — useful as Kibana filters when many services
// share an index.
Service string
Version string
Environment string
// Extra is appended to the zap.New options list. Use it for hooks,
// custom AddCallerSkip, ReplaceCore, etc.
Extra []zap.Option
}
Options tunes New. All fields are optional; sensible production defaults are applied when zero.
type Outgoing ¶
type Outgoing struct {
Req *Req `json:"req"`
Res *Res `json:"res"`
Error *string `json:"error,omitempty"`
ResponseTime string `json:"responseTime"`
}
Outgoing is the payload published under the "outgoing" key by the httpclient hook.
type Req ¶
type Req struct {
Params any `json:"params,omitempty"`
QueryString any `json:"queryString,omitempty"`
Headers any `json:"headers,omitempty"`
Body any `json:"body,omitempty"`
}
Req is the request side of an incoming or outgoing log entry. Fields stay omitempty so the JSON in Kibana is compact.