Skip to content

Go Gorm Logger

DodaTech 1 min read

In this tutorial, you'll learn about GORM Logger: No SQL Logging. We cover key concepts, practical examples, and best practices.

GORM SQL logging -- Enable GORM's built-in logger to see executed SQL queries during development.

The Problem

By default, GORM logs nothing for successful queries. GORM's logger shows SQL, execution time, and errors.

Wrong

db, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{})
db.Find(&users)

Output:

// No output at all
db, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{
    Logger: logger.Default.LogMode(logger.Info),
})
db.Find(&users)

Output:

[2026-06-24] [0.12ms] SELECT * FROM users

Prevention

  • Use logger.Info for development
  • Use logger.Warn for staging
  • Use logger.Silent for production
  • Customize with logger.New()
  • Set SlowThreshold for slow query logging

Common Mistakes with gorm logger

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

**Can I log queries to a file?**

Yes. Pass custom io.Writer to logger.New().

How to ignore Info statements?

Use LogMode(logger.Warn) to only show Warnings and Errors.

Does logging affect performance?

Yes. Use Silent in production.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro