gol is log, log for gin and gorm.
- Rotating logs by date
go 1.16+
go get github.com/yaoming00/gol
var (
ServerLogger io.Writer
SQLLogger io.Writer
ErrorLogger io.Writer
)
runMode := "debug"
gin.SetMode(runMode)
gin.DisableConsoleColor()
gin.DefaultWriter = gol.SetLogger(runMode, ServerLogger)
gin.DefaultErrorWriter = gol.SetLogger(runMode, ServerLogger)
// main
log.SetOutput(
gol.SetLogger(
runMode,
ServerLogger,
),
)
func init() {
SQLLogger = gol.NewLogger(${path + filename})
getDB()
}
func getDB() {
runMode := "debug"
sqlLogWriter := gol.SetLogger(runMode, SQLLogger)
newLogger := logger.New(
log.New(sqlLogWriter, "\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.LogLevel(${logLevel}),
IgnoreRecordNotFoundError: true,
Colorful: false,
},
)
var err error
db, err = gorm.Open(mysql.Open(config.DBLink), &gorm.Config{
Logger: newLogger,
})
if err != nil {
log.Println("Get SQL connection ERROR:", err)
}
}