{"id":21961510,"url":"https://github.com/saulortega/gcl","last_synced_at":"2026-05-11T04:35:11.159Z","repository":{"id":90327527,"uuid":"412680521","full_name":"saulortega/gcl","owner":"saulortega","description":"Logger for Google Cloud Logging","archived":false,"fork":false,"pushed_at":"2021-10-20T02:10:04.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-28T00:26:59.893Z","etag":null,"topics":["gcloud","gcp","logger","logging"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/saulortega.png","metadata":{"files":{"readme":"README.en.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}},"created_at":"2021-10-02T03:34:11.000Z","updated_at":"2022-12-22T01:40:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"ac3e5363-bac5-49bd-be20-0c93f7b62461","html_url":"https://github.com/saulortega/gcl","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/saulortega%2Fgcl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saulortega%2Fgcl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saulortega%2Fgcl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saulortega%2Fgcl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/saulortega","download_url":"https://codeload.github.com/saulortega/gcl/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245016679,"owners_count":20547652,"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":["gcloud","gcp","logger","logging"],"created_at":"2024-11-29T10:16:07.685Z","updated_at":"2026-05-11T04:35:06.130Z","avatar_url":"https://github.com/saulortega.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gcl\n\nThis library allows to generate logs compatible with the Google Cloud Logging API that can be used in code implemented in Cloud Functions, Cloud Run, or Kubernetes.\n\n## Methods\n```go\n// error cause.\ngcl.Cause(err)\n\n// Custom fields with arbitrary values.\n// Will appear in the entry of Cloud Logging in «jsonPayload».\ngcl.Field(\"user\", 573298)\ngcl.Field(\"role\", \"supervisor\")\n\n// Custom fields for labels.\n// Will appear in the entry of Cloud Logging in «labels».\ngcl.Label(\"app\", \"my-app\")\n\n// Description of the error with severity.\n// This must be a message suitable for be replied to the final user\n// when it is used in HTTP requests.\ngcl.Debug(\"oops\")\ngcl.Info(\"oops\")\ngcl.Notice(\"oops\")\ngcl.Warning(\"oops\")\ngcl.Error(\"oops\")\ngcl.Critical(\"oops\")\ngcl.Alert(\"oops\")\ngcl.Emergency(\"oops\")\n\n// Set HTTP status code and data from HTTP request.\ngcl.HTTPStatus(http.StatusNotFound)\ngcl.HTTPRequest(r)\n\n// All the above methods can be chained:\ngcl.Cause(err).Field(\"user\", 573298).Warning(\"user not found\").HTTPStatus(http.StatusNotFound)\n\n// The above methods initialize the entry of log, but does not write the log.\n// To write the log to stdout, use Log():\ngcl.Cause(err).Field(\"user\", 573298).Warning(\"user not found\").HTTPStatus(http.StatusNotFound).Log()\n\n// To return an error and handle it later, use Err():\ngcl.Cause(err).Field(\"user\", 573298).Warning(\"user not found\").HTTPStatus(http.StatusNotFound).Err()\n\n```\n\n## Error handling in HTTP requests:\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/saulortega/gcl\"\n)\n\nfunc main() {\n\thttp.HandleFunc(\"/users\", users)\n\tlog.Fatal(http.ListenAndServe(\":80\", nil))\n}\n\nfunc users(w http.ResponseWriter, r *http.Request) {\n\tdata, err := getUser(r.FormValue(\"user\"))\n\tif err != nil {\n\t\tres, cod := handleError(r, err)\n\t\thttp.Error(w, res, cod)\n\t\treturn\n\t}\n\n\tfmt.Fprint(w, data)\n}\n\nfunc getUser(u string) ([]byte, error) {\n\tif u == \"\" {\n\t\treturn nil, gcl.Notice(\"empty user\").HTTPStatus(http.StatusBadRequest).Err()\n\t}\n\n\tvar user = struct {\n\t\tname string\n\t}{}\n\n\terr := db.QueryRow(\"SELECT name FROM users WHERE id = $1\", u).Scan(\u0026user)\n\tif err == sql.ErrNoRows {\n\t\treturn nil, gcl.Cause(err).Notice(\"user not found\").HTTPStatus(http.StatusNotFound).Err()\n\t} else if err != nil {\n\t\treturn nil, gcl.Cause(err).Field(\"user\", u).Error(\"something went wrong\").HTTPStatus(http.StatusInternalServerError).Err()\n\t}\n\n\tb, err := json.Marshal(user)\n\tif err != nil {\n\t\treturn nil, gcl.Cause(err).Field(\"user\", u).Error(\"something went wrong\").HTTPStatus(http.StatusInternalServerError).Err()\n\t}\n\n\treturn b, nil\n}\n\nfunc handleError(r *http.Request, e error) (string, int) {\n\t// An *gcl.Err is expected. If not, this parameters will be used:\n\tmsg := \"something went wrong\"\n\tcod := http.StatusInternalServerError\n\tres := fmt.Sprintf(`{\"error\":\"%s\"}`, msg)\n\n\terr, ok := e.(*gcl.Err)\n\tif ok {\n\t\t// Returns the code previously setted, or 500 if not was setted.\n\t\tcod = err.HTTPStatus()\n\n\t\t// Returns the setted message in one of the eight functions with severity (Debug, Info, Error, etc.), or msg if not was setted.\n\t\tmsg = err.Message(msg)\n\n\t\tres = fmt.Sprintf(`{\"error\":\"%s\"}`, msg)\n\n\t\t// Set HTTP Request data:\n\t\terr.HTTPRequest(r)\n\n\t\t// Write log to stdout:\n\t\terr.Log()\n\t}\n\n\treturn res, cod\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaulortega%2Fgcl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaulortega%2Fgcl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaulortega%2Fgcl/lists"}