dblogger

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OFF = iota + 1
	DEBUG
	INFO
	WARN
	ERROR
)
View Source
const (
	SessionIDKey = "__xorm_session_id"
)

Variables

View Source
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
	if len(values) > 1 {
		var (
			sql             string
			formattedValues []string
			level           = values[0]
		)

		if level == "sql" {

			messages = append(messages, fmt.Sprintf("执行耗时: %.2fms\t", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))

			for _, value := range values[4].([]interface{}) {
				indirectValue := reflect.Indirect(reflect.ValueOf(value))
				if indirectValue.IsValid() {
					value = indirectValue.Interface()
					if t, ok := value.(time.Time); ok {
						if t.IsZero() {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00"))
						} else {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
						}
					} else if b, ok := value.([]byte); ok {
						if str := string(b); isPrintable(str) {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
						} else {
							formattedValues = append(formattedValues, "'<binary>'")
						}
					} else if r, ok := value.(driver.Valuer); ok {
						if value, err := r.Value(); err == nil && value != nil {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
						} else {
							formattedValues = append(formattedValues, "NULL")
						}
					} else {
						switch value.(type) {
						case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
							formattedValues = append(formattedValues, fmt.Sprintf("%v", value))
						default:
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
						}
					}
				} else {
					formattedValues = append(formattedValues, "NULL")
				}
			}

			if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
				sql = values[3].(string)
				for index, value := range formattedValues {
					placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
					sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
				}
			} else {
				formattedValuesLength := len(formattedValues)
				for index, value := range sqlRegexp.Split(values[3].(string), -1) {
					sql += value
					if index < formattedValuesLength {
						sql += formattedValues[index]
					}
				}
			}

			messages = append(messages, fmt.Sprintf("执行SQL: %v\t", sql))

			messages = append(messages, fmt.Sprintf("影响行数: %v", strconv.FormatInt(values[5].(int64), 10)))
		} else {

			messages = append(messages, values[2:]...)

		}
	}

	return
}

Functions

This section is empty.

Types

type Config

type Config struct {
	// Logger                    *zap.Logger   `json:"logger,omitempty"`
	LogLevel                  LogLevel      `json:"logLevel,omitempty"`
	SlowThreshold             time.Duration `json:"slowThreshold,omitempty"`
	Colorful                  bool          `json:"colorful,omitempty"`
	IgnoreRecordNotFoundError bool          `json:"ignoreRecordNotFoundError,omitempty"`
	ParameterizedQueries      bool          `json:"parameterizedQueries,omitempty"`
	ShowSql                   bool          `json:"showSql,omitempty"`
}

func (*Config) String

func (c *Config) String() string

type GormLoggerInterface

type GormLoggerInterface interface {
	LogMode(gormlogger.LogLevel) gormlogger.Interface
	Info(context.Context, string, ...interface{})
	Warn(context.Context, string, ...interface{})
	Error(context.Context, string, ...interface{})
	Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)
}

type LogLevel

type LogLevel int

type Logger

type Logger struct {
	LogLevel                  LogLevel
	SlowThreshold             time.Duration
	Colorful                  bool
	IgnoreRecordNotFoundError bool
	ParameterizedQueries      bool
	// contains filtered or unexported fields
}

func NewDBLogger

func NewDBLogger(config Config, options ...Option) *Logger

func (Logger) AfterSQL

func (l Logger) AfterSQL(ctx xormlogger.LogContext)

func (Logger) BeforeSQL

func (l Logger) BeforeSQL(ctx xormlogger.LogContext)

func (Logger) Debugf

func (l Logger) Debugf(format string, v ...interface{})

func (Logger) Error

func (l Logger) Error(ctx context.Context, msg string, data ...interface{})

func (Logger) Errorf

func (l Logger) Errorf(format string, v ...interface{})

func (Logger) Info

func (l Logger) Info(ctx context.Context, msg string, data ...interface{})

func (Logger) Infof

func (l Logger) Infof(format string, v ...interface{})

func (Logger) IsShowSQL

func (l Logger) IsShowSQL() bool

func (Logger) Level

func (l Logger) Level() xormlogger.LogLevel

func (Logger) LogMode

func (l Logger) LogMode(level dbLogger.LogLevel) dbLogger.Interface

func (Logger) Print added in v1.0.0

func (l Logger) Print(values ...interface{})

func (Logger) SetLevel

func (l Logger) SetLevel(lv xormlogger.LogLevel)

func (Logger) ShowSQL

func (l Logger) ShowSQL(show ...bool)

func (Logger) String

func (l Logger) String() string

func (Logger) Trace

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

func (Logger) Warn

func (l Logger) Warn(ctx context.Context, msg string, data ...interface{})

func (Logger) Warnf

func (l Logger) Warnf(format string, v ...interface{})

type Option added in v0.1.1

type Option interface {
	Name() string
	Value() interface{}
}

func WithCustomLogger added in v0.1.1

func WithCustomLogger(log *zap.Logger) Option

type XormLoggerInterface

type XormLoggerInterface interface {
	BeforeSQL(context xormlogger.LogContext) // only invoked when IsShowSQL is true
	AfterSQL(context xormlogger.LogContext)
	Debugf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
	Infof(format string, v ...interface{})
	Warnf(format string, v ...interface{})

	Level() xormlogger.LogLevel
	SetLevel(l xormlogger.LogLevel)
	ShowSQL(show ...bool)
	IsShowSQL() bool
}

Jump to

Keyboard shortcuts

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