{"id":13413329,"url":"https://github.com/simukti/sqldb-logger","last_synced_at":"2025-04-04T13:08:55.612Z","repository":{"id":36320181,"uuid":"219189025","full_name":"simukti/sqldb-logger","owner":"simukti","description":"A logger for Go SQL database driver without modifying existing *sql.DB stdlib usage.","archived":false,"fork":false,"pushed_at":"2023-12-29T17:13:44.000Z","size":1378,"stargazers_count":366,"open_issues_count":11,"forks_count":19,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-28T12:03:47.733Z","etag":null,"topics":["database","db","go","golang","logger","query","sql","sql-logger","sqldb-logger"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simukti.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-11-02T17:28:03.000Z","updated_at":"2025-03-26T13:26:06.000Z","dependencies_parsed_at":"2024-01-08T15:34:44.741Z","dependency_job_id":null,"html_url":"https://github.com/simukti/sqldb-logger","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simukti%2Fsqldb-logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simukti%2Fsqldb-logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simukti%2Fsqldb-logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simukti%2Fsqldb-logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simukti","download_url":"https://codeload.github.com/simukti/sqldb-logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247176477,"owners_count":20896479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","db","go","golang","logger","query","sql","sql-logger","sqldb-logger"],"created_at":"2024-07-30T20:01:37.873Z","updated_at":"2025-04-04T13:08:55.591Z","avatar_url":"https://github.com/simukti.png","language":"Go","funding_links":[],"categories":["Logging","Go","日志记录","Logging 日志库","Repositories","Relational Databases"],"sub_categories":["Search and Analytic Databases","Advanced Console UIs","检索及分析资料库","SQL 查询语句构建库"],"readme":"# SQLDB-Logger\n\n[![Coverage Status](https://coveralls.io/repos/github/simukti/sqldb-logger/badge.svg)](https://coveralls.io/github/simukti/sqldb-logger) [![Go Report Card](https://goreportcard.com/badge/github.com/simukti/sqldb-logger)](https://goreportcard.com/report/github.com/simukti/sqldb-logger) [![Sonar Violations (long format)](https://img.shields.io/sonar/violations/simukti_sqldb-logger?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=simukti_sqldb-logger) [![Sonar Tech Debt](https://img.shields.io/sonar/tech_debt/simukti_sqldb-logger?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=simukti_sqldb-logger) [![Sonar Quality Gate](https://img.shields.io/sonar/quality_gate/simukti_sqldb-logger?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=simukti_sqldb-logger) [![Documentation](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/github.com/simukti/sqldb-logger) [![License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://raw.githubusercontent.com/simukti/sqldb-logger/master/LICENSE.txt)\n\nA logger for Go SQL database driver without modify existing `*sql.DB` stdlib usage.\n\n![shameless console output sample](./logadapter/zerologadapter/console.jpg?raw=true \"go sql database logger output\") \n_Colored console writer output above only for sample/development_\n\n## FEATURES\n\n- Leveled, detailed and [configurable](./options.go) logging.\n- Keep using (or re-use existing) `*sql.DB` as is.\n- Bring your own logger backend via simple log interface.\n- Trackable log output:\n    - Every call has its own unique ID.\n    - Prepared statement and execution will have same ID.\n    - On execution/result error, it will include the query, arguments, params, and related IDs. \n\n## INSTALL\n\n```bash\ngo get -u -v github.com/simukti/sqldb-logger\n```\n\n_Version pinning using dependency manager such as [Mod](https://github.com/golang/go/wiki/Modules) or [Dep](https://github.com/golang/dep) is highly recommended._\n\n## USAGE\n\nAs a start, `Logger` is just a simple interface:\n\n```go\ntype Logger interface {\n\tLog(ctx context.Context, level Level, msg string, data map[string]interface{})\n}\n``` \n\nThere are 4 included basic implementation that uses well-known JSON structured logger for quickstart:\n\n- [Zerolog adapter](logadapter/zerologadapter): Using [rs/zerolog](https://github.com/rs/zerolog) as its logger.\n- [Onelog adapter](logadapter/onelogadapter): Using [francoispqt/onelog](https://github.com/francoispqt/onelog) as its logger.\n- [Zap adapter](logadapter/zapadapter): Using [uber-go/zap](https://github.com/uber-go/zap) as its logger.\n- [Logrus adapter](logadapter/logrusadapter): Using [sirupsen/logrus](https://github.com/sirupsen/logrus) as its logger.\n\n_Note: [those adapters](./logadapter) does not use given `context`, you need to modify it and adjust with your needs._ \n_(example: add http request id/whatever value from context to query log when you call `QueryerContext` and`ExecerContext` methods)_\n\nThen for that logger to works, you need to integrate with a compatible driver which will be used by `*sql.DB`.\n\n### INTEGRATE WITH EXISTING SQL DB DRIVER\n \nRe-use from existing `*sql.DB` driver, this is the simplest way:\n\nFor example, from:\n\n```go\ndsn := \"username:passwd@tcp(mysqlserver:3306)/dbname?parseTime=true\"\ndb, err := sql.Open(\"mysql\", dsn) // db is *sql.DB\ndb.Ping() // to check connectivity and DSN correctness\n```\n\nTo:\n\n```go\n// import sqldblogger \"github.com/simukti/sqldb-logger\"\n// import \"github.com/simukti/sqldb-logger/logadapter/zerologadapter\"\ndsn := \"username:passwd@tcp(mysqlserver:3306)/dbname?parseTime=true\"\ndb, err := sql.Open(\"mysql\", dsn) // db is *sql.DB\n// handle err\nloggerAdapter := zerologadapter.New(zerolog.New(os.Stdout))\ndb = sqldblogger.OpenDriver(dsn, db.Driver(), loggerAdapter/*, using_default_options*/) // db is STILL *sql.DB\ndb.Ping() // to check connectivity and DSN correctness\n```\n\nThat's it, all `*sql.DB` interaction now logged.\n\n### INTEGRATE WITH SQL DRIVER STRUCT\n\nIt is also possible to integrate with following public empty struct driver directly: \n\n#### MySQL ([go-sql-driver/mysql](https://github.com/go-sql-driver/mysql))\n\n```go\ndb := sqldblogger.OpenDriver(dsn, \u0026mysql.MySQLDriver{}, loggerAdapter /*, ...options */)\n```\n\n#### PostgreSQL ([lib/pq](https://github.com/lib/pq))\n\n```go\ndb := sqldblogger.OpenDriver(dsn, \u0026pq.Driver{}, loggerAdapter /*, ...options */) \n```\n\n#### SQLite3 ([mattn/go-sqlite3](https://github.com/mattn/go-sqlite3))\n\n```go\ndb := sqldblogger.OpenDriver(dsn, \u0026sqlite3.SQLiteDriver{}, loggerAdapter /*, ...options */)\n```\n\n_Following struct drivers **maybe** compatible:_ \n\n#### SQL Server ([denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb))\n\n```go\ndb := sqldblogger.OpenDriver(dsn, \u0026mssql.Driver{}, loggerAdapter /*, ...options */)\n```\n\n#### Oracle ([mattn/go-oci8](https://github.com/mattn/go-oci8))\n\n```go\ndb := sqldblogger.OpenDriver(dsn, oci8.OCI8Driver, loggerAdapter /*, ...options */)\n```\n\n## LOGGER OPTIONS\n\nWhen using `sqldblogger.OpenDriver(dsn, driver, logger, opt...)` without 4th variadic argument, it will use [default options](./options.go#L37-L59).\n\nHere is sample of `OpenDriver()` using all available options and use non-default value:\n\n```go\ndb = sqldblogger.OpenDriver(\n    dsn, \n    db.Driver(), \n    loggerAdapter,\n    // AVAILABLE OPTIONS\n    sqldblogger.WithErrorFieldname(\"sql_error\"),                    // default: error\n    sqldblogger.WithDurationFieldname(\"query_duration\"),            // default: duration\n    sqldblogger.WithTimeFieldname(\"log_time\"),                      // default: time\n    sqldblogger.WithSQLQueryFieldname(\"sql_query\"),                 // default: query\n    sqldblogger.WithSQLArgsFieldname(\"sql_args\"),                   // default: args\n    sqldblogger.WithMinimumLevel(sqldblogger.LevelTrace),           // default: LevelDebug\n    sqldblogger.WithLogArguments(false),                            // default: true\n    sqldblogger.WithDurationUnit(sqldblogger.DurationNanosecond),   // default: DurationMillisecond\n    sqldblogger.WithTimeFormat(sqldblogger.TimeFormatRFC3339),      // default: TimeFormatUnix\n    sqldblogger.WithLogDriverErrorSkip(true),                       // default: false\n    sqldblogger.WithSQLQueryAsMessage(true),                        // default: false\n    sqldblogger.WithUIDGenerator(sqldblogger.UIDGenerator),         // default: *defaultUID\n    sqldblogger.WithConnectionIDFieldname(\"con_id\"),                // default: conn_id\n    sqldblogger.WithStatementIDFieldname(\"stm_id\"),                 // default: stmt_id\n    sqldblogger.WithTransactionIDFieldname(\"trx_id\"),               // default: tx_id\n    sqldblogger.WithWrapResult(false),                              // default: true\n    sqldblogger.WithIncludeStartTime(true),                         // default: false\n    sqldblogger.WithStartTimeFieldname(\"start_time\"),               // default: start\n    sqldblogger.WithPreparerLevel(sqldblogger.LevelDebug),          // default: LevelInfo\n    sqldblogger.WithQueryerLevel(sqldblogger.LevelDebug),           // default: LevelInfo\n    sqldblogger.WithExecerLevel(sqldblogger.LevelDebug),            // default: LevelInfo\n)\n```\n\n[Click here](https://pkg.go.dev/github.com/simukti/sqldb-logger#Option) for options documentation.\n\n## MOTIVATION\n\nI want to:\n\n- Keep using `*sql.DB`.\n- Have configurable output field.\n- Leverage structured logging.\n- Fetch and log `context.Context` value if needed. \n- Re-use [pgx log interface](https://github.com/jackc/pgx/blob/f3a3ee1a0e5c8fc8991928bcd06fdbcd1ee9d05c/logger.go#L46-L49).\n\nI haven't found Go `*sql.DB` logger with that features, so why not created myself? \n\n## REFERENCES\n\n- [Stdlib sql.DB](https://github.com/golang/go/blob/master/src/database/sql/sql.go)\n- [SQL driver interfaces](https://github.com/golang/go/blob/master/src/database/sql/driver/driver.go)\n- [SQL driver implementation](https://github.com/golang/go/wiki/SQLDrivers)\n\n## CONTRIBUTE\n\nIf you found a bug, typo, wrong test, idea, help with existing issue, or anything constructive.\n \nDon't hesitate to create an issue or pull request.\n\n## CREDITS\n\n- [pgx](https://github.com/jackc/pgx) for awesome PostgreSQL driver.\n\n## LICENSE\n\n[MIT](./LICENSE.txt)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimukti%2Fsqldb-logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimukti%2Fsqldb-logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimukti%2Fsqldb-logger/lists"}