{"id":17325055,"url":"https://github.com/orandin/slog-gorm","last_synced_at":"2025-05-11T15:43:02.421Z","repository":{"id":188564781,"uuid":"678985728","full_name":"orandin/slog-gorm","owner":"orandin","description":"A slog adapter, highly configurable, for gorm logger","archived":false,"fork":false,"pushed_at":"2024-08-13T19:54:39.000Z","size":70,"stargazers_count":52,"open_issues_count":3,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-11T09:44:14.103Z","etag":null,"topics":["gorm-logger","slog"],"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/orandin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-08-15T21:09:04.000Z","updated_at":"2025-04-26T17:45:40.000Z","dependencies_parsed_at":"2023-08-15T22:42:35.710Z","dependency_job_id":"76a907f1-38fb-4ab5-a854-ec372083fe1a","html_url":"https://github.com/orandin/slog-gorm","commit_stats":null,"previous_names":["orandin/slog-gorm"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orandin%2Fslog-gorm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orandin%2Fslog-gorm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orandin%2Fslog-gorm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orandin%2Fslog-gorm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orandin","download_url":"https://codeload.github.com/orandin/slog-gorm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253547176,"owners_count":21925541,"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":["gorm-logger","slog"],"created_at":"2024-10-15T14:12:20.042Z","updated_at":"2025-05-11T15:43:02.382Z","avatar_url":"https://github.com/orandin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# slog-gorm\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/orandin/slog-gorm.svg)](https://pkg.go.dev/github.com/orandin/slog-gorm)\n![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.21-%23007d9c)\n[![CI](https://github.com/orandin/slog-gorm/actions/workflows/ci.yaml/badge.svg)](https://github.com/orandin/slog-gorm/actions/workflows/ci.yaml)\n[![Go report](https://goreportcard.com/badge/github.com/orandin/slog-gorm)](https://goreportcard.com/report/github.com/orandin/slog-gorm)\n[![Coverage](https://img.shields.io/codecov/c/github/orandin/slog-gorm)](https://codecov.io/gh/orandin/slog-gorm)\n[![Renovate](https://img.shields.io/badge/dependabot-enabled-brightgreen.svg)](https://docs.github.com/en/code-security/dependabot/working-with-dependabot)\n[![License](https://img.shields.io/github/license/orandin/slog-gorm)](./LICENSE)\n\n`slog-gorm` provides a slog adapter, highly configurable, for [gorm logger](https://gorm.io/docs/logger.html)\nto have homogeneous logs between your application / script and gorm.\n\n## Key features\n\n- compatible with any `slog.Handler`, which allows you to keep control on\n  the format of your logs.\n- can define a threshold to identify and log the slow queries.\n- can log all SQL messages or just the errors if you prefer.\n- can define a custom `slog.Level` for errors, slow queries or the other logs.\n- can log context values with each Gorm log.\n\n## Requirement\n\n- `golang \u003e= 1.21`\n\n## Usage\n\n```golang\nimport (\n    \"log/slog\"\n    \"os\"\n\n    \"gorm.io/driver/sqlite\"\n    \"gorm.io/gorm\"\n\n    slogGorm \"github.com/orandin/slog-gorm\"\n)\n\n// Create an slog-gorm instance\ngormLogger := slogGorm.New() // use slog.Default() by default\n\n\n// GORM: Globally mode\ndb, err := gorm.Open(sqlite.Open(\"test.db\"), \u0026gorm.Config{\n    Logger: gormLogger,\n})\n\n// GORM: Continuous session mode\ntx := db.Session(\u0026Session{Logger: gormLogger})\ntx.First(\u0026user)\ntx.Model(\u0026user).Update(\"Age\", 18)\n```\n\n### With your `slog.Logger`\n\nThe following example shows you how to use a specific `slog.Logger` with `slog-gorm`:\n\n```golang\n// With your slog.Logger\nlogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))\n\n// Also, you can set specific attributes to distinguish between your application logs and gorm logs\n// logger = logger.With(slog.String(\"log_type\", \"database\"))\n\ngormLogger := slogGorm.New(\n    // slogGorm.WithLogger(logger), // Deprecated since v1.3.0, use `slogGorm.WithHandler(...)` instead.\n    slogGorm.WithHandler(logger.Handler()), // since v1.3.0\n    slogGorm.WithTraceAll(), // trace all messages \n    slogGorm.SetLogLevel(DefaultLogType, slog.Level(32)), // Define the default logging level\n)\n```\n\n### Use your custom `slog.Level`\n\nAs some loggers *(e.g. syslog)* have their own logging levels, `slog-gorm` lets you\nuse them to ensure the consistency of your logs and make them easier to understand.\n\nYou can set the logging level for these log types:\n\n| Type                        | Description                          | Default           |\n|-----------------------------|--------------------------------------|-------------------|\n| `slogGorm.ErrorLogType`     | For SQL errors                       | `slog.LevelError` |\n| `slogGorm.SlowQueryLogType` | For slow queries                     | `slog.LevelWarn`  |\n| `slogGorm.DefaultLogType`   | For other messages *(default level)* | `slog.LevelInfo`  |\n\nExample:\n\n```golang\nconst (\n    LOG_EMERG   = slog.Level(0)\n    // ...\n    LOG_ERR     = slog.Level(3)\n    LOG_WARNING = slog.Level(4)\n    LOG_NOTICE  = slog.Level(5)\n    // ...\n    LOG_DEBUG   = slog.Level(7)\n)\n\nlogger := slog.New(syslogHandler)\n\ngormLogger := slogGorm.New(\n    slogGorm.WithHandler(logger.Handler()),\n\n    // Set logging level for SQL errors\n    slogGorm.SetLogLevel(slogGorm.ErrorLogType, LOG_ERR)\n\n    // Set logging level for slow queries\n    slogGorm.SetLogLevel(slogGorm.SlowQueryLogType, LOG_NOTICE)\n\n    // Set logging level for other messages (default level)\n    slogGorm.SetLogLevel(slogGorm.DefaultLogType, LOG_DEBUG)\n)\n```\n\n### Other options\n\n```golang\ncustomLogger := sloggorm.New(\n\tslogGorm.WithSlowThreshold(500 * time.Millisecond), // to identify slow queries\n\n\tslogGorm.WithRecordNotFoundError(), // don't ignore not found errors\n\n\tslogGorm.WithSourceField(\"origin\"), // instead of \"file\" (by default)\n\n\tslogGorm.WithErrorField(\"err\"),     // instead of \"error\" (by default)\n\n\tslogGorm.WithContextValue(\"slogAttrName1\", \"ctxKey\"), // adds an slog.Attr if a value is found for this key in the Gorm's query context\n\n\tslogGorm.WithContextFunc(\"slogAttrName2\", func(ctx context.Context) (slog.Value, bool) {\n\t\tv, ok := ctx.Value(ctxKey1).(time.Duration)\n\t\tif !ok {\n\t\t\treturn slog.Value{}, false\n\t\t}\n\t\treturn slog.DurationValue(v), true\n\t}), // adds an slog.Attr if the given function returns an slog.Value and true\n)\n```\n\nBy default, the slow queries and SQL errors are logged, but you can ignore all SQL messages with `WithIgnoreTrace()`.\n\n```\ncustomLogger := sloggorm.New(\n    slogGorm.WithIgnoreTrace(), // disable the tracing of SQL queries by the logger.\n)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forandin%2Fslog-gorm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forandin%2Fslog-gorm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forandin%2Fslog-gorm/lists"}