{"id":16383683,"url":"https://github.com/ainsleyclark/logger","last_synced_at":"2026-05-18T05:34:59.420Z","repository":{"id":57708642,"uuid":"506691659","full_name":"ainsleyclark/logger","owner":"ainsleyclark","description":"✏️ A Go wrapper for Logrus, Errors, Mongo and Facebook Workplace giving you extremely detailed log reports.","archived":false,"fork":false,"pushed_at":"2022-12-19T07:53:36.000Z","size":499,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-02T06:44:32.713Z","etag":null,"topics":["facebook","go","golang","logger","mongo","workplace"],"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":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-23T15:20:57.000Z","updated_at":"2022-10-22T17:23:08.000Z","dependencies_parsed_at":"2022-09-09T15:50:43.824Z","dependency_job_id":null,"html_url":"https://github.com/ainsleyclark/logger","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Flogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Flogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Flogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ainsleyclark%2Flogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ainsleyclark","download_url":"https://codeload.github.com/ainsleyclark/logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240099815,"owners_count":19747582,"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":["facebook","go","golang","logger","mongo","workplace"],"created_at":"2024-10-11T04:09:25.037Z","updated_at":"2026-05-07T21:30:17.281Z","avatar_url":"https://github.com/ainsleyclark.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003cimg height=\"250\" src=\"res/logo.svg\" alt=\"Logger Logo\" /\u003e\n\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/logger)](https://goreportcard.com/report/github.com/ainsleyclark/logger)\n[![Maintainability](https://api.codeclimate.com/v1/badges/12e933a5f951c21c79a0/maintainability)](https://codeclimate.com/github/ainsleyclark/logger/maintainability)\n[![Test](https://github.com/ainsleyclark/logger/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/ainsleyclark/logger/actions/workflows/test.yml)\n[![codecov](https://codecov.io/gh/ainsleyclark/logger/branch/master/graph/badge.svg?token=K27L8LS7DA)](https://codecov.io/gh/ainsleyclark/logger)\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/logger)\n\n\u003c/div\u003e\n\n# ✏️ Logger\n\nA Go wrapper for Logrus, Errors, Mongo and Facebook Workplace giving you extremely detailed log reports. This package is\ndesigned to be used with [github.com/ainsleyclark/errors](https://github.com/ainsleyclark/errors) for error reporting\nwith codes, messages and more.\n\n## Overview\n\n- ✅ Log a wide variety of log levels.\n- ✅ Logs with custom errors featuring codes, messages and lifelines.\n- ✅ Beautiful middleware and formatters for entries (see screenshots below).\n- ✅ Facebook Workplace integration, any error marked as `INTERNAL` will be sent to WP.\n- ✅ Mongo integration, if specified log entries will be sent to a Mongo collection.\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 errors to a central location, using a Logrus Hook.\n\n![Workplace](res/wp.png)\n\n## Installation\n\n```bash\ngo get -u github.com/ainsleyclark/logger\n```\n\n### Quick Start\n\nGet started with the Logger by calling `logger.New()` and creating new options. The service is required, this is the\nname of your currently running app.\n\n```go\nfunc QuickStart() error {\n\terr := logger.New(context.TODO(), logger.NewOptions().Service(\"service\"))\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tlogger.Trace(\"Trace Entry\")\n\tlogger.Debug(\"Debug Entry\")\n\tlogger.Info(\"Info Entry\")\n\tlogger.Warn(\"Warn Entry\")\n\tlogger.Error(\"Error Entry\")\n\tlogger.WithError(errors.NewInternal(errors.New(\"error\"), \"message\", \"op\")).Error()\n\tlogger.Fatal(\"Fatal Entry\")\n\n\treturn nil\n}\n```\n\n**Outputs:**\n\n![Logger Entries](res/entries.png)\n\n### Fields\n\nFields allow you to log out key value pairs to the logger that will appear under data. The simplest way to use the\nlogger is simply the package-level exported logger.\n\n```go\nfunc Fields() {\n\tlogger.WithFields(types.Fields{\n\t\t\"animal\": \"walrus\",\n\t}).Info(\"A walrus appears\")\n}\n```\n\n## Errors\n\nThis package is designed to work with [github.com/ainsleyclark/errors][https://github.com/ainsleyclark/errors] as such\nthe `WithError` function can be used to log deatiled and rich error data.\n\n```go\nfunc WithError() {\n\tlogger.WithError(errors.NewInternal(errors.New(\"error\"), \"message\", \"op\")).Error()\n}\n```\n\n## Middleware\n\nMiddleware is provided out of the box in the form of a fire hook. Upon receiving a request from the API,\ncalling `logger.Fire`will send the log entry to stdout with detailed request information and meta.\n\n```go\nfunc Middleware(r *http.Request) {\n\tlogger.Fire(logger.FireHook{\n\t\tRequest:      r,\n\t\tStatus:       http.StatusOK,\n\t\tMessage:      \"Message from API\",\n\t\tData:         map[string]any{},\n\t\tRequestTime:  time.Now(),\n\t\tResponseTime: time.Now(),\n\t\tLatency:      100,\n\t})\n}\n```\n\n## Recipes\n\n### Simple\n\nCreates a simple logger with stdout.\n\n```go\nfunc Simple() error {\n\topts := logger.NewOptions().\n\t\tService(\"service\").\n\t\tPrefix(\"prefix\").\n\t\tDefaultStatus(\"status\")\n\n\terr := logger.New(context.Background(), opts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tlogger.Info(\"Hello from Logger!\")\n\n\treturn nil\n}\n```\n\n### WithWorkplace\n\nCreate a logger with Facebook Workplace integration. A token and a thread are required to send any error code that has\nbeen marked as `errors.INTERNAL` to thread ID passed.\n\n```go\nfunc WithWorkplace() error {\n\topts := logger.NewOptions().\n\t\tService(\"api\").\n\t\tWithWorkplaceNotifier(\"token\", \"thread\", nil, nil)\n\n\terr := logger.New(context.Background(), opts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tlogger.Info(\"Hello from Logger!\")\n\n\treturn nil\n}\n```\n\n#### Workplace CallBack\nYou can pass a function to `WithWorkplaceNotifier` as the second argument which is a callback function to determine if\nthe log entry should be sent to a thread, an example is below:\n\n```go\nfunc WithWorkplaceReport() {\n\t// Don't send the message to Workplace if there is no error.\n\tworkplaceCallBack := func(entry types.Entry) bool {\n\t\tif !entry.HasError() {\n\t\t\treturn false\n\t\t}\n\t\treturn true\n\t}\n\n\t_ = logger.NewOptions().\n\tService(\"api\").\n\tWithWorkplaceNotifier(\"token\", \"thread\", workplaceCallBack, nil)\n\n\t// etc\n}\n```\n\n#### Workplace Formatter\nYou can pass a function to `WithWorkplaceNotifier` as the third argument which is a callback function to write the\nmessage to Workplace. This is where you can customise the message easily and return a formatted string.\n\n```go\nfunc WithWorkplaceReport() {\n\t// Format the message with the supplied arguments.\n\tworkplaceCallBack := func(entry types.Entry) bool {\n\t\tif !entry.HasError() {\n\t\t\treturn false\n\t\t}\n\t\treturn true\n\t}\n\n\t_ = logger.NewOptions().\n\tService(\"api\").\n\tWithWorkplaceNotifier(\"token\", \"thread\", workplaceCallBack, nil)\n\n\t// etc\n}\n```\n\n## WithSlack\n\nCreate a logger with Facebook Slack integration. A token and a channel are required to send any error code that has\nbeen marked as `errors.INTERNAL` to thread ID passed.\n\n**Note**\nAll formatting and callbacks are available with Slack. See above for more details.\n\n```go\nfunc WithSlack() error {\n\topts := logger.NewOptions().\n\t\tService(\"api\").\n\t\tWithSlackNotifier(\"token\", \"#channel\", nil, nil)\n\n\terr := logger.New(context.Background(), opts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tlogger.Info(\"Hello from Logger!\")\n\n\treturn nil\n}\n```\n\n### WithMongo\n\nCreate a logger with Mongo integration. All logs are sent to the collection passed\nusing [github.com/ainsleyclark/mogurs](https://github.com/ainsleyclark/mogurs).\n\n```go\nfunc WithMongo() error {\n\tclientOptions := options.Client().\n\t\tApplyURI(os.Getenv(\"MONGO_CONNECTION\")).\n\t\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 := logger.NewOptions().\n\t\tService(\"api\").\n\t\tWithMongoCollection(client.Database(\"logs\").Collection(\"col\"))\n\n\terr = logger.New(context.Background(), opts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tlogger.Info(\"Hello from Logger!\")\n\n\treturn nil\n}\n```\n\n#### Mongo CallBack\nYou can pass a function to `WithWorkplaceNotifier` as the second argument which is a callback function to determine if\nthe log entry should be stored within Mongo, an example is below:\n\n```go\nfunc WithMongoReport() {\n\t// Don't send the message to Mongo if there is no error.\n\tmongoCallBack := func(entry types.Entry) bool {\n\t\tif !entry.HasError() {\n\t\t\treturn false\n\t\t}\n\t\treturn true\n\t}\n\n\tclient, err := mongo.Connect(context.Background(), options.Client().ApplyURI(os.Getenv(\"MONGO_CONNECTION\")))\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\t_ = logger.NewOptions().\n\tService(\"api\").\n\tWithMongoCollection(client.Database(\"logs\").Collection(\"col\"), mongoCallBack)\n\n\t// etc\n}\n```\n\n### KitchenSink\n\nBoostrap all Log integrations.\n\n```go\nfunc KitchenSink() error {\n\tclientOptions := options.Client().\n\t\tApplyURI(os.Getenv(\"MONGO_CONNECTION\")).\n\t\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 := logger.NewOptions().\n\t\tService(\"service\").\n\t\tPrefix(\"prefix\").\n\t\tDefaultStatus(\"status\").\n\t\tWithWorkplaceNotifier(\"token\", \"thread\").\n\t\tWithSlackNotifier(\"token\", \"#channel\").\n\t\tWithMongoCollection(client.Database(\"logs\").Collection(\"col\"))\n\n\terr = logger.New(context.Background(), opts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tlogger.Info(\"Hello from Logger!\")\n\n\treturn nil\n}\n```\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 Errors. Code released under the [MIT Licence](LICENSE).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fainsleyclark%2Flogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fainsleyclark%2Flogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fainsleyclark%2Flogger/lists"}