entslog

package module
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 6 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 provides logging capabilities initWith ent entities.

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var DefaultOption = Option{
	// contains filtered or unexported fields
}

DefaultOption provides the default configuration options for the logging handler.

Functions ¶

func New ¶

func New(logger *slog.Logger, opts ...func(*Option) *Option) func(dialect.Driver) dialect.Driver

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

func WithDefaultLevel ¶

func WithDefaultLevel(level slog.Leveler) func(*Option) *Option

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 WithErrorLevel ¶

func WithErrorLevel(level slog.Leveler) func(*Option) *Option

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 ¶ added in v2.0.2

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

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 WithGenerateID ¶

func WithGenerateID(generateID func(context.Context) string) func(*Option) *Option

WithGenerateID 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.

- `generateID`: 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.

func WithHandleError ¶

func WithHandleError(handleError bool) func(*Option) *Option

WithHandleError 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.

Types ¶

type Handler ¶

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

func (*Handler) Filter ¶ added in v2.0.2

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

type Option ¶

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

Option defines configuration options for the logging handler.

type SlogDriver ¶

type SlogDriver struct {
	dialect.Driver // underlying init.
	Handler        // log function. defaults to slog.Default()
}

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) 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 {
	dialect.Tx // underlying transaction.
	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.

Jump to

Keyboard shortcuts

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