{"id":13413333,"url":"https://github.com/lmittmann/tint","last_synced_at":"2025-05-13T16:11:40.530Z","repository":{"id":148190732,"uuid":"616184794","full_name":"lmittmann/tint","owner":"lmittmann","description":"🌈 slog.Handler that writes tinted (colorized) logs","archived":false,"fork":false,"pushed_at":"2025-03-17T10:11:18.000Z","size":50,"stargazers_count":926,"open_issues_count":2,"forks_count":49,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-23T20:59:24.838Z","etag":null,"topics":["ansi","color","golang","logging","slog"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/lmittmann/tint","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/lmittmann.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,"zenodo":null}},"created_at":"2023-03-19T21:02:26.000Z","updated_at":"2025-04-23T03:59:48.000Z","dependencies_parsed_at":"2023-05-03T03:01:05.241Z","dependency_job_id":"f318f025-3673-4389-baa6-57a881cbf3a0","html_url":"https://github.com/lmittmann/tint","commit_stats":{"total_commits":47,"total_committers":9,"mean_commits":5.222222222222222,"dds":"0.23404255319148937","last_synced_commit":"bd5634cad3a14d6130690213c4d5287ed1baf89e"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmittmann%2Ftint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmittmann%2Ftint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmittmann%2Ftint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmittmann%2Ftint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lmittmann","download_url":"https://codeload.github.com/lmittmann/tint/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250514767,"owners_count":21443208,"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":["ansi","color","golang","logging","slog"],"created_at":"2024-07-30T20:01:37.990Z","updated_at":"2025-04-23T20:59:29.157Z","avatar_url":"https://github.com/lmittmann.png","language":"Go","readme":"# `tint`: 🌈 **slog.Handler** that writes tinted logs\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/lmittmann/tint.svg)](https://pkg.go.dev/github.com/lmittmann/tint#section-documentation)\n[![Go Report Card](https://goreportcard.com/badge/github.com/lmittmann/tint)](https://goreportcard.com/report/github.com/lmittmann/tint)\n\n\u003cpicture\u003e\n    \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://github.com/lmittmann/tint/assets/3458786/3d42f8d5-8bdf-40db-a16a-1939c88689cb\"\u003e\n    \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://github.com/lmittmann/tint/assets/3458786/3d42f8d5-8bdf-40db-a16a-1939c88689cb\"\u003e\n    \u003cimg src=\"https://github.com/lmittmann/tint/assets/3458786/3d42f8d5-8bdf-40db-a16a-1939c88689cb\"\u003e\n\u003c/picture\u003e\n\u003cbr\u003e\n\u003cbr\u003e\n\nPackage `tint` implements a zero-dependency [`slog.Handler`](https://pkg.go.dev/log/slog#Handler)\nthat writes tinted (colorized) logs. Its output format is inspired by the `zerolog.ConsoleWriter` and\n[`slog.TextHandler`](https://pkg.go.dev/log/slog#TextHandler).\n\nThe output format can be customized using [`Options`](https://pkg.go.dev/github.com/lmittmann/tint#Options)\nwhich is a drop-in replacement for [`slog.HandlerOptions`](https://pkg.go.dev/log/slog#HandlerOptions).\n\n```\ngo get github.com/lmittmann/tint\n```\n\n## Usage\n\n```go\nw := os.Stderr\n\n// create a new logger\nlogger := slog.New(tint.NewHandler(w, nil))\n\n// set global logger with custom options\nslog.SetDefault(slog.New(\n    tint.NewHandler(w, \u0026tint.Options{\n        Level:      slog.LevelDebug,\n        TimeFormat: time.Kitchen,\n    }),\n))\n```\n\n### Customize Attributes\n\n`ReplaceAttr` can be used to alter or drop attributes. If set, it is called on\neach non-group attribute before it is logged. See [`slog.HandlerOptions`](https://pkg.go.dev/log/slog#HandlerOptions)\nfor details.\n\n```go\n// create a new logger that doesn't write the time\nw := os.Stderr\nlogger := slog.New(\n    tint.NewHandler(w, \u0026tint.Options{\n        ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {\n            if a.Key == slog.TimeKey \u0026\u0026 len(groups) == 0 {\n                return slog.Attr{}\n            }\n            return a\n        },\n    }),\n)\n```\n\n```go\n// create a new logger that writes all errors in red\nw := os.Stderr\nlogger := slog.New(\n    tint.NewHandler(w, \u0026tint.Options{\n        ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {\n            if err, ok := a.Value.Any().(error); ok {\n                aErr := tint.Err(err)\n                aErr.Key = a.Key\n                return aErr\n            }\n            return a\n        },\n    }),\n)\n```\n\n### Automatically Enable Colors\n\nColors are enabled by default and can be disabled using the `Options.NoColor`\nattribute. To automatically enable colors based on the terminal capabilities,\nuse e.g. the [`go-isatty`](https://github.com/mattn/go-isatty) package.\n\n```go\nw := os.Stderr\nlogger := slog.New(\n    tint.NewHandler(w, \u0026tint.Options{\n        NoColor: !isatty.IsTerminal(w.Fd()),\n    }),\n)\n```\n\n### Windows Support\n\nColor support on Windows can be added by using e.g. the\n[`go-colorable`](https://github.com/mattn/go-colorable) package.\n\n```go\nw := os.Stderr\nlogger := slog.New(\n    tint.NewHandler(colorable.NewColorable(w), nil),\n)\n```\n","funding_links":[],"categories":["Formatting","Logging","Go","日志记录"],"sub_categories":["Search and Analytic Databases","检索及分析资料库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flmittmann%2Ftint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flmittmann%2Ftint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flmittmann%2Ftint/lists"}