{"id":19298632,"url":"https://github.com/ibbd-dev/go-log","last_synced_at":"2025-10-13T14:10:17.471Z","repository":{"id":117525203,"uuid":"73899897","full_name":"ibbd-dev/go-log","owner":"ibbd-dev","description":"golang logger，支持按时间周期写入，按概率写入，异步批量写入，错误日志等。","archived":false,"fork":false,"pushed_at":"2016-11-25T01:27:33.000Z","size":28,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-20T23:05:13.207Z","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-16T08:33:52.000Z","updated_at":"2017-07-18T03:05:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"ef2c5d87-e084-4c04-8ca5-531e86867dfd","html_url":"https://github.com/ibbd-dev/go-log","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ibbd-dev/go-log","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-log","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-log/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-log/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-log/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ibbd-dev","download_url":"https://codeload.github.com/ibbd-dev/go-log/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-log/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261032037,"owners_count":23100048,"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:35.001Z","updated_at":"2025-10-13T14:10:17.396Z","avatar_url":"https://github.com/ibbd-dev.png","language":"Go","readme":"# golang logger\n\n实现log的基本操作，实现按照时间周期写入\n\n## Install \n\n```sh\n# log基本操作，并实现按时间周期写入log\n# 按时间周期写入，保证一个周期内，只会写入一次。\n# 对于很多写log的情况，我们都需要控制一定的输出频率，避免log文件被写爆掉。\n# 接口类似官方的log包\ngo get -u github.com/ibbd-dev/go-log\n\n# 异步写log, 支持写入概率，例如以30%的概率写日志\n# 注意：该项目的接口是以ibbd-dev/go-log为基础的\ngo get -u github.com/ibbd-dev/go-log/async-log\n\n# 错误日志，支持错误等级\n# 该项目可以自由和上面两个项目的log.Logger，asyncLog.AsyncLogger进行组合\n# 也可以和官方的log包进行组合\ngo get -u github.com/ibbd-dev/go-log/error-log\n```\n\n注：可以和`github.com/ibbd-dev/go-rotate-file`组合使用，该项目可以支持按时间切割文件，例如每小时一个文件\n\n## Example\n\n\n### 随机的周期性写入log\n\n例如每秒写入一条log，能有效过于频繁的写log。\n\n```go\npackage main\n\nimport (\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/ibbd-dev/go-log\"\n\t\"github.com/ibbd-dev/go-rotate-file\"\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\tif err != nil {\n\t\t// TODO\n\t}\n\tdefer file.Close()\n\n\tlogger := log.NewDurationLogger(file, \"\", time.RFC3339)\n\tlogger.SetDuration(time.Millisecond * 100)\n\tlogger.SetPrefix(\"=====\")\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\nfunc main2() {\n\tfile := rotateFile.Open(\"/tmp/test-rotate-log.log\")\n\tdefer file.Close()\n\n\tlogger := NewDurationLogger(file, \"\", time.RFC3339)\n\tlogger.SetDuration(time.Millisecond * 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一些不太重要的日志，可以采取异步写入的形式，大大减轻io压力。\n\n注意：批量写入log的过程中，如果程序被异常中断，可能会丢失部分数据，而丢失的多少，而设置的写入周期和日志的频率有关。\n\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"github.com/ibbd-dev/go-rotate-file\"\n\t\"github.com/ibbd-dev/go-log/async-log\"\n)\n\nfunc main() {\n\tfile := rotateFile.Open(\"/tmp/test-async.log\")\n\tdefer file.Close()\n\n\tlogger := asyncLog.New(file, \"\", time.RFC3339, \"test-async\")\n\tlogger.SetDuration(time.Millisecond * 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-log/async-log`项目中有接口`SetProbability`，该接口可以用来设置写入概率，例如\n\n```go\n// 设置按10%的概率写日志\nlogger.SetProbability(0.1)\n```\n\n### 错误日志\n\n对于提供`Debug`, `Info`, `Warn`, `Error`, `Fatal`等接口（对应的格式化接口有`Debugf`, `Infof`, `Warnf`, `Errorf`, `Fatalf`等，提供格式化的功能），方便错误日志的写入。\n\n该项目可以方便的和其他日志对象的项目进行组合使用，例如和`asyncLog`组合使用可以实现按概率写入，或者批量异步写入，也可以和`ibbd-dev/go-log` 结合使用，或者和官方的`log`结合使用等。\n\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"github.com/ibbd-dev/go-rotate-file\"\n\t\"github.com/ibbd-dev/go-log/async-log\"\n\t\"github.com/ibbd-dev/go-log/error-log\"\n)\n\nfunc main() {\n\tfile := rotateFile.Open(\"/tmp/test-error.log\")\n\tdefer file.Close()\n\n\tlogger := asyncLog.New(file, \"\", time.RFC3339, \"test-error\")\n\tlogger.SetDuration(time.Millisecond * 100)\n\n    // logger 写日志的对象，该对象只要有接口Output(string)error即可。\n    // LevelWarn 写入级别，只有大于或者等于该级别的日志才会被写入\n\terrorLog := errorLog.New(logger, errorLog.LevelWarn)\n\terrorLog.Debug(12, \"Debug\", 1.023)\n\terrorLog.Info(13, \"Info\", 1.023)\n\terrorLog.Warn(14, \"Warn\", 1.023)\n\terrorLog.Error(15, \"Error\", 1.023)\n\terrorLog.Fatalf(\"id=%d, type=%s, price=%f\", 16, \"Fatal\", 1.023)\n\n\ttime.Sleep(time.Second)\n}\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibbd-dev%2Fgo-log","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fibbd-dev%2Fgo-log","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibbd-dev%2Fgo-log/lists"}