How to Use SQLDB-Logger in Your Go SQL Applications

Jun 25, 2024 | Programming

Logging is an essential part of application development that helps troubleshoot issues and monitor performance. With SQLDB-Logger, you can easily integrate logging into your Go applications without altering the standard *sql.DB usage. Let’s dive into how to implement it!

Features of SQLDB-Logger

  • Leveled, detailed, and configurable logging.
  • Re-use existing *sql.DB as is for easy integration.
  • Customizable logger backend via a simple log interface.
  • Trackable log output with unique IDs for each call.
  • Error logging includes query, arguments, params, and related IDs.

Installation

Begin by installing SQLDB-Logger using the following command:

bash
go get -u -v github.com/simukti/sql-db-logger

For version pinning, it is advisable to use a dependency manager like Mod or Dep.

Usage

The logger is a simple interface as illustrated below:

go
type Logger interface {
    Log(ctx context.Context, level Level, msg string, data map[string]interface{})
}

Four basic implementations are provided using widely-known JSON structured loggers, which will help you get started quickly:

Integrating with an Existing SQL DB Driver

The easiest way to use SQLDB-Logger is to integrate it with your existing *sql.DB driver. Here’s how you can accomplish this:

Given the following code:

go
dsn := "username:passwd@tcp(mysqlserver:3306)/dbname?parseTime=true"
db, err := sql.Open(mysql, dsn) // db is *sql.DB
if err != nil { log.Error(err) }
db.Ping() // Check connectivity and DSN correctness

You can modify it like this:

go
import sqldblogger github.com/simukti/sql-db-logger
import github.com/simukti/sql-db-logger/logadapter/zerologadapter

loggerAdapter := zerologadapter.New(zerolog.New(os.Stdout))
db = sqldblogger.OpenDriver(dsn, db.Driver(), loggerAdapter, using_default_options) // db is STILL *sql.DB
db.Ping() // Check connectivity and DSN correctness

That’s it! All interactions with *sql.DB will now be logged.

Logger Options

When using sqldblogger.OpenDriver, various options can be customized:

go
godb = sqldblogger.OpenDriver(
    dsn,
    db.Driver(),
    loggerAdapter,
    sqldblogger.WithErrorFieldname("sql_error"),
    sqldblogger.WithDurationFieldname("query_duration"),
    sqldblogger.WithTimeFieldname("log_time"),
    // Add more options as needed...
)

For detailed documentation on the options available, please refer to this link.

Troubleshooting

If you encounter issues, consider the following troubleshooting tips:

  • Ensure the DSN provided is correct.
  • Check if the logger adapter is properly initialized.
  • Review your application logs for any error outputs.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

SQLDB-Logger is a powerful tool for incorporating structured logging into your Go applications seamlessly. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox