Golang xorm log operations in the file

Time:2022-8-13

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 (
 "time"
 
 "github.com/arthurkiller/rollingwriter"
 _ "github.com/go-sql-driver/mysql"
 "github.com/go-xorm/xorm"
)
 
func main() {
 var conn string = "root:[email protected](127.0.0.1)/logsdemo?charset=utf-8"
 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 {
 panic(err)
 }
 
 var logger *xorm.SimpleLogger = xorm.NewSimpleLogger(writer) 
 Engine.SetLogger(logger)
 Engine.ShowSQL(true) 
 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 (
 "fmt"
 "github.com/go-xorm/xorm"
 "time"
)
 
// 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
}

http://www.xorm.io/docs/

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.