{"id":17162565,"url":"https://github.com/chanxuehong/log","last_synced_at":"2025-04-13T14:12:32.555Z","repository":{"id":57484009,"uuid":"129884382","full_name":"chanxuehong/log","owner":"chanxuehong","description":"a simple, flexible, leveled logging package for Go/golang.","archived":false,"fork":false,"pushed_at":"2021-10-09T06:29:40.000Z","size":102,"stargazers_count":10,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T05:13:15.424Z","etag":null,"topics":["go","golang","log","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chanxuehong.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}},"created_at":"2018-04-17T10:01:14.000Z","updated_at":"2022-12-04T16:45:05.000Z","dependencies_parsed_at":"2022-08-28T19:30:26.875Z","dependency_job_id":null,"html_url":"https://github.com/chanxuehong/log","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/chanxuehong%2Flog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanxuehong%2Flog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanxuehong%2Flog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanxuehong%2Flog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chanxuehong","download_url":"https://codeload.github.com/chanxuehong/log/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248724629,"owners_count":21151561,"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","logger","logging"],"created_at":"2024-10-14T22:46:04.158Z","updated_at":"2025-04-13T14:12:32.533Z","avatar_url":"https://github.com/chanxuehong.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## log\n\n```golang\npackage main\n\nimport (\n\t\"context\"\n\t\"net/http\"\n\n\t\"github.com/chanxuehong/log\"\n\t\"github.com/chanxuehong/log/trace\"\n)\n\n// set defaults if necessary.\n// normally sets when the program starts.\nfunc init() {\n\tlog.SetFormatter(log.TextFormatter)\n\tlog.SetLevelString(log.DebugLevelString)\n\tlog.SetDefaultOptions([]log.Option{\n\t\tlog.WithFormatter(log.TextFormatter),\n\t\tlog.WithLevelString(log.DebugLevelString),\n\t})\n}\n\ntype mockResponseWriter struct {\n\thttp.ResponseWriter\n\theader http.Header\n}\n\nfunc (w *mockResponseWriter) Header() http.Header {\n\treturn w.header\n}\n\nfunc main() {\n\t// mock http.ResponseWriter and *http.Request\n\tw := \u0026mockResponseWriter{\n\t\theader: make(http.Header),\n\t}\n\tr, _ := http.NewRequest(http.MethodGet, \"http://www.example.com\", nil)\n\n\t// handle http request\n\tmiddleware(handler)(w, r)\n}\n\nfunc middleware(next http.HandlerFunc) http.HandlerFunc {\n\treturn func(w http.ResponseWriter, r *http.Request) {\n\t\t// gets traceId from request header, if not exists, generates it.\n\t\ttraceId, ok := trace.FromRequest(r)\n\t\tif !ok {\n\t\t\ttraceId = trace.NewTraceId()\n\t\t\tr.Header.Set(trace.TraceIdHeaderKey, traceId)\n\t\t}\n\t\t// sets traceId to response header\n\t\tw.Header().Set(trace.TraceIdHeaderKey, traceId)\n\n\t\t// adds traceId to request.Context\n\t\tr = r.WithContext(trace.NewContext(r.Context(), traceId))\n\n\t\t// adds log.Logger to request.Context\n\t\tl := log.New(log.WithTraceId(traceId))\n\t\tr = log.NewRequest(r, l)\n\n\t\t// call http handler\n\t\tnext(w, r)\n\t}\n}\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n\tl := log.MustFromRequest(r)\n\tl.Info(\"1.info message\")\n\tl.Info(\"2.info message\", \"key1\", 1, \"key2\", 2)\n\n\tl = l.WithField(\"key3\", 3)\n\tl = l.WithFields(\"key4\", 4, \"key5\", 5)\n\tl.Info(\"3.info message\")\n\n\tctx := log.NewContext(r.Context(), l)\n\tfn1(ctx)\n}\n\nfunc fn1(ctx context.Context) {\n\tl := log.MustFromContext(ctx)\n\tl.Info(\"4.info message\")\n\tl.Info(\"5.info message\", \"key6\", 6)\n\n\tl = l.WithField(\"key7\", 7)\n\tctx = log.NewContext(ctx, l)\n\tfn2(ctx)\n}\n\nfunc fn2(ctx context.Context) {\n\tlog.InfoContext(ctx, \"6.info message\", \"key8\", 8) // shortcut\n\tfn3(ctx)\n}\n\nfunc fn3(ctx context.Context) {\n\tlog.InfoContext(ctx, \"7.info message\", \"key9\", 9) // shortcut\n}\n```\n\n```Text\ntime=2018-07-01 15:49:04.520, level=info, request_id=39eff2b97d0311e89fcd000c294d93c4, location=main.handler(test1/main.go:67), msg=1.info message\ntime=2018-07-01 15:49:04.520, level=info, request_id=39eff2b97d0311e89fcd000c294d93c4, location=main.handler(test1/main.go:68), msg=2.info message, key1=1, key2=2\ntime=2018-07-01 15:49:04.520, level=info, request_id=39eff2b97d0311e89fcd000c294d93c4, location=main.handler(test1/main.go:72), msg=3.info message, key3=3, key4=4, key5=5\ntime=2018-07-01 15:49:04.520, level=info, request_id=39eff2b97d0311e89fcd000c294d93c4, location=main.fn1(test1/main.go:80), msg=4.info message, key3=3, key4=4, key5=5\ntime=2018-07-01 15:49:04.520, level=info, request_id=39eff2b97d0311e89fcd000c294d93c4, location=main.fn1(test1/main.go:81), msg=5.info message, key3=3, key4=4, key5=5, key6=6\ntime=2018-07-01 15:49:04.520, level=info, request_id=39eff2b97d0311e89fcd000c294d93c4, location=main.fn2(test1/main.go:89), msg=6.info message, key3=3, key4=4, key5=5, key7=7, key8=8\ntime=2018-07-01 15:49:04.520, level=info, request_id=39eff2b97d0311e89fcd000c294d93c4, location=main.fn3(test1/main.go:94), msg=7.info message, key3=3, key4=4, key5=5, key7=7, key9=9\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanxuehong%2Flog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchanxuehong%2Flog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanxuehong%2Flog/lists"}