/gol

gol is log, log for golang.

Primary LanguageGoMIT LicenseMIT

gol

Go Report Card Test Actions codecov

gol is log, log for gin and gorm.

  • Rotating logs by date

go 1.16+

Installation

go get github.com/yaoming00/gol 

Usage

define logger

var (
	ServerLogger io.Writer
	SQLLogger    io.Writer
	ErrorLogger  io.Writer
)

log for gin

    runMode := "debug"
	gin.SetMode(runMode)
	gin.DisableConsoleColor()

	gin.DefaultWriter = gol.SetLogger(runMode, ServerLogger)
	gin.DefaultErrorWriter = gol.SetLogger(runMode, ServerLogger)

log error by log.Println()

// main
	log.SetOutput(
		gol.SetLogger(
			runMode,
			ServerLogger,
		),
	)

log for gorm

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)
	}
}

Thanks