logger

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GormLoggerFrom

func GormLoggerFrom(logger kratoslog.Logger, module string) gormlogger.Interface

Types

type GormLogger

type GormLogger struct {
	ZapLogger     *zap.Logger
	SlowThreshold time.Duration
}

GormLogger 操作对象,实现 gormlogger.Interface

func (GormLogger) Error

func (l GormLogger) Error(ctx context.Context, str string, args ...any)

Error 实现 gormlogger.Interface 的 Error 方法

func (GormLogger) Info

func (l GormLogger) Info(ctx context.Context, str string, args ...any)

Info 实现 gormlogger.Interface 的 Info 方法

func (GormLogger) LogMode

LogMode 实现 gormlogger.Interface 的 LogMode 方法

func (GormLogger) Trace

func (l GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

Trace 实现 gormlogger.Interface 的 Trace 方法

func (GormLogger) Warn

func (l GormLogger) Warn(ctx context.Context, str string, args ...any)

Warn 实现 gormlogger.Interface 的 Warn 方法

type Helper

type Helper = log.Helper

func For

func For(l Logger, module string) *Helper

For creates a *Helper scoped to the given module — the one-liner replacement for logger.NewHelper(l, logger.WithModule("x/y/z")).

Before: logger.NewHelper(l, logger.WithModule("user/biz/iam-service"))
After:  logger.For(l, "user/biz/iam")

IMPORTANT: The returned helper is NOT bound to any context. Kratos Valuers (such as the trace_id / span_id valuers registered by bootstrap) are only invoked with the ctx attached to the helper at log-emit time. To make them resolve, callers MUST chain .WithContext(ctx) per call:

r.log.WithContext(ctx).Warnf("query failed: %v", err)

Calling helper methods directly (r.log.Warnf(...)) without .WithContext leaves context-derived fields blank. See ExampleHelper_WithContext.

func NewHelper

func NewHelper(l Logger, opts ...Option) *Helper

NewHelper creates a *Helper with optional Option fields applied. Prefer For() when only a module label is needed.

IMPORTANT: As with For(), callers must chain .WithContext(ctx) at each call site to activate context-aware valuers (trace_id, span_id, etc). See ExampleHelper_WithContext for the canonical pattern.

type Logger

type Logger = log.Logger

Logger and Helper are type aliases for Kratos interfaces, kept for ergonomics.

func With

func With(l Logger, args ...any) Logger

With adds structured fields to a logger. Accepts two styles:

logger.With(l, "component/layer/service")          — module shorthand
logger.With(l, WithModule("x"), WithField("k", v)) — option style

type Option

type Option func(keyvals *[]any)

Option is a function that appends key-value pairs to a logger's fields.

func WithField

func WithField(key string, value any) Option

WithField returns an Option that adds an arbitrary key-value field.

func WithModule

func WithModule(module string) Option

WithModule returns an Option that adds a "module" field. Naming convention: "component/layer/service" (e.g. "user/biz/iam").

type ZapLogger

type ZapLogger struct {
	// contains filtered or unexported fields
}

ZapLogger wraps *zap.Logger and satisfies kratos log.Logger.

func New

func New(app *corev1.App) *ZapLogger

New creates a ZapLogger from a proto App config. nil-safe: returns a dev console logger when app is nil or app.Log is nil.

func (*ZapLogger) GetGormLogger

func (l *ZapLogger) GetGormLogger(module string) GormLogger

GetGormLogger returns a GORM-compatible logger scoped to the given module.

func (*ZapLogger) Log

func (l *ZapLogger) Log(level log.Level, keyvals ...any) error

Log implements kratos log.Logger.

func (*ZapLogger) Sync

func (l *ZapLogger) Sync() error

Sync flushes any buffered log entries.

func (*ZapLogger) Zap

func (l *ZapLogger) Zap() *zap.Logger

Zap returns the underlying *zap.Logger for integrations that require it (e.g. franz-go kzap plugin, GORM bridge).

Jump to

Keyboard shortcuts

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