Use a new ContextLogger interface to implement logger #1557

Merged
lunny merged 8 commits from lunny/log_context into master 2 years ago
lunny commented 2 years ago
Owner

This pull request introduces a new logger interface with context, so that customerize logger implementation could do more things. For compitable, the old interface has been kept and will be adapter to the new logger interface.

type ContextLogger interface {
	SQLLogger

	Debugf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
	Infof(format string, v ...interface{})
	Warnf(format string, v ...interface{})

	Level() LogLevel
	SetLevel(l LogLevel)

	ShowSQL(show ...bool)
	IsShowSQL() bool
}

The new logger interface introduces two new methods BeforeSQL(context LogContext), and AfterSQL(context LogContext). Meanwhile it removs some unnecessary methods.

type SQLLogger interface {
	BeforeSQL(context LogContext)
	AfterSQL(context LogContext)
}

The BeforeSQL will be invoked before a SQL will be executed, and AfterSQL will be invoked after a SQL executed. Both methods will be given LogContext which you could find some useful informations.

type LogContext struct {
	Ctx         context.Context
	SQL         string        // log content or SQL
	Args        []interface{} // if it's a SQL, it's the arguments
	ExecuteTime time.Duration
	Err         error // SQL executed error
}
This pull request introduces a new logger interface with context, so that customerize logger implementation could do more things. For compitable, the old interface has been kept and will be adapter to the new logger interface. ``` type ContextLogger interface { SQLLogger Debugf(format string, v ...interface{}) Errorf(format string, v ...interface{}) Infof(format string, v ...interface{}) Warnf(format string, v ...interface{}) Level() LogLevel SetLevel(l LogLevel) ShowSQL(show ...bool) IsShowSQL() bool } ``` The new logger interface introduces two new methods `BeforeSQL(context LogContext)`, and `AfterSQL(context LogContext)`. Meanwhile it removs some unnecessary methods. ```go type SQLLogger interface { BeforeSQL(context LogContext) AfterSQL(context LogContext) } ``` The `BeforeSQL` will be invoked before a SQL will be executed, and `AfterSQL` will be invoked after a SQL executed. Both methods will be given `LogContext` which you could find some useful informations. ```go type LogContext struct { Ctx context.Context SQL string // log content or SQL Args []interface{} // if it's a SQL, it's the arguments ExecuteTime time.Duration Err error // SQL executed error } ```
lunny added this to the 1.0.0 milestone 2 years ago
lunny added the
kind/feature
label 2 years ago
lunny changed title from WIP: Use a new ContextLogger interface to implement logger to Use a new ContextLogger interface to implement logger 2 years ago

LGTM
image

LGTM ![image](https://www.google.com/search?q=GOPHER+LGTM&sxsrf=ALeKk00OwezqRFtUQYQpLH75Y0NBd_o6vQ:1582963868934&source=lnms&tbm=isch&sa=X&ved=2ahUKEwj_g-b4p_bnAhWDy4sBHW_qB2wQ_AUoAXoECAwQAw&biw=1920&bih=937#imgrc=-5xbol-m4XMf6M)

没问题

没问题
lunny closed this pull request 2 years ago
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
The pull request has been merged as 41388c2f56.
Sign in to join this conversation.
Loading…
There is no content yet.