{"id":19298653,"url":"https://github.com/ibbd-dev/go-rate-log","last_synced_at":"2025-02-24T01:15:30.452Z","repository":{"id":117525283,"uuid":"73251082","full_name":"ibbd-dev/go-rate-log","owner":"ibbd-dev","description":"实现频率控制的log服务    Write log by rate","archived":false,"fork":false,"pushed_at":"2016-11-15T03:37:32.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-05T22:24:42.503Z","etag":null,"topics":[],"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/ibbd-dev.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}},"created_at":"2016-11-09T03:56:54.000Z","updated_at":"2018-05-15T08:36:37.000Z","dependencies_parsed_at":"2024-08-04T02:01:57.913Z","dependency_job_id":null,"html_url":"https://github.com/ibbd-dev/go-rate-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/ibbd-dev%2Fgo-rate-log","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-rate-log/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-rate-log/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-rate-log/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ibbd-dev","download_url":"https://codeload.github.com/ibbd-dev/go-rate-log/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240400334,"owners_count":19795332,"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":[],"created_at":"2024-11-09T23:08:43.932Z","updated_at":"2025-02-24T01:15:30.387Z","avatar_url":"https://github.com/ibbd-dev.png","language":"Go","readme":"# 控制写入频率的log\n\n按时间周期写入，保证一个周期内，只会写入一次。\n\n对于很多写log的情况，我们都需要控制一定的输出频率，避免log文件被写爆掉。\n\n## Install\n\n```sh\ngo get -u github.com/ibbd-dev/go-rate-log\n```\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"os\"\n\t\"time\"\n\n    \"github.com/ibbd-dev/go-rate-log\"\n)\n\nfunc main() {\n\t// 文件Flag\n\tfileFlag := os.O_WRONLY | os.O_CREATE | os.O_APPEND\n\tfile, err := os.OpenFile(\"/tmp/test-rate.log\", fileFlag, 0666)\n\tdefer file.Close()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\n\tlogger := rateLog.New(file, \"\", time.RFC3339)\n\tlogger.SetDuration(time.Millisecond * 100) // 每100毫秒写入一次\n\tlogger.Println(\"hello world\")\n\tlogger.Println(\"hello world2\")\n\ttime.Sleep(time.Millisecond * 105)\n\tlogger.Println(\"hello world3\")\n\tlogger.Println(\"hello world3\")\n\ttime.Sleep(time.Millisecond * 10)\n\tlogger.Println(\"hello world4\")\n}\n```\n\n## 根据周期自动切割文件\n\n配合`github.com/ibbd-dev/go-rotate-file`，即可实现自动切割文件。\n\n```go\npackage main\n\nimport (\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/ibbd-dev/go-rotate-file\"\n    \"github.com/ibbd-dev/go-rate-log\"\n)\n\nfunc main() {\n\t// 最终保存时，文件名如：/tmp/test-rotate.log.161109\n    // 其中后缀161109表示2016-11-09，默认是按照日期对文件进行切割\n\tfile := rotateFile.Open(\"/tmp/test-rotate.log\")\n\tdefer file.Close()\n\n\tlogger := rateLog.New(file, \"\", time.RFC3339)\n\tlogger.SetDuration(time.Millisecond * 100) // 每100毫秒写入一次\n\tlogger.Println(\"hello world\")\n\tlogger.Println(\"hello world2\")\n\ttime.Sleep(time.Millisecond * 105)\n\tlogger.Println(\"hello world3\")\n\tlogger.Println(\"hello world3\")\n\ttime.Sleep(time.Millisecond * 10)\n\tlogger.Println(\"hello world4\")\n}\n```\n\n## 根据错误类型写日志\n\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"github.com/ibbd-dev/go-rotate-file\"\n    \"github.com/ibbd-dev/go-rate-log\"\n)\n\nfunc main() {\n\tfile := rotateFile.Open(\"/tmp/test-level-log.log\")\n\tdefer file.Close()\n\n\tlogger := rateLog.New(file, \"\", time.RFC3339)\n\tlogger.SetDuration(time.Millisecond * 100)\n\n\tll := rateLog.NewLevelLog(logger, rateLog.LevelWarn)\n\tll.Debug(\"Debug\")\n\tll.Info(\"Info\")\n\tll.Warn(\"Warn\")\n\tll.Error(\"Error\")\n\ttime.Sleep(time.Second)\n\tll.Fatal(\"Fatal\")\n}\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibbd-dev%2Fgo-rate-log","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fibbd-dev%2Fgo-rate-log","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibbd-dev%2Fgo-rate-log/lists"}