{"id":41030293,"url":"https://github.com/mdanialr/api-pkg-go","last_synced_at":"2026-01-22T10:12:55.879Z","repository":{"id":189385045,"uuid":"680504608","full_name":"mdanialr/api-pkg-go","owner":"mdanialr","description":"Useful collection of reuseable packages for Go","archived":false,"fork":false,"pushed_at":"2024-07-30T01:34:46.000Z","size":201,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-30T05:55:30.899Z","etag":null,"topics":["go","golang","library","logger","pkg"],"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/mdanialr.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}},"created_at":"2023-08-19T13:11:23.000Z","updated_at":"2024-07-30T01:34:48.000Z","dependencies_parsed_at":"2024-07-24T03:52:57.703Z","dependency_job_id":null,"html_url":"https://github.com/mdanialr/api-pkg-go","commit_stats":null,"previous_names":["mdanialr/api-pkg-go"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/mdanialr/api-pkg-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdanialr%2Fapi-pkg-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdanialr%2Fapi-pkg-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdanialr%2Fapi-pkg-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdanialr%2Fapi-pkg-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdanialr","download_url":"https://codeload.github.com/mdanialr/api-pkg-go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdanialr%2Fapi-pkg-go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28661270,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T01:17:37.254Z","status":"online","status_checked_at":"2026-01-22T02:00:07.137Z","response_time":144,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","library","logger","pkg"],"created_at":"2026-01-22T10:12:55.163Z","updated_at":"2026-01-22T10:12:55.874Z","avatar_url":"https://github.com/mdanialr.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Programming Language](https://img.shields.io/badge/language-GO-blue.svg)](https://shields.io/)\n[![Go version](https://img.shields.io/badge/Go-v1.21-blue)](https://img.shields.io/)\n[![CI Status](https://github.com/mdanialr/api-pkg-go/workflows/CI/badge.svg)](https://github.com/mdanialr/api-pkg-go/actions/workflows/on_push_pr.yml)\n[![Code Coverage](https://github.com/mdanialr/api-pkg-go/wiki/coverage.svg)](https://github.com/mdanialr/api-pkg-go/wiki/coverage.svg)\n\n# API Pkg Go\nUseful collection of reusable packages for Go\n\n- `Log`: logging pkg that support write logs to multiple output target such as `console`, `file` (with logrotate), `newrelic` and `platform log` at the same time.\n\n## Log\nThere are two main parts in `log` which are __Logger__ and __Writer__. `frontend` is the API provided by `Logger` interface and `backend` is any pkg/lib that implement `Logger`\n\n- __Logger__: Main actor that will decide where, how and whether it should write the logs or not based on the log level defined in each `Writer`.\n  You may call this as the `frontend`, since you will and should only interact with the provided API from `Logger` interface.\n- __Writer__: Decide where the logs passed from `Logger` should be written to. Is it to terminal, file or whatever this `Writer` will decide that.\n  We already provide pre-defined `Writer` implementer namely `console`, `file`, `newrelic`, `platform`.\n\nFor now, we can support two `backend` which are [zap](https://github.com/uber-go/zap) \u0026 [slog](https://pkg.go.dev/golang.org/x/exp/slog).\nUse `NewZapLogger` to use `zap` as the logger backend or `NewSlogLogger` to use `slog` instead.\n\n### Getting Started\n```go\n// set console/terminal writer to listen to all logs level DEBUG, INFO, WARNING, ERROR\n//  console writer, write logs to local terminal/console\ncns := log.NewConsoleWriter(log.DebugLevel)\t\n\n// use predefined zap as the backend\nwr := log.NewZapLogger(cns)\n//  or use this if you want to use slog under the hood instead\n//    wr := logger.NewSlogLogger(cns)\n\n// call Init before using any other API\nwr.Init(3 * time.Second) // you may give longer or shorter timeout/deadline\n\n// info level log message that include contextual data 'hello':'world'\nwr.Inf(\"INFO message\", log.String(\"hello\", \"world\"))\n//  terminal: 2023-09-22T13:38:39.784+0700    INFO    INFO message    {\"hello\": \"world\"}\n//  json: {\"level\":\"INFO\",\"time\":\"2023-09-22T13:38:39.784+0700\",\"msg\":\"INFO message\",\"hello\":\"world\"}\n\n// debug level log message\nwr.Dbg(\"DEBUG message\")\n//  terminal: 2023-09-22T13:38:39.784+0700    DEBUG   DEBUG message\n//  json: {\"level\":\"DEBUG\",\"time\":\"2023-09-22T13:38:39.784+0700\",\"msg\":\"DEBUG message\"}\n\n// SHOULD be called before program exit to make sure any pending logs in buffer properly flushed by each Writer\nwr.Flush(2 * time.Second) // you may give longer or shorter timeout/deadline\n```\n\n### Contextual Data\n```go\n// give contextual data that will be passed down to subsequent call\nwr = wr.With(log.String(\"app_env\", \"local\"))\nwr.Wrn(\"warning log\")\n//  terminal: 2023-09-22T13:38:39.784+0700    WARN    warning log     {\"app_env\": \"local\"}\n//  json: {\"level\":\"WARN\",\"time\":\"2023-09-22T13:38:39.784+0700\",\"msg\":\"warning log\",\"app_env\":\"local\"}\n\nwr = wr.With(log.Num(\"ram\", 2)) // this will also accumulate previous contextual data\nwr.Inf(\"look how many ram i have\")\n//  terminal: 2023-09-22T13:38:39.784+0700    INFO    look how many ram i have        {\"app_env\": \"local\", \"ram\": 2}\n//  json: {\"level\":\"INFO\",\"time\":\"2023-09-22T13:38:39.784+0700\",\"msg\":\"look how many ram i have\",\"app_env\":\"local\",\"ram\":2}\n```\n**Note**: if the contextual data **should** change in each function call, then make sure to create new variable in each\n`.With()` call.\n\nExample\n```go\nfunc writeGoodLog(wr log.Logger) {\n    // create new variable instead of replacing the old wr variable\n    newWr := wr.With(\n        log.String(\"x-request-id\", uuid.NewString()),\n    )\n\n    newWr.Inf(\"info message\")\n}\n\nfunc writeBadLog(wr log.Logger) {\n    wr = wr.With(\n        log.String(\"x-request-id\", uuid.NewString()),\n    )\n\t\n    wr.Inf(\"info message\")\n}\n\nfunc main() {\n    // setup myLog that's type of log.Logger\n    myLog\n\n    writeBadLog(myLog)\n    // output: {\"msg\":\"info message\", \"x-request-id\": \"c684f881-07a5-45e6-97fd-cb2af8ad7c4e\"}\n    writeBadLog(myLog)\n    // output: {\"msg\":\"info message\", \"x-request-id\": \"c684f881-07a5-45e6-97fd-cb2af8ad7c4e\"}\n\t\n    // NOTE that each call on writeBadLog they will generate exactly same x-request-id, this is because the children\n    //  and parent always affecting each other, if the children generate new x-request-id then the parent will also\n    //   change their x-request-id and make that contextual data duplicate\n\n\t\n    writeGoodLog(myLog)\n    // output: {\"msg\":\"info message\", \"x-request-id\": \"4014d36a-8f34-4b26-b91a-12480605033d\"}\n    writeGoodLog(myLog)\n    // output: {\"msg\":\"info message\", \"x-request-id\": \"2935ee81-a4a3-4586-b7f0-95d26473a5ac\"}\n\t\n    // This time the x-request-id will always generate new string without affecting the log.Logger from param, because\n    //  it always generates new log.Logger on each writeGoodLog() instead of replace and reusing log.Logger that come from param\n}\n```\n\n### Leveled Log\n```go\ncns := log.NewConsoleWriter(log.ErrorLevel)\nwr := log.NewZapLogger(cns)\nwr.Init(3 * time.Second)\n\n// won't print, less than error\nwr.Dbg(\"DEBUG message\")\n\n// won't print, less than error\nwr.Inf(\"INFO message\")\n\n// won't print, less than error\nwr.Wrn(\"warning log\")\n\n// printed, higher or equal than error\nwr.Err(\"oops!!\")\n//  terminal: 2023-09-22T13:38:39.784+0700    ERROR   oops!!\n//  json: {\"level\":\"ERROR\",\"time\":\"2023-09-22T13:38:39.784+0700\",\"msg\":\"oops!!\"}\n\n// never forget to flush before exit\nwr.Flush(1 * time.Second)\n```\nLog is prioritized in these order:\n1. Error `Err`: (Error) print only in log level Error\n2. Warning `Wrn`: (Warning, Error) print in log level Warning, Error\n3. Info `Inf`: (Info, Warning, Error) print in log level Info, Warning, Error\n4. Debug `Dbg`: (Debug, Info, Warning, Error) print in all log level\n\n### Logger with Context\n```go\n// put the logger wr to context with 'log.WithCtx'\nctx := log.WithCtx(context.Background(), wr)\nprintFromCtx(ctx) // pass logger contained context\n\nfunc printFromCtx(ctx context.Context) {\n    // grab logger from context\n    wr := log.FromCtx(ctx)\n    // note that even if there is no logger inside context\n    // this won't cause any panic and just return nop-logger\n    // that will never write any logs\n\t\n    wr.Inf(\"my information\")\n    //  terminal: 2023-09-22T13:38:39.784+0700    INFO    my information\n    //  json: {\"level\":\"INFO\",\"time\":\"2023-09-22T13:38:39.784+0700\",\"msg\":\"my information\"}\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdanialr%2Fapi-pkg-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdanialr%2Fapi-pkg-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdanialr%2Fapi-pkg-go/lists"}