{"id":40819079,"url":"https://github.com/trinhdaiphuc/logger","last_synced_at":"2026-01-21T21:44:57.130Z","repository":{"id":38197798,"uuid":"418055476","full_name":"trinhdaiphuc/logger","owner":"trinhdaiphuc","description":"logger attached in request context, log showed as steps and in only one line for every request.","archived":false,"fork":false,"pushed_at":"2022-08-27T03:24:31.000Z","size":73,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-15T22:40:43.037Z","etag":null,"topics":["echo-framework","fiber-framework","gin","gin-gonic","go","golang","grpc-go","grpc-middleware","logger-middleware","logging","logrus"],"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/trinhdaiphuc.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":"2021-10-17T07:36:16.000Z","updated_at":"2022-07-07T03:14:34.000Z","dependencies_parsed_at":"2023-01-16T18:01:11.185Z","dependency_job_id":null,"html_url":"https://github.com/trinhdaiphuc/logger","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/trinhdaiphuc/logger","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trinhdaiphuc%2Flogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trinhdaiphuc%2Flogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trinhdaiphuc%2Flogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trinhdaiphuc%2Flogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trinhdaiphuc","download_url":"https://codeload.github.com/trinhdaiphuc/logger/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trinhdaiphuc%2Flogger/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28644149,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T21:29:11.980Z","status":"ssl_error","status_checked_at":"2026-01-21T21:24:31.872Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["echo-framework","fiber-framework","gin","gin-gonic","go","golang","grpc-go","grpc-middleware","logger-middleware","logging","logrus"],"created_at":"2026-01-21T21:44:56.396Z","updated_at":"2026-01-21T21:44:57.117Z","avatar_url":"https://github.com/trinhdaiphuc.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Logger\n\n## Introduce:\n\nA wrapper [Logrus](https://github.com/sirupsen/logrus) library which make logger attached in request context, log should\nbe showed as steps and in only one line for every request.\n\nSupport Logger middleware for [Echo](https://echo.labstack.com/), [Fiber](https://gofiber.io/)\n, [Gin](https://github.com/gin-gonic/gin) and [GRPC](https://grpc.io/docs/languages/go/basics/) framework. You can make\nyour own Logger middleware by adding your logger into request context and getting it by `logger.GetLogger(ctx)`\nfunction. You can follow my middlewares and create yours.\n\n## Usages:\n\n**Install the package:**\n\n```shell\ngo get -u github.com/trinhdaiphuc/logger\n```\n\n### Echo\n\nExample code:\n\n```go\npackage main\n\nimport (\n\t\"github.com/labstack/echo/v4\"\n\t\"github.com/trinhdaiphuc/logger\"\n)\n\nfunc main() {\n\tserver := echo.New()\n\tserver.Use(logger.EchoMiddleware(logger.ConfigEcho{\n\t\tSkipperEcho: func(context echo.Context) bool {\n\t\t\tif context.Request().RequestURI == \"/metrics\" {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\treturn false\n\t\t},\n\t}))\n\n\tserver.GET(\"/hello/:name\", func(ctx echo.Context) error {\n\t\tlog := logger.GetLogger(ctx.Request().Context())\n\t\tname := ctx.Param(\"name\")\n\t\tlog.AddLog(\"request name %v\", name)\n\t\treturn ctx.String(200, \"Hello \"+name)\n\t})\n\n\tif err := server.Start(\":8080\"); err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\nTry logger with request:\n\n```shell\nfor i in {0..5}; do curl :8080/hello/user-${i}; done \n```\n\nLogger output:\n\n```shell\n    ____    __\n  / __/___/ /  ___\n / _// __/ _ \\/ _ \\\n/___/\\__/_//_/\\___/ v4.6.1\nHigh performance, minimalist Go web framework\nhttps://echo.labstack.com\n____________________________________O/_______\n                                    O\\\n⇨ http server started on [::]:8080\n{\"STEP_1\":\"request name user-0\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:27:10.717449+07:00\",\"level\":\"info\",\"msg\":\"latency: 25.637µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:27:10+07:00\",\"uri\":\"/hello/user-0\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-1\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:27:10.728884+07:00\",\"level\":\"info\",\"msg\":\"latency: 13.214µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:27:10+07:00\",\"uri\":\"/hello/user-1\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-2\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:27:10.740955+07:00\",\"level\":\"info\",\"msg\":\"latency: 33.484µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:27:10+07:00\",\"uri\":\"/hello/user-2\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-3\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:27:10.752934+07:00\",\"level\":\"info\",\"msg\":\"latency: 23.883µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:27:10+07:00\",\"uri\":\"/hello/user-3\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-4\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:27:10.765675+07:00\",\"level\":\"info\",\"msg\":\"latency: 27.749µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:27:10+07:00\",\"uri\":\"/hello/user-4\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-5\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:27:10.778096+07:00\",\"level\":\"info\",\"msg\":\"latency: 30.309µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:27:10+07:00\",\"uri\":\"/hello/user-5\",\"user_agent\":\"curl/7.64.1\"}\n```\n\n### Fiber\n\nExample code:\n\n```go\npackage main\n\nimport (\n\t\"github.com/gofiber/fiber/v2\"\n\t\"github.com/trinhdaiphuc/logger\"\n)\n\nfunc main() {\n\tapp := fiber.New()\n\tapp.Use(logger.FiberMiddleware(logger.ConfigFiber{\n\t\tSkipperFiber: func(context *fiber.Ctx) bool {\n\t\t\tif string(context.Request().RequestURI()) == \"/metrics\" {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\treturn false\n\t\t},\n\t}))\n\n\tapp.Get(\"/hello/:name\", func(ctx *fiber.Ctx) error {\n\t\tlog := logger.GetLogger(ctx.Context())\n\t\tname := ctx.Params(\"name\")\n\t\tlog.AddLog(\"request name %v\", name)\n\t\treturn ctx.Status(200).SendString(\"Hello \" + name)\n\t})\n\n\tif err := app.Listen(\":8080\"); err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\nTry logger with request:\n\n```shell\nfor i in {0..5}; do curl :8080/hello/user-${i}; done \n```\n\nLogger output:\n\n```shell\n ┌───────────────────────────────────────────────────┐ \n │                   Fiber v2.20.2                   │ \n │               http://127.0.0.1:8080               │ \n │       (bound on host 0.0.0.0 and port 8080)       │ \n │                                                   │ \n │ Handlers ............. 3  Processes ........... 1 │ \n │ Prefork ....... Disabled  PID ............. 42319 │ \n └───────────────────────────────────────────────────┘ \n\n{\"STEP_1\":\"request name user-0\",\"Status\":200,\"client_ip\":\"127.0.0.1\",\"end\":\"2021-10-17T16:28:24.268446+07:00\",\"level\":\"info\",\"msg\":\"latency: 26.555µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:24+07:00\",\"uri\":\"/hello/user-0\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-1\",\"Status\":200,\"client_ip\":\"127.0.0.1\",\"end\":\"2021-10-17T16:28:24.274605+07:00\",\"level\":\"info\",\"msg\":\"latency: 9.141µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:24+07:00\",\"uri\":\"/hello/user-1\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-2\",\"Status\":200,\"client_ip\":\"127.0.0.1\",\"end\":\"2021-10-17T16:28:24.280196+07:00\",\"level\":\"info\",\"msg\":\"latency: 9.223µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:24+07:00\",\"uri\":\"/hello/user-2\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-3\",\"Status\":200,\"client_ip\":\"127.0.0.1\",\"end\":\"2021-10-17T16:28:24.286032+07:00\",\"level\":\"info\",\"msg\":\"latency: 12.195µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:24+07:00\",\"uri\":\"/hello/user-3\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-4\",\"Status\":200,\"client_ip\":\"127.0.0.1\",\"end\":\"2021-10-17T16:28:24.292232+07:00\",\"level\":\"info\",\"msg\":\"latency: 17.991µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:24+07:00\",\"uri\":\"/hello/user-4\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-5\",\"Status\":200,\"client_ip\":\"127.0.0.1\",\"end\":\"2021-10-17T16:28:24.301115+07:00\",\"level\":\"info\",\"msg\":\"latency: 41.77µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:24+07:00\",\"uri\":\"/hello/user-5\",\"user_agent\":\"curl/7.64.1\"}\n```\n\n### Gin\n\nExample code:\n\n```go\npackage main\n\nimport (\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/trinhdaiphuc/logger\"\n)\n\nfunc main() {\n\tgin.SetMode(gin.ReleaseMode)\n\tserver := gin.New()\n\tserver.Use(logger.GinMiddleware(logger.ConfigGin{SkipperGin: func(context *gin.Context) bool {\n\t\tif context.Request.RequestURI == \"/metrics\" {\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}}))\n\tserver.GET(\"/hello/:name\", func(ctx *gin.Context) {\n\t\tlog := logger.GetLogger(ctx)\n\t\tname := ctx.Param(\"name\")\n\t\tlog.AddLog(\"request name %v\", name)\n\t\tctx.String(200, \"Hello \"+name)\n\t})\n\n\tif err := server.Run(\":8080\"); err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\nTry logger with request:\n\n```shell\nfor i in {0..5}; do curl :8080/hello/user-${i}; done \n```\n\nLogger output:\n\n```shell\n{\"STEP_1\":\"request name user-0\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:28:52.836854+07:00\",\"level\":\"info\",\"msg\":\"latency: 26.574µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:52+07:00\",\"uri\":\"/hello/user-0\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-1\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:28:52.842935+07:00\",\"level\":\"info\",\"msg\":\"latency: 9.643µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:52+07:00\",\"uri\":\"/hello/user-1\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-2\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:28:52.848682+07:00\",\"level\":\"info\",\"msg\":\"latency: 14.542µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:52+07:00\",\"uri\":\"/hello/user-2\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-3\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:28:52.855196+07:00\",\"level\":\"info\",\"msg\":\"latency: 10.794µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:52+07:00\",\"uri\":\"/hello/user-3\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-4\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:28:52.863981+07:00\",\"level\":\"info\",\"msg\":\"latency: 24.512µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:52+07:00\",\"uri\":\"/hello/user-4\",\"user_agent\":\"curl/7.64.1\"}\n{\"STEP_1\":\"request name user-5\",\"Status\":200,\"client_ip\":\"::1\",\"end\":\"2021-10-17T16:28:52.874219+07:00\",\"level\":\"info\",\"msg\":\"latency: 12.744µs\",\"request_method\":\"GET\",\"time\":\"2021-10-17T16:28:52+07:00\",\"uri\":\"/hello/user-5\",\"user_agent\":\"curl/7.64.1\"}\n```\n\n### GRPC\n\nExample code:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"github.com/trinhdaiphuc/logger\"\n\tpb \"github.com/trinhdaiphuc/logger/proto/hello\"\n\t\"google.golang.org/grpc\"\n\t\"google.golang.org/grpc/codes\"\n\t\"google.golang.org/grpc/status\"\n\t\"net\"\n\t\"os\"\n\t\"os/signal\"\n\t\"syscall\"\n)\n\ntype HelloService struct{}\n\nvar _ pb.HelloServiceServer = (*HelloService)(nil)\n\nfunc (h HelloService) Hello(ctx context.Context, request *pb.HelloRequest) (*pb.HelloResponse, error) {\n\tlog := logger.GetLogger(ctx)\n\tlog.AddLog(\"Hello service\")\n\tif len(request.Name) == 0 {\n\t\tlog.AddLog(\"empty name\")\n\t\treturn nil, status.Error(codes.InvalidArgument, \"empty name\")\n\t}\n\treturn \u0026pb.HelloResponse{Message: \"Hello \" + request.Name}, nil\n}\n\nfunc main() {\n\tlistener, err := net.Listen(\"tcp\", \":50051\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tserver := grpc.NewServer(grpc.UnaryInterceptor(\n\t\tlogger.GrpcInterceptor(logger.ConfigGrpc{\n\t\t\tSkipperGrpc: func(ctx context.Context, info *grpc.UnaryServerInfo) bool {\n\t\t\t\tfmt.Println(\"method\", info.FullMethod)\n\t\t\t\tif strings.HasSuffix(info.FullMethod, \"/Hello\") {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t},\n\t\t}),\n\t))\n\n\tpb.RegisterHelloServiceServer(server, \u0026HelloService{})\n\n\tgo func() {\n\t\tif err := server.Serve(listener); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\t// Block main routine until a signal is received\n\tc := make(chan os.Signal)\n\tsignal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL, syscall.SIGHUP, syscall.SIGQUIT)\n\t\u003c-c\n\n\tfmt.Println(\"Gracefully shutting down...\")\n\tserver.GracefulStop()\n\tlistener.Close()\n\tfmt.Println(\"GRPC was successful shutdown.\")\n}\n```\n\nTry logger with GRPC client:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"github.com/trinhdaiphuc/logger\"\n\tpb \"github.com/trinhdaiphuc/logger/proto/hello\"\n\t\"google.golang.org/grpc\"\n)\n\nfunc main() {\n\tconn, err := grpc.Dial(\":50051\", grpc.WithInsecure())\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer conn.Close()\n\tlog := logger.New(logger.WithFormatter(\u0026logger.TextFormatter{}))\n\tclient := pb.NewHelloServiceClient(conn)\n\n\tfor i := 0; i \u003c= 5; i++ {\n\t\treq := \u0026pb.HelloRequest{\n\t\t\tName: fmt.Sprintf(\"user-%d\", i),\n\t\t}\n\t\tresp, err := client.Hello(context.Background(), req)\n\t\tif err != nil {\n\t\t\tlog.Error(err)\n\t\t}\n\t\tlog.Info(\"resp\", log.ToJsonString(resp))\n\t}\n}\n```\n\nLogger output:\n\n```shell\n{\"STEP_1\":\"Hello service\",\"client_ip\":\"127.0.0.1:62328\",\"code\":\"OK\",\"level\":\"info\",\"msg\":\"latency: 9.363µs\",\"request\":{\"name\":\"user-0\"},\"response\":{\"message\":\"Hello user-0\"},\"start\":\"2021-10-17T16:34:56.495895+07:00\",\"time\":\"2021-10-17T16:34:56+07:00\",\"uri\":\"/hello.HelloService/Hello\"}\n{\"STEP_1\":\"Hello service\",\"client_ip\":\"127.0.0.1:62328\",\"code\":\"OK\",\"level\":\"info\",\"msg\":\"latency: 34.926µs\",\"request\":{\"name\":\"user-1\"},\"response\":{\"message\":\"Hello user-1\"},\"start\":\"2021-10-17T16:34:56.496685+07:00\",\"time\":\"2021-10-17T16:34:56+07:00\",\"uri\":\"/hello.HelloService/Hello\"}\n{\"STEP_1\":\"Hello service\",\"client_ip\":\"127.0.0.1:62328\",\"code\":\"OK\",\"level\":\"info\",\"msg\":\"latency: 26.017µs\",\"request\":{\"name\":\"user-2\"},\"response\":{\"message\":\"Hello user-2\"},\"start\":\"2021-10-17T16:34:56.497186+07:00\",\"time\":\"2021-10-17T16:34:56+07:00\",\"uri\":\"/hello.HelloService/Hello\"}\n{\"STEP_1\":\"Hello service\",\"client_ip\":\"127.0.0.1:62328\",\"code\":\"OK\",\"level\":\"info\",\"msg\":\"latency: 23.865µs\",\"request\":{\"name\":\"user-3\"},\"response\":{\"message\":\"Hello user-3\"},\"start\":\"2021-10-17T16:34:56.497793+07:00\",\"time\":\"2021-10-17T16:34:56+07:00\",\"uri\":\"/hello.HelloService/Hello\"}\n{\"STEP_1\":\"Hello service\",\"client_ip\":\"127.0.0.1:62328\",\"code\":\"OK\",\"level\":\"info\",\"msg\":\"latency: 16.271µs\",\"request\":{\"name\":\"user-4\"},\"response\":{\"message\":\"Hello user-4\"},\"start\":\"2021-10-17T16:34:56.498207+07:00\",\"time\":\"2021-10-17T16:34:56+07:00\",\"uri\":\"/hello.HelloService/Hello\"}\n{\"STEP_1\":\"Hello service\",\"client_ip\":\"127.0.0.1:62328\",\"code\":\"OK\",\"level\":\"info\",\"msg\":\"latency: 17.859µs\",\"request\":{\"name\":\"user-5\"},\"response\":{\"message\":\"Hello user-5\"},\"start\":\"2021-10-17T16:34:56.498731+07:00\",\"time\":\"2021-10-17T16:34:56+07:00\",\"uri\":\"/hello.HelloService/Hello\"}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrinhdaiphuc%2Flogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrinhdaiphuc%2Flogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrinhdaiphuc%2Flogger/lists"}