Golang xorm log operations in the file


golang accesses the database to record SQL statements:

The packages used are:

1: github.com/arthurkiller/rollingwriter //write log package

2: github.com/go-xorm/xorm //xorm package

The specific implementation is:

package main 
import (
 _ "github.com/go-sql-driver/mysql"
func main() {
 var conn string = "root:[email protected]("
 Engine, err := xorm.NewEngine("mysql", conn)
 if err != nil {
 panic("mysql connect fail")
 config := rollingwriter.Config{
 LogPath: "./logs", //log path
 TimeTagFormat: "060102150405", //time format string
 FileName: "mysql_exec", //log file name
 MaxRemain: 3, //Configure the maximum number of logs retained
 RollingPolicy: rollingwriter.VolumeRolling, //Configure the rolling policy norolling t 
 imerolling volumerolling
 RollingTimePattern: "* * * * * *", //Configure the time rolling strategy
 RollingVolumeSize: "1M", //Configure the lower limit size of the truncated file
 WriterMode: "none",
 BufferWriterThershould: 256,
 // Compress will compress log file with gzip
 Compress: true,
 writer, err := rollingwriter.NewWriterFromConfig(&config)
 if err != nil {
 var logger *xorm.SimpleLogger = xorm.NewSimpleLogger(writer) 
 logger.Info("ts info code") 
 Engine.Exec("select * from cp_order where id = ? ", 3)
 time.Sleep(1 * time.Second)

rollingwriter implements an io.Writer passed in to xorm.NewSimpleLogger, implements log writing in xorm, and saves the log to a file in rollingwriter

Dependency package:https://github.com/robfig/cron

Added: golang xorm Model model file template

* Record.go

package models 
import (
// User logs in for the first time
type FirstLoginRecord struct {
 Id int64 `xorm:"not null pk autoincr INT(11)"`
 UserID int64 `json:"userid" xorm:"int(11) not null 'userid'"`
 IP string `json:"logonip" xorm:"varchar(45) not null 'logonip'"`
 CreatedAt time.Time `json:"logontime" xorm:"timestamp not null 'logontime'"`
 UpdatedAt time.Time `json:"logofftime" xorm:"timestamp not null 'logofftime'"`
 Source int `json:"source" xorm:"tinyint(3) not null"`
 IsCharged int `json:"ischargeaccount" xorm:"tinyint(3) unsigned not null 'ischargeaccount'"`
func (*FirstLoginRecord) TableName() string {
 return "W_UserFirstLogonRec"
func (*FirstLoginRecord) DB() *xorm.Engine {
 return DbDefault()
func (t *FirstLoginRecord) Save() error {
 var err error
 var engine = t.DB()
 if t.Id == 0 {
 t.Id, err = engine.Insert(t)
 return err
 var rowsAffected int64
 rowsAffected, err = engine.Id(t.Id).Update(t)
 fmt.Printf("@table: %s: %d rows affected", t.TableName(), rowsAffected)
 return err
// User id unique constraint
func (this *FirstLoginRecord) AddDistinct() error {
 var err error
 var engine = this.DB()
 var has bool
 var ent = FirstLoginRecord{UserID: this.UserID }
 has, err = engine.Get(&ent)
 if has {
 return nil
 this.Id, err = engine.Insert(this)
 return err


The above is my personal experience, I hope it can give you a reference, and I hope you will support developpaer a lot. If there are any mistakes or not considered completely, please let me know.