{"id":13413299,"url":"https://github.com/go-playground/log","last_synced_at":"2025-04-05T16:08:50.638Z","repository":{"id":4157307,"uuid":"51254271","full_name":"go-playground/log","owner":"go-playground","description":":green_book: Simple, configurable and scalable Structured Logging for Go.","archived":false,"fork":false,"pushed_at":"2023-08-17T00:21:03.000Z","size":500,"stargazers_count":293,"open_issues_count":1,"forks_count":21,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-10-25T05:23:06.563Z","etag":null,"topics":["errors","logging","stack-traces"],"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/go-playground.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2016-02-07T16:17:48.000Z","updated_at":"2024-09-02T22:53:01.000Z","dependencies_parsed_at":"2022-09-02T06:04:04.231Z","dependency_job_id":"c37e0590-6db4-48fb-b478-3b9e05b7ef79","html_url":"https://github.com/go-playground/log","commit_stats":{"total_commits":131,"total_committers":6,"mean_commits":"21.833333333333332","dds":0.2824427480916031,"last_synced_commit":"f536b51b99bfbf273ac68e71b735f73621dbeea9"},"previous_names":["go-experimental/log"],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Flog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Flog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Flog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Flog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-playground","download_url":"https://codeload.github.com/go-playground/log/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361691,"owners_count":20926643,"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":["errors","logging","stack-traces"],"created_at":"2024-07-30T20:01:37.139Z","updated_at":"2025-04-05T16:08:50.619Z","avatar_url":"https://github.com/go-playground.png","language":"Go","readme":"## log\n\u003cimg align=\"center\" src=\"https://raw.githubusercontent.com/go-playground/log/master/logo.png\"\u003e![Project status](https://img.shields.io/badge/version-8.1.2-green.svg)\n[![Test](https://github.com/go-playground/log/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/log/actions/workflows/go.yml)\n[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)\n[![GoDoc](https://godoc.org/github.com/go-playground/log?status.svg)](https://godoc.org/github.com/go-playground/log)\n![License](https://img.shields.io/dub/l/vibe-d.svg)\n\nLog is a simple, highly configurable, Structured Logging library\n\nWhy another logging library?\n----------------------------\nThere's a lot of great stuff out there, but this library contains a number of unique features noted below using *.\n\nFeatures\n--------\n- [x] Logger is simple, only logic to create the log entry and send it off to the handlers, they take it from there.\n- [x] Handlers are simple to write + easy to register + easy to remove\n- [x] *Ability to specify which log levels get sent to each handler\n- [x] *Handlers \u0026 Log Levels are configurable at runtime.\n- [x] *`WithError` automatically extracts and adds file, line and package in error output.\n- [x] *Convenient context helpers `GetContext` \u0026 `SetContext`\n- [x] *Works with go-playground/errors extracting wrapped errors, types and tags when used with the `WithError` interface. This is the default but configurable to support more or other error libraries using `SetWithErrorFn`.\n- [x] *Default logger for quick prototyping and cli applications. It is automatically removed when you register one of your own.\n\nInstallation\n-----------\n\nUse go get \n\n```go\ngo get github.com/go-playground/log/v8@latest\n``` \n\nUsage\n------\nimport the log package.\n```go\npackage main\n\nimport (\n\t\"io\"\n\tstdlog \"log\"\n\n\t\"github.com/go-playground/errors/v5\"\n\t\"github.com/go-playground/log/v8\"\n)\n\nfunc main() {\n\tlog.RedirectGoStdLog(true)\n\n\t// Trace\n\tdefer log.WithTrace().Info(\"time to run\")\n\n\tlog.Debug(\"debug\")\n\tlog.Info(\"info\")\n\tlog.Notice(\"notice\")\n\tlog.Warn(\"warn\")\n\tlog.Error(\"error\")\n\t// log.Panic(\"panic\") // this will panic\n\tlog.Alert(\"alert\")\n\t// log.Fatal(\"fatal\") // this will call os.Exit(1)\n\n\terr := errors.New(\"this is the inner error\").AddTags(errors.T(\"inner\", \"tag\"))\n\terr = errors.Wrap(err, \"this is the wrapping error\").AddTags(errors.T(\"outer\", \"tag\"))\n\n\t// logging with fields can be used with any of the above\n\tlog.WithError(err).WithFields(log.F(\"key\", \"value\")).Info(\"test info\")\n\n\t// log unwrapped error\n\tlog.WithError(io.EOF).Error(\"unwrapped error\")\n\n\t// predefined global fields\n\tlog.WithDefaultFields(log.Fields{\n\t\tlog.F(\"program\", \"test\"),\n\t\tlog.F(\"version\", \"0.1.3\"),\n\t}...)\n\n\tlog.WithField(\"key\", \"value\").Info(\"testing default fields\")\n\n\t// or request scoped default fields\n\tlogger := log.WithFields(\n\t\tlog.F(\"request\", \"req\"),\n\t\tlog.F(\"scoped\", \"sco\"),\n\t)\n\n\tlogger.WithField(\"key\", \"value\").Info(\"test\")\n\n\tstdlog.Println(\"This was redirected from Go STD output!\")\n\tlog.RedirectGoStdLog(false)\n\tstdlog.Println(\"This was NOT redirected from Go STD output!\")\n}\n```\n\nAdding your own Handler\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\n\t\"github.com/go-playground/log/v8\"\n)\n\n// CustomHandler is your custom handler\ntype CustomHandler struct {\n\t// whatever properties you need\n}\n\n// Log accepts log entries to be processed\nfunc (c *CustomHandler) Log(e log.Entry) {\n\n\t// below prints to os.Stderr but could marshal to JSON\n\t// and send to central logging server\n\t//\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t       ---------\n\t// \t\t\t\t                                                                 |----------\u003e | console |\n\t//                                                                               |             ---------\n\t// i.e. -----------------               -----------------     Unmarshal    -------------       --------\n\t//     | app log handler | -- json --\u003e | central log app | --    to    -\u003e | log handler | --\u003e | syslog |\n\t//      -----------------               -----------------       Entry      -------------       --------\n\t//      \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t         |             ---------\n\t//                                  \t\t\t\t\t\t\t\t\t         |----------\u003e | DataDog |\n\t//          \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t        \t   ---------\n\tb := new(bytes.Buffer)\n\tb.Reset()\n\tb.WriteString(e.Message)\n\n\tfor _, f := range e.Fields {\n\t\t_, _ = fmt.Fprintf(b, \" %s=%v\", f.Key, f.Value)\n\t}\n\tfmt.Println(b.String())\n}\n\nfunc main() {\n\n\tcLog := new(CustomHandler)\n\tlog.AddHandler(cLog, log.AllLevels...)\n\n\t// Trace\n\tdefer log.WithTrace().Info(\"took this long\")\n\n\tlog.Debug(\"debug\")\n\tlog.Info(\"info\")\n\tlog.Notice(\"notice\")\n\tlog.Warn(\"warn\")\n\tlog.Error(\"error\")\n\t// log.Panic(\"panic\") // this will panic\n\tlog.Alert(\"alert\")\n\t// log.Fatal(\"fatal\") // this will call os.Exit(1)\n\n\t// logging with fields can be used with any of the above\n\tlog.WithField(\"key\", \"value\").Info(\"test info\")\n}\n```\n\n#### Go 1.21+ slog compatibility\n\nThere is a compatibility layer for slog, which allows redirecting slog to this logger and ability to output to an slog.Handler+.\n\n| type                                        | Definition                                                                                                                                                                          |\n|---------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Handler](_examples/slog/hanlder/main.go)   | This example demonstrates how to redirect the std log and slog to this logger by using it as an slog.Handler.                                                                       |\n| [Redirect](_examples/slog/redirect/main.go) | This example demonstrates how to redirect the std log and slog to this logger and output back out to any slog.Handler, as well as any other handler(s) registered with this logger. |\n\n```go\n\nLog Level Definitions\n---------------------\n\n| Level  | Description                                                                                                                                                                                               |\n|--------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Debug  | Info useful to developers for debugging the application, not useful during normal operations.                                                                                                             |\n| Info   | Normal operational messages which may be harvested for reporting, measuring throughput, etc. no action required.                                                                                          |\n| Notice | Normal but significant condition. Events that are unusual but not error conditions eg. might be summarized in an email to developers or admins to spot potential problems - no immediate action required. |\n| Warn   | Warning messages, not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full. Each item must be resolved within a given time.                                |\n| Error  | Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time.                                                                                     |\n| Panic  | A \"panic\" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call.                                                                          |\n| Alert  | Action must be taken immediately. Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection.                              |\n| Fatal  | Should be corrected immediately, but indicates failure in a primary system, an example is a loss of a backup ISP connection. ( same as SYSLOG CRITICAL )                                                  |\n\nHandlers\n-------------\nPull requests for new handlers are welcome when they don't pull in dependencies, it is preferred to have a dedicated package in this case.\n\n| Handler | Description                                                                                                                              | Docs                                                                                                                                                              |\n| ------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| json    | Allows for log messages to be sent to any wrtier in json format.                                                                         | [![GoDoc](https://godoc.org/github.com/go-playground/log/handlers/json?status.svg)](https://godoc.org/github.com/go-playground/log/handlers/json)                 |\n\nPackage Versioning\n----------\nThis package strictly adheres to semantic versioning guidelines.","funding_links":[],"categories":["Logging","\u003cspan id=\"日志-logging\"\u003e日志 Logging\u003c/span\u003e","日志记录","Logging 日志库","Relational Databases","日志"],"sub_categories":["Advanced Console UIs","Search and Analytic Databases","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","检索及分析资料库","SQL 查询语句构建库","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-playground%2Flog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-playground%2Flog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-playground%2Flog/lists"}