{"id":43763762,"url":"https://github.com/gojuukaze/go-watch-file","last_synced_at":"2026-02-05T15:34:48.291Z","repository":{"id":57482665,"uuid":"185938463","full_name":"gojuukaze/go-watch-file","owner":"gojuukaze","description":"For writing files that may be rotated (e.g. log rotation). 用于写入可能被轮转的文件，如日志轮询场景。","archived":false,"fork":false,"pushed_at":"2026-01-29T02:44:40.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-29T18:59:47.622Z","etag":null,"topics":["go-log","go-watchfile","log","logrus","watchfile"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gojuukaze.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":"2019-05-10T07:16:54.000Z","updated_at":"2026-01-29T02:44:44.000Z","dependencies_parsed_at":"2022-09-02T06:21:22.341Z","dependency_job_id":null,"html_url":"https://github.com/gojuukaze/go-watch-file","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/gojuukaze/go-watch-file","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuukaze%2Fgo-watch-file","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuukaze%2Fgo-watch-file/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuukaze%2Fgo-watch-file/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuukaze%2Fgo-watch-file/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gojuukaze","download_url":"https://codeload.github.com/gojuukaze/go-watch-file/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuukaze%2Fgo-watch-file/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29124802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T14:05:12.718Z","status":"ssl_error","status_checked_at":"2026-02-05T14:03:53.078Z","response_time":65,"last_error":"SSL_read: 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":["go-log","go-watchfile","log","logrus","watchfile"],"created_at":"2026-02-05T15:34:48.221Z","updated_at":"2026-02-05T15:34:48.278Z","avatar_url":"https://github.com/gojuukaze.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# watchFile\n\n`go-watch-file` is a Go library similar to Python’s`WatchedFileHandler`, for writing files that may berotated, such as log rotation scenarios.  \n  \n(This project is stable and generally does not require updates unless Unix system behavior changes)\n  \n`go-watch-file` 是一个类似 Python `WatchedFileHandler` 的 Go 库，用于写入可能被轮转的文件，如日志轮转场景。  \n  \n（本项目已处于稳定状态，在 Unix 系统行为未发生变化的情况下通常不需要更新）\n\n\n# note\n* only Unix systems are supported\n* 仅支持 Unix 系统\n\n# version\n\n- latest: `v1.0.3`\n\n# Install\n\n```bash\ngo get -u github.com/gojuukaze/go-watch-file\n\n# use go mod\ngo get github.com/gojuukaze/go-watch-file@v1.0.3\n# or\ngo mod edit -require=github.com/gojuukaze/go-watch-file@v1.0.3\n\n```\n\n\n\n# Example\n```go\npackage main\n\nimport (\n\t\"github.com/gojuukaze/go-watch-file\"\n\t\"os\"\n\t\"fmt\"\n)\n\nfunc main() {\n\n\tf, _ := watchFile.OpenWatchFile(\"./a.txt\")\n\tf.WriteString(\"111\")\n\n\tos.Remove(\"./a.txt\")\n\n\tf.WriteString(\"222\\n\")\n\tf.Write([]byte(\"44\"))\n\n\tf.Close()\n\n\t//\n\tf2, _ := watchFile.OpenWatchFile2(\"./b.txt\", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)\n\tf2.WriteString(\"333\")\n\tf2.Close()\n\n\t// use other file method\n\tf3, _ := watchFile.OpenWatchFile2(\"./b.txt\", os.O_RDONLY|os.O_CREATE, 0666)\n\tf3.Reopen()\n\tb := make([] byte, 4)\n\tf3.F.Read(b)\n\tfmt.Println(b)\n\tf3.Close()\n\n}\n\n```\n# using with logrus\n\n## set output\n```go\nf,err:=watchFile.OpenWatchFile(\"m.log\")\nif err != nil {\n\tpanic(err)\n}\nLog := logrus.New()\nLog.SetOutput(f)\n```\n\n## hook\n```go\ntype FileHook struct {\n\tf         *watchFile.WatchFile\n\tformatter logrus.Formatter\n}\n\nfunc (hook FileHook) Levels() []logrus.Level {\n\treturn logrus.AllLevels\n}\n\nfunc (hook FileHook) Fire(entry *logrus.Entry) error {\n\tmsg, err := hook.formatter.Format(entry)\n\tif err != nil {\n\t\treturn err\n\t}\n\thook.f.Write(msg)\n\treturn nil\n}\n\nfunc NewFileHook(name string) FileHook {\n\tf, err := watchFile.OpenWatchFile(name)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tvar hook = FileHook{\n\t\tf:         f,\n\t\tformatter: \u0026logrus.TextFormatter{DisableColors: true},\n\t}\n\treturn hook\n\n}\n\n```\n\n```go\nLog = logrus.New()\nfHook:=NewFileHook(\"m.log\")\nLog.AddHook(fHook)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgojuukaze%2Fgo-watch-file","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgojuukaze%2Fgo-watch-file","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgojuukaze%2Fgo-watch-file/lists"}