entslog

package module
v3.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 8 Imported by: 0

README ¶

entslog: slog ent handler

tag Go Version GoDoc Build Status Go report Coverage Contributors License

A ent Handler for slog Go library.

Installation

go get github.com/godcong/entslog@latest

Compatibility: go >= 1.21

No breaking changes will be made to exported APIs before v1.0.0.

💡 Usage

GoDoc: https://pkg.go.dev/github.com/godcong/entslog

Documentation ¶

Overview ¶

Package entslog for entgo.io/ent

Package entslog for entgo.io/ent

Package entslog for entgo.io/ent

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func New ¶

func New(dri dialect.Driver, ss ...Setting) dialect.Driver

New gets a init and an optional logging function, and returns a new slog-init that prints all outgoing operations.

Types ¶

type FilterAttrs ¶

type FilterAttrs func(context.Context, ...slog.Attr) []slog.Attr

FilterAttrs defines a function to filter out attributes from log entries.

type Handler ¶

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

func (*Handler) Filter ¶

func (h *Handler) Filter(ctx context.Context, attrs ...slog.Attr) []slog.Attr

func (*Handler) Log ¶

func (h *Handler) Log(ctx context.Context, msg string, attrs ...slog.Attr)

func (*Handler) LogError ¶

func (h *Handler) LogError(ctx context.Context, msg string, err error) error

func (*Handler) WithTrace ¶

func (h *Handler) WithTrace(ctx context.Context) string

type Option ¶

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

Option defines configuration options for the logging handler.

type Setting ¶ added in v3.0.2

type Setting = func(*Option)

Setting is a type alias for the settings.Setting type.

func WithDefaultLevel ¶

func WithDefaultLevel(level slog.Leveler) Setting

WithDefaultLevel sets the default log level for the given logging options.

- `level`: The log level to be set as the default.

Returns a function that accepts an `*Option` parameter, modifies it by setting the default log level, and returns the updated `*Option` pointer.

func WithError ¶

func WithError() Setting

WithError explicitly enables or disables error handling for the given logging options.

- `handleError`: A boolean indicating whether to enable (true) or disable (false) error handling.

Returns a function that accepts an `*Option` parameter, modifies it by setting the error handling flag, and returns the updated `*Option` pointer.

func WithErrorLevel ¶

func WithErrorLevel(level slog.Leveler) Setting

WithErrorLevel sets the error log level and enables handling of errors for the given logging options.

- `level`: The log level to be set for error logging.

Returns a function that accepts an `*Option` parameter, modifies it by setting the error log level and enabling error handling, then returns the updated `*Option` pointer.

func WithFilter ¶

func WithFilter(filter func(context.Context, ...slog.Attr) []slog.Attr) Setting

WithFilter specifies a list of attribute names to filter out from logged messages for the given logging options.

- `attrs`: A variadic list of strings representing attribute names to be filtered.

Returns a function that accepts an `*Option` parameter, modifies it by setting the list of filtered attributes, and returns the updated `*Option` pointer.

func WithLogger ¶

func WithLogger(logger *slog.Logger) Setting

WithLogger specifies the logger to be used for logging. If not specified, the default logger will be used.

- `logger`: The logger to be used for logging.

Returns a function that accepts an `*Option` parameter, modifies it by setting the logger, and returns the updated `*Option` pointer.

func WithTrace ¶

func WithTrace(trace func(context.Context) string) Setting

WithTrace assigns a custom ID generation function for the given logging options. This function will be used to generate unique IDs for log entries within a given context.

- `trace`: A function that accepts a `context.Context` and returns a string representing the generated ID.

Returns a function that accepts an `*Option` parameter, modifies it by setting the custom ID generation function, and returns the updated `*Option` pointer.

type SlogDriver ¶

type SlogDriver struct {
	Handler // log function. defaults to slog.Default()
	// contains filtered or unexported fields
}

SlogDriver is a init that logs all init operations.

func (*SlogDriver) BeginTx ¶

func (d *SlogDriver) BeginTx(ctx context.Context, opts *sql.TxOptions) (dialect.Tx, error)

BeginTx adds an log-id for the transaction and calls the underlying init BeginTx command if it is supported.

func (*SlogDriver) Close ¶

func (d *SlogDriver) Close() error

func (*SlogDriver) Dialect ¶

func (d *SlogDriver) Dialect() string

func (*SlogDriver) Exec ¶

func (d *SlogDriver) Exec(ctx context.Context, query string, args, v any) error

Exec logs its params and calls the underlying init Exec method.

func (*SlogDriver) ExecContext ¶

func (d *SlogDriver) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext logs its params and calls the underlying init ExecContext method if it is supported.

func (*SlogDriver) Query ¶

func (d *SlogDriver) Query(ctx context.Context, query string, args, v any) error

Query logs its params and calls the underlying init Query method.

func (*SlogDriver) QueryContext ¶

func (d *SlogDriver) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext logs its params and calls the underlying init QueryContext method if it is supported.

func (*SlogDriver) Tx ¶

func (d *SlogDriver) Tx(ctx context.Context) (dialect.Tx, error)

Tx adds an log-id for the transaction and calls the underlying init Tx command.

type SlogTx ¶

type SlogTx struct {
	Handler
	// contains filtered or unexported fields
}

SlogTx is a transaction implementation that logs all transaction operations.

func (*SlogTx) Commit ¶

func (d *SlogTx) Commit() error

Commit logs this step and calls the underlying transaction Commit method.

func (*SlogTx) Exec ¶

func (d *SlogTx) Exec(ctx context.Context, query string, args, v any) error

Exec logs its params and calls the underlying transaction Exec method.

func (*SlogTx) ExecContext ¶

func (d *SlogTx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext logs its params and calls the underlying transaction ExecContext method if it is supported.

func (*SlogTx) Query ¶

func (d *SlogTx) Query(ctx context.Context, query string, args, v any) error

Query logs its params and calls the underlying transaction Query method.

func (*SlogTx) QueryContext ¶

func (d *SlogTx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext logs its params and calls the underlying transaction QueryContext method if it is supported.

func (*SlogTx) Rollback ¶

func (d *SlogTx) Rollback() error

Rollback logs this step and calls the underlying transaction Rollback method.

type TraceFunc ¶

type TraceFunc func(context.Context) string

TraceFunc generates a unique identifier for a log entry to string.

Jump to

Keyboard shortcuts

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