{"id":14969933,"url":"https://github.com/ainsleyclark/mogrus","last_synced_at":"2026-02-11T22:37:05.312Z","repository":{"id":38262748,"uuid":"494049709","full_name":"ainsleyclark/mogrus","owner":"ainsleyclark","description":"✏️ A Go wrapper for Logrus, Errors and Mongo giving you extremley detailed log reports.","archived":false,"fork":false,"pushed_at":"2022-10-07T12:58:15.000Z","size":70,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-31T18:28:38.875Z","etag":null,"topics":["go","golang","log","logging","logrus","mogrus","mongo"],"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/ainsleyclark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["ainsleyclark"]}},"created_at":"2022-05-19T11:50:33.000Z","updated_at":"2023-08-06T06:02:19.000Z","dependencies_parsed_at":"2022-08-25T20:22:06.853Z","dependency_job_id":null,"html_url":"https://github.com/ainsleyclark/mogrus","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Fmogrus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Fmogrus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Fmogrus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Fmogrus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ainsleyclark","download_url":"https://codeload.github.com/ainsleyclark/mogrus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238310345,"owners_count":19450842,"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":["go","golang","log","logging","logrus","mogrus","mongo"],"created_at":"2024-09-24T13:42:42.890Z","updated_at":"2025-10-26T10:31:30.077Z","avatar_url":"https://github.com/ainsleyclark.png","language":"Go","funding_links":["https://github.com/sponsors/ainsleyclark"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003cimg height=\"250\" src=\"res/logo.svg?size=new\" alt=\"Errors Logo\" /\u003e\n\t\n[![made-with-Go](https://img.shields.io/badge/Made%20with-Go-1f425f.svg)](http://golang.org)\n[![Go Report Card](https://goreportcard.com/badge/github.com/ainsleyclark/mogrus)](https://goreportcard.com/report/github.com/ainsleyclark/mogrus)\n[![Maintainability](https://api.codeclimate.com/v1/badges/b3afd7bf115341995077/maintainability)](https://codeclimate.com/github/ainsleyclark/mogrus/maintainability)\n[![Test](https://github.com/ainsleyclark/mogrus/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/ainsleyclark/mogrus/actions/workflows/test.yml)\n[![codecov](https://codecov.io/gh/ainsleyclark/mogrus/branch/master/graph/badge.svg?token=K27L8LS7DA)](https://codecov.io/gh/ainsleyclark/mogrus)\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat)](https://pkg.go.dev/github.com/ainsleyclark/mogrus)\n\t\n\u003c/div\u003e\n\n# ✏️ Mogrus\n\nA Go wrapper for Logrus, Errors and Mongo giving you extremely detailed log reports. This package is designed to be used\nwith [github.com/ainsleyclark/errors](https://github.com/ainsleyclark/errors) for error reporting with codes, messages and more.\n\n## Overview\n\n- ✅ Add hooks to a Mongo collection.\n- ✅ Logs with custom errors featuring codes, messages and lifelines.\n- ✅ Customisable expiry times for different Logrus levels.\n- ✅ Specify a callback function for when an entry is fired to Mongo.\n\n## Why?\n\nDetailed and verbose logging is important to any application or API. This package aims to make it easy for APIs to log\nerrors to a central location, using a Logrus Hook.\n\n## Installation\n\n```bash\ngo get -u github.com/ainsleyclark/mogrus\n```\n\n## How to use\n\nBelow is an example of how to use Mogrus, instantiate a Logrus instance and connect to a Mongo DB and pass the hook to\nthe `AddHook()` function.\n\n### Add hook\n\n```go\nfunc ExampleMogrus() {\n\tl := logrus.New()\n\n\tclientOptions := options.Client().\n\tApplyURI(os.Getenv(\"MONGO_CONNECTION\")).\n\tSetServerAPIOptions(options.ServerAPI(options.ServerAPIVersion1))\n\n\tclient, err := mongo.Connect(context.Background(), clientOptions)\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\topts := mogrus.Options{\n\t\tCollection: client.Database(\"logs\").Collection(\"col\"),\n\t\t// FireHook is a hook function called just before an\n\t\t// entry is logged to Mongo.\n\t\tFireHook: func(e mogrus.Entry) {\n\t\t\tfmt.Printf(\"%+v\\n\", e)\n\t\t},\n\t\t// ExpirationLevels allows for the customisation of expiry\n\t\t// time for each Logrus level by default entries do not expire.\n\t\tExpirationLevels: mogrus.ExpirationLevels{\n\t\t\tlogrus.DebugLevel: time.Second * 5,\n\t\t\tlogrus.InfoLevel:  time.Second * 15,\n\t\t\tlogrus.ErrorLevel: time.Second * 30,\n\t\t},\n\t}\n\n\t// Create the new Mogrus hook, returns an error if the\n\t// collection is nil.\n\thook, err := mogrus.New(context.Background(), opts)\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\t// Add the hook to the Logrus instance.\n\tl.AddHook(hook)\n\n\tl.Debug(\"Debug level\")\n\tl.WithField(\"key\", \"value\").Info(\"Info level\")\n\tl.WithError(errors.NewInternal(errors.New(\"error\"), \"message\", \"op\")).Error(\"Error level\")\n}\n```\n\n### Entry\n\nThe definition of an entry below is the object that is fired to Mongo.\n\n```go\n// Entry defines a singular entry sent to Mongo\n// when a Logrus event is fired.\nEntry struct {\n\tLevel   string               `json:\"level\" bson:\"level\"`\n\tTime    time.Time            `json:\"time\" bson:\"time\"`\n\tMessage string               `json:\"message\" bson:\"message\"`\n\tData    map[string]any       `json:\"data\" bson:\"data\"`\n\tError   *Error               `json:\"error\" bson:\"error\"`\n\tExpiry  map[string]time.Time `json:\"expiry\" bson:\"expiry\"`\n}\n```\n\n### Error\n\nThe definition of an error below is the object that is stored in an Entry when `log.WithError` is used.\n\n```go\n// Error defines a custom Error for log entries, detailing\n// the file line, errors are returned as strings instead\n// of the stdlib error.\nError struct {\n\tCode      string `json:\"code\" bson:\"code\"`\n\tMessage   string `json:\"message\" bson:\"message\"`\n\tOperation string `json:\"operation\" bson:\"op\"`\n\tErr       string `json:\"error\" bson:\"err\"`\n\tFileLine  string `json:\"file_line\" bson:\"file_line\"`\n}\n```\n\n## TODO\n\n- Add global expiration time for all log levels.\n- Add constructors for:\n  - `WithDB()`\n  - `WithAuth()`\n  - `New()`\n\n## Contributing\n\nPlease feel free to make a pull request if you think something should be added to this package!\n\n## Credits\n\nShout out to the incredible [Maria Letta](https://github.com/MariaLetta) for her excellent Gopher illustrations.\n\n## Licence\n\nCode Copyright 2022 Mogrus. Code released under the [MIT Licence](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fainsleyclark%2Fmogrus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fainsleyclark%2Fmogrus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fainsleyclark%2Fmogrus/lists"}