{"id":21436755,"url":"https://github.com/modfin/eal","last_synced_at":"2025-04-19T17:51:52.637Z","repository":{"id":44631732,"uuid":"426319276","full_name":"modfin/eal","owner":"modfin","description":"Extended Access Logging","archived":false,"fork":false,"pushed_at":"2025-04-17T04:37:07.000Z","size":42,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-17T18:41:39.124Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/modfin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-11-09T17:14:09.000Z","updated_at":"2025-04-17T04:37:04.000Z","dependencies_parsed_at":"2024-01-09T09:43:03.983Z","dependency_job_id":"2f33560b-54f6-4365-9a29-c2e45f9d3e60","html_url":"https://github.com/modfin/eal","commit_stats":{"total_commits":19,"total_committers":4,"mean_commits":4.75,"dds":0.5789473684210527,"last_synced_commit":"2fda7cdaa2b406f9f8829650510f1a69961654dc"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Feal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Feal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Feal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Feal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/modfin","download_url":"https://codeload.github.com/modfin/eal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249753088,"owners_count":21320664,"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":[],"created_at":"2024-11-23T00:15:08.489Z","updated_at":"2025-04-19T17:51:52.620Z","avatar_url":"https://github.com/modfin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# eal\nExtended Access Logging\n\u003e Simplifies access and error logging of Labstack/echo HTTP servers\n\n## Setup echo access/error logging\nTo get started with access and error logging, it's enough to call `eal.Init`, `eal.InitDefaultErrorLogging` (both are optional),\nand then add the middleware returned by `eal.CreateLoggerMiddleware()` to the echo server.\n\n```go\npackage main\n\nimport (\n  \"github.com/labstack/echo/v4\"\n  \"github.com/modfin/eal\"\n)\n\nfunc main() {\n  // Initialize logrus JSON logger.\n  eal.Init(false)\n\n  // Initialize eal default error logging for echo.HTTPError and jwt.ValidationError error types.\n  eal.InitDefaultErrorLogging()\n\n  // Create echo instance and set up the access logging middleware.\n  e := echo.New()\n  e.Use(eal.CreateLoggerMiddleware())\n\n  // Setup endpoints and start echo server the usual way...\n  // ...\n```\n\n## Add information to access/error log entry\nTo extend the log entry that is going to be written when the endpoint is about to return, one can use the `AddContextFields` method.\n```go\n  e.POST(\"/user\", func(c echo.Context) error {\n    userID := c.FormValue(\"user-id\")\n\n    // Add \"user-id\" field to context, that will be included in the log entry generated by the middleware when\n    // handler have returned.\n    eal.AddContextFields(c, Fields{\"user-id\": userID})\n\n    // ...\n  })\n```\n\n## Add stacktrace information to logged errors\nTo generate a stacktrace, the `Trace` method can be used. `Trace` takes an error and wrap it in a new error that contain a stacktrace. \nIt is possible to configure what errors and error types that shouldn't generate a stacktrace (see `InhibitStacktraceForError` for more information). \nIf the error provided to `Trace` already is, or contain, a wrapped stacktrace-error, the original error will be returned unmodified.\n\nThere is a global parameter that can be set that affect when the stacktrace is first logged: `LogCallStackDirectly`. If it's\n`true`, `Trace` will write a new log entry directly after the new stacktrace error have been created. This can be useful if there is a chance\nthat the error returned by `Trace` isn't wrapped and returned to the middleware logger.\n\n```go\n  if err != nil {\n    // Wrap the original error in a stacktrace, before wrapping it in a new error with more information (GO 1.13 and later)\n    return fmt.Errorf(\"encode: %v: %w\", data, eal.Trace(err))\n  }\n```\n\n## Add more error information to the log event\nSome error types may have more information than what's shown in the `Error()` string, or if it's desirable to have some error information\nlogged as a separate field in the log. The `RegisterErrorLogFunc` method can be used to extend the log entry with specific error information.\n\nSee `InitDefaultErrorLogging()` for an example of how to use `RegisterErrorLogFunc`.\n\n## Send Error information to caller\nNormally echo will send back a HTTP status 500 when an error is returned from the echo handlerFunc, unless the error is a echo.HTTPError.\nWhen the `eal.CreateLoggerMiddleware` is used, it will look for the earliest echo.HTTPError if can find in the returned error, and return\nthat to echo, if the returned error don't contain a wrapped echoHTTPError, the error will be passed on to echo unmodified.\n\n```go\nvar errNope error = echo.NewHTTPError(http.StatusNotFound, \"Nope\") // Returns 404 {\"message\":\"Nope\"}, to caller\n\n...\n\n  e.GET(\"/droids\", func(c echo.Context) error {\n    return errNope\n  })\n\n```\n\nor if the error information that we want to send back is caused by an error, eal implement a `NewHTTPError` method that wrap an error in a\necho.HTTPError\n\n\n```go\nfunc errNope(err error) error {\n  // Wrap the error in a stacktrace, and then wrap it in a echo.HTTPError\n  return eal.NewHTTPError(eal.Trace(err), http.StatusNotFound, \"Nope\") // Return 404 {\"message\":\"Nope\"}, to caller\n}\n\n...\n\n  e.GET(\"/droids\", func(c echo.Context) error {\n    d, err := getDroids()\n    if err != nil {\n      return errNope(err)\n    }\n    return c.JSON(http.StatusOK, d)\n  })\n\n```\n\nit's also possible to send back a custom JSON message to the caller by using a struct as a parameter in the echo.HTTPError\n\n```go\ntype ErrorMessage struct {\n  ErrorCode    int    `json:\"error_code\"`\n  ErrorMessage string `json:\"error_message\"`\n}\n\nvar ErrSomeMessage error = echo.NewHTTPError(http.StatusNotFound, \u0026ErrorMessage{ErrorCode: 42, ErrorMessage: \"common.error.some_message\"})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodfin%2Feal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmodfin%2Feal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodfin%2Feal/lists"}