{"id":1249,"hash":"5dc23c29b8e29be6f472d08d161be8d6816cbce2f965a8a27d28c4604c1bd736","pattern":"How to disable default error logger in Go-Gorm","full_message":"I am using GORM with MySQL, I have encountered and handled the error Error 1062: Duplicate entry. The problem is that it's still printed to the console.\n\nCode in gym/models/auth.go:49:\n\nfunc AddAuth(username, password string) error {\n    passwordHash, err := auth.HashPassword(password, argon2Conf)\n    if err != nil {\n        return err\n    }\n    userAuth := Auth{\n        Username: username,\n        Password: passwordHash,\n    }\n    return db.Create(&userAuth).Error\n}\n\nI am handling the error in the handler function:\n\nfunc SignUpHandler(c *gin.Context) {\n    var form user\n    if err := c.ShouldBind(&form); err != nil {\n        c.JSON(http.StatusBadRequest, gin.H{\"error\": err.Error()})\n        return\n    }\n    if err := models.AddAuth(form.Username, form.Password); err == nil {\n        c.JSON(http.StatusOK, gin.H{\"status\": \"you are signed in\"})\n    } else {\n        // I think I have handled the sql error here\n        c.JSON(http.StatusBadRequest, gin.H{\"error\": \"sign in failed\"})\n    }\n}\n\nWhen I send a POST request, the error is correctly handled and I get the correct response with {\"error\": \"sign in failed\"}. But the console still prints this error message:\n\n(/...../gym/models/auth.go:49) \n[2019-04-28 23:37:06]  Error 1062: Duplicate entry '123123' for key 'username' \n[GIN] 2019/04/28 - 23:37:06 | 400 |  136.690908ms |             ::1 | POST     /signup\n\nI am confused since I handled the error but it still gets printed. How to prevent this error from getting printed to the error log? Or am I handle the error correct?","ecosystem":"go","package_name":"mysql","package_version":null,"solution":"UPDATE: for gorm v2:\n\nUse the Logger in gorm.Config:\n\ndb, err := gorm.Open(sqlite.Open(\"test.db\"), &gorm.Config{\n  Logger: logger.Default.LogMode(logger.Silent),\n})\n\nFor gorm v1:\n\nUse db.LogMode to silence the default error logger.\n\nLogMode set log mode, true for detailed logs, false for no log, default, will only print error logs.\n\ndb.LogMode(false) should do the job!","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/55892035/how-to-disable-default-error-logger-in-go-gorm","votes":16,"created_at":"2026-04-19T04:52:42.960651+00:00","updated_at":"2026-04-19T04:52:42.960651+00:00"}