Use a new ContextLogger interface to implement logger #1557

Merged
lunny merged 8 commits from lunny/log_context into master 2020-02-29 09:00:08 +00:00
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 2020-02-27 09:12:34 +00:00
lunny added the
kind
feature
label 2020-02-27 09:12:34 +00:00
lunny changed title from WIP: Use a new ContextLogger interface to implement logger to Use a new ContextLogger interface to implement logger 2020-02-29 02:29:11 +00:00
First-time contributor

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)
Contributor

没问题

没问题
lunny closed this pull request 2020-02-29 09:00:08 +00:00
Sign in to join this conversation.
No description provided.