Documentation
¶
Index ¶
- Variables
- func Initialize()
- type BaseModel
- type Conf
- type Database
- type JSONTime
- func (p *JSONTime) GobDecode(data []byte) error
- func (p JSONTime) GobEncode() ([]byte, error)
- func (p JSONTime) MarshalJSON() ([]byte, error)
- func (p *JSONTime) Scan(v interface{}) error
- func (p JSONTime) String() string
- func (p *JSONTime) UnmarshalJSON(data []byte) error
- func (p JSONTime) Value() (driver.Value, error)
- type LogWriter
- type Logger
Constants ¶
This section is empty.
Variables ¶
View Source
var ( Slave *gorm.DB Master *gorm.DB )
View Source
var LogFormatter = func(values ...interface{}) (messages []interface{}) { if len(values) > 1 { var ( sql string formattedValues []string level = values[0] currentTime = "\n[" + time.Now().Local().Format("2006-01-02 15:04:05.000000") + "]" source = fmt.Sprintf("source: (%v)", values[1]) ) messages = []interface{}{source, currentTime} if level == "sql" { messages = append(messages, fmt.Sprintf(" took: [%.2fms] \n", 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 { 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 { 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, sql) messages = append(messages, fmt.Sprintf(" \n[%v] ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned ")) } else { messages = append(messages, values[2:]...) } } return }
LogFormatter ... 自定义日志格式化
Functions ¶
func Initialize ¶
func Initialize()
Types ¶
type BaseModel ¶
type BaseModel struct { ID uint `gorm:"primary_key"` CreatedAt *JSONTime `gorm:"index;type:timestamptz" json:",omitempty"` UpdatedAt *JSONTime `gorm:"index;type:timestamptz" json:",omitempty"` DeletedAt *JSONTime `gorm:"index;type:timestamptz" json:"-"` Version uint `gorm:"not null;index;default:1" json:",omitempty"` }
BaseModel ...
type Database ¶
type Database struct { Type string `yaml:",omitempty"` Address string Port int User string `yaml:",omitempty"` Password string `yaml:",omitempty"` DB string `yaml:",omitempty"` }
Database configuration Settings
type JSONTime ¶
JSONTime ...
func (*JSONTime) UnmarshalJSON ¶
UnmarshalJSON ...
Click to show internal directories.
Click to hide internal directories.