moul / zapgorm2

⚡ zap logging driver for gorm v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why does zapgorm2.New() default to gormlogger.Warn?

sonnysideup opened this issue · comments

I'd expect a new logger to take on the level of the provided zap logger, right? In the case where the zap logger is set to zapcore.DebugLevel, then I'd expect that to set the gorm logger to logger.Info since that's the highest verbosity level supported by gorm.

I have to work around this issue using the following code snippet, but I feel like this should really be internal to the library since it's not unreasonable to expect your gorm log level to closely match your zap level.

	log, _ := zap.NewDevelopment()

	var gormLogLevel logger.LogLevel
	switch log.Level() {
	case zapcore.DebugLevel, zapcore.InfoLevel:
		gormLogLevel = logger.Info
	case zapcore.WarnLevel:
		gormLogLevel = logger.Warn
	default:
		gormLogLevel = logger.Error
	}

	// NOTE: this always sets the logger to WARN
	wrongLevel := zapgorm2.New(log)
	desiredLogger := wrongLevel.LogMode(gormLogLevel)

I'm happy to submit a PR if this all makes sense to you.

You should set the parameter value for SlowThreshold, for example:
zapLogger := zapgorm2.New(zap.L())
zapLogger.SlowThreshold = 2 * time.Second