{"action":"created","comment":{"url":"https://api.github.com/repos/moby/moby/pulls/comments/120048555","pull_request_review_id":41979014,"id":120048555,"diff_hunk":"@@ -90,20 +96,125 @@ func rotate(name string, maxFiles int) error {\n \tif maxFiles < 2 {\n \t\treturn nil\n \t}\n-\tfor i := maxFiles - 1; i > 1; i-- {\n-\t\ttoPath := name + \".\" + strconv.Itoa(i)\n-\t\tfromPath := name + \".\" + strconv.Itoa(i-1)\n+\n+\tfor i := maxFiles - 1; i > 2; i-- {\n+\t\ttoPath := name + \".\" + strconv.Itoa(i) + \".gz\"\n+\t\tfromPath := name + \".\" + strconv.Itoa(i-1) + \".gz\"\n \t\tif err := os.Rename(fromPath, toPath); err != nil && !os.IsNotExist(err) {\n \t\t\treturn err\n \t\t}\n \t}\n \n+\tif _, err := os.Stat(name + \".1\"); err == nil && maxFiles > 2 {\n+\t\tif err := os.Rename(name+\".1\", name+\".2\"); err != nil {\n+\t\t\treturn err\n+\t\t}\n+\n+\t\tif err := compressJSONFile(name + \".2\"); err != nil {\n+\t\t\treturn err\n+\t\t}\n+\t}\n+\n+\t// The \"[name].1\" that jast renamed from \"[name]\" is not compressed\n+\t// in order to prevent the log tracking tool from losing some historical\n+\t// log data when a new log file is created.\n \tif err := os.Rename(name, name+\".1\"); err != nil && !os.IsNotExist(err) {\n \t\treturn err\n \t}\n \treturn nil\n }\n \n+func compressJSONFile(fileName string) (err error) {\n+\tfile, err := os.Open(fileName)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tdefer func() {\n+\t\tfile.Close()\n+\t\tos.Remove(fileName)\n+\t}()\n+\n+\toutFile, err := os.OpenFile(fileName+\".gz\", os.O_CREATE|os.O_RDWR, 0640)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tdefer func() {\n+\t\toutFile.Close()\n+\t\tif err != nil {\n+\t\t\tos.Remove(fileName + \".gz\")\n+\t\t}\n+\t}()\n+\n+\tcompressWriter := gzip.NewWriter(outFile)\n+\tdefer compressWriter.Close()\n+\n+\t// Extract the last log entry timestramp from log file\n+\tlastLine, err := readLastLine(file)\n+\tl := &jsonlog.JSONLog{}\n+\terr = json.Unmarshal(lastLine, l)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tlastTime := l.Created.Format(jsonlog.RFC3339NanoFixed)\n+\n+\t// Add the last log entry timestramp to the gzip header\n+\textra := &RotateFileMetadata{}\n+\textra.LastTime = lastTime\n+\tcompressWriter.Header.Extra, err = json.Marshal(extra)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\n+\tfile.Seek(0, os.SEEK_SET)\n+\t_, err = io.Copy(compressWriter, file)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\n+\treturn nil\n+}\n+\n+func readLastLine(file *os.File) ([]byte, error) {","path":"daemon/logger/loggerutils/rotatefilewriter.go","position":null,"original_position":103,"commit_id":"d0dfffdcec1c1e8beba18663ab8ba7acea77681b","original_commit_id":"647a11d4d2e86d390c9e89959b2e5ee90070a94d","user":{"login":"miaoyq","id":17755856,"avatar_url":"https://avatars1.githubusercontent.com/u/17755856?v=3","gravatar_id":"","url":"https://api.github.com/users/miaoyq","html_url":"https://github.com/miaoyq","followers_url":"https://api.github.com/users/miaoyq/followers","following_url":"https://api.github.com/users/miaoyq/following{/other_user}","gists_url":"https://api.github.com/users/miaoyq/gists{/gist_id}","starred_url":"https://api.github.com/users/miaoyq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/miaoyq/subscriptions","organizations_url":"https://api.github.com/users/miaoyq/orgs","repos_url":"https://api.github.com/users/miaoyq/repos","events_url":"https://api.github.com/users/miaoyq/events{/privacy}","received_events_url":"https://api.github.com/users/miaoyq/received_events","type":"User","site_admin":false},"body":"> We may want to change the interface on the rotating writer to be Write(logEntry) error instead of a generic io.Writer.\r\n\r\n@cpuguy83  Updated.","created_at":"2017-06-05T06:29:17Z","updated_at":"2017-06-05T06:29:17Z","html_url":"https://github.com/moby/moby/pull/29932#discussion_r120048555","pull_request_url":"https://api.github.com/repos/moby/moby/pulls/29932","_links":{"self":{"href":"https://api.github.com/repos/moby/moby/pulls/comments/120048555"},"html":{"href":"https://github.com/moby/moby/pull/29932#discussion_r120048555"},"pull_request":{"href":"https://api.github.com/repos/moby/moby/pulls/29932"}}},"pull_request":{"url":"https://api.github.com/repos/moby/moby/pulls/29932","id":100397496,"html_url":"https://github.com/moby/moby/pull/29932","diff_url":"https://github.com/moby/moby/pull/29932.diff","patch_url":"https://github.com/moby/moby/pull/29932.patch","issue_url":"https://api.github.com/repos/moby/moby/issues/29932","number":29932,"state":"open","locked":false,"title":"add support for compressibility of log file","user":{"login":"miaoyq","id":17755856,"avatar_url":"https://avatars1.githubusercontent.com/u/17755856?v=3","gravatar_id":"","url":"https://api.github.com/users/miaoyq","html_url":"https://github.com/miaoyq","followers_url":"https://api.github.com/users/miaoyq/followers","following_url":"https://api.github.com/users/miaoyq/following{/other_user}","gists_url":"https://api.github.com/users/miaoyq/gists{/gist_id}","starred_url":"https://api.github.com/users/miaoyq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/miaoyq/subscriptions","organizations_url":"https://api.github.com/users/miaoyq/orgs","repos_url":"https://api.github.com/users/miaoyq/repos","events_url":"https://api.github.com/users/miaoyq/events{/privacy}","received_events_url":"https://api.github.com/users/miaoyq/received_events","type":"User","site_admin":false},"body":"Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>\r\n\r\n<!--\r\nPlease make sure you've read and understood our contributing guidelines;\r\nhttps://github.com/docker/docker/blob/master/CONTRIBUTING.md\r\n\r\n** Make sure all your commits include a signature generated with `git commit -s` **\r\n\r\nFor additional information on our contributing process, read our contributing\r\nguide https://docs.docker.com/opensource/code/\r\n\r\nIf this is a bug fix, make sure your description includes \"fixes #xxxx\", or\r\n\"closes #xxxx\"\r\n\r\nPlease provide the following information:\r\n-->\r\n\r\n**- What I did**\r\nThis PR adds support for compressibility of log file. I added a new option `conpression` for the `jsonfile` log driver, this option allows the user to specify compression algorithm to compress the log files.\r\n\r\n**- How I did it**\r\nThe `jsonfile` will compress the history log file, except for `container-id-json.log.1`, when `compression=gzip`. The `container-id-json.log.1` is not compressed in order to prevent the log tracking tool from losing some historical log data when a new log file is created.\r\n**- How to verify it**\r\nI run a container as follow:\r\n ```\r\n$ docker run -d --log-opt max-size=1m --log-opt max-file=5 --log-opt compression=gzip busybox /bin/sh -c 'while true; do sleep 0.001; echo \"hello!\"; done'\r\nb355968fc501942525f8c48a52201e62cc9e48b6b342b4372d60000feb59c447\r\n```\r\nThe results are as follows:\r\n```\r\n$ ls -l\r\ntotal 1360\r\n-r--------    1 root     root        120003 Jan  5 10:48 b355968fc501942525f8c48a52201e62cc9e48b6b342b4372d60000feb59c447-json.log\r\n-r--------    1 root     root       1000039 Jan  5 10:48 b355968fc501942525f8c48a52201e62cc9e48b6b342b4372d60000feb59c447-json.log.1\r\n-rw-------    1 root     root         75813 Jan  5 10:48 b355968fc501942525f8c48a52201e62cc9e48b6b342b4372d60000feb59c447-json.log.2.gz\r\n-rw-------    1 root     root         75795 Jan  5 10:48 b355968fc501942525f8c48a52201e62cc9e48b6b342b4372d60000feb59c447-json.log.3.gz\r\n-rw-------    1 root     root         75814 Jan  5 10:48 b355968fc501942525f8c48a52201e62cc9e48b6b342b4372d60000feb59c447-json.log.4.gz\r\ndrwx------    2 root     root             6 Jan  5 10:47 checkpoints\r\n-rw-r--r--    1 root     root          2532 Jan  5 10:47 config.v2.json\r\n-rw-r--r--    1 root     root          1165 Jan  5 10:47 hostconfig.json\r\n-rw-r--r--    1 root     root            13 Jan  5 10:47 hostname\r\n-rw-r--r--    1 root     root           174 Jan  5 10:47 hosts\r\n-rw-r--r--    1 root     root            75 Jan  5 10:47 resolv.conf\r\n-rw-r--r--    1 root     root            71 Jan  5 10:47 resolv.conf.hash\r\ndrwxrwxrwt    2 root     root            40 Jan  5 10:47 shm\r\n```\r\n**- Description for the changelog**\r\n<!--\r\nWrite a short (one line) summary that describes the changes in this\r\npull request for inclusion in the changelog:\r\n-->\r\n\r\n\r\n**- A picture of a cute animal (not mandatory but encouraged)**\r\n","created_at":"2017-01-06T01:48:49Z","updated_at":"2017-06-05T06:29:17Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0ffc43465a08613140ad72bcbd4212a5a87e9283","assignee":{"login":"mlaventure","id":16065150,"avatar_url":"https://avatars2.githubusercontent.com/u/16065150?v=3","gravatar_id":"","url":"https://api.github.com/users/mlaventure","html_url":"https://github.com/mlaventure","followers_url":"https://api.github.com/users/mlaventure/followers","following_url":"https://api.github.com/users/mlaventure/following{/other_user}","gists_url":"https://api.github.com/users/mlaventure/gists{/gist_id}","starred_url":"https://api.github.com/users/mlaventure/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mlaventure/subscriptions","organizations_url":"https://api.github.com/users/mlaventure/orgs","repos_url":"https://api.github.com/users/mlaventure/repos","events_url":"https://api.github.com/users/mlaventure/events{/privacy}","received_events_url":"https://api.github.com/users/mlaventure/received_events","type":"User","site_admin":false},"assignees":[{"login":"mlaventure","id":16065150,"avatar_url":"https://avatars2.githubusercontent.com/u/16065150?v=3","gravatar_id":"","url":"https://api.github.com/users/mlaventure","html_url":"https://github.com/mlaventure","followers_url":"https://api.github.com/users/mlaventure/followers","following_url":"https://api.github.com/users/mlaventure/following{/other_user}","gists_url":"https://api.github.com/users/mlaventure/gists{/gist_id}","starred_url":"https://api.github.com/users/mlaventure/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mlaventure/subscriptions","organizations_url":"https://api.github.com/users/mlaventure/orgs","repos_url":"https://api.github.com/users/mlaventure/repos","events_url":"https://api.github.com/users/mlaventure/events{/privacy}","received_events_url":"https://api.github.com/users/mlaventure/received_events","type":"User","site_admin":false}],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/moby/moby/pulls/29932/commits","review_comments_url":"https://api.github.com/repos/moby/moby/pulls/29932/comments","review_comment_url":"https://api.github.com/repos/moby/moby/pulls/comments{/number}","comments_url":"https://api.github.com/repos/moby/moby/issues/29932/comments","statuses_url":"https://api.github.com/repos/moby/moby/statuses/d0dfffdcec1c1e8beba18663ab8ba7acea77681b","head":{"label":"miaoyq:container-log-add-archive","ref":"container-log-add-archive","sha":"d0dfffdcec1c1e8beba18663ab8ba7acea77681b","user":{"login":"miaoyq","id":17755856,"avatar_url":"https://avatars1.githubusercontent.com/u/17755856?v=3","gravatar_id":"","url":"https://api.github.com/users/miaoyq","html_url":"https://github.com/miaoyq","followers_url":"https://api.github.com/users/miaoyq/followers","following_url":"https://api.github.com/users/miaoyq/following{/other_user}","gists_url":"https://api.github.com/users/miaoyq/gists{/gist_id}","starred_url":"https://api.github.com/users/miaoyq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/miaoyq/subscriptions","organizations_url":"https://api.github.com/users/miaoyq/orgs","repos_url":"https://api.github.com/users/miaoyq/repos","events_url":"https://api.github.com/users/miaoyq/events{/privacy}","received_events_url":"https://api.github.com/users/miaoyq/received_events","type":"User","site_admin":false},"repo":{"id":61846081,"name":"docker","full_name":"miaoyq/docker","owner":{"login":"miaoyq","id":17755856,"avatar_url":"https://avatars1.githubusercontent.com/u/17755856?v=3","gravatar_id":"","url":"https://api.github.com/users/miaoyq","html_url":"https://github.com/miaoyq","followers_url":"https://api.github.com/users/miaoyq/followers","following_url":"https://api.github.com/users/miaoyq/following{/other_user}","gists_url":"https://api.github.com/users/miaoyq/gists{/gist_id}","starred_url":"https://api.github.com/users/miaoyq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/miaoyq/subscriptions","organizations_url":"https://api.github.com/users/miaoyq/orgs","repos_url":"https://api.github.com/users/miaoyq/repos","events_url":"https://api.github.com/users/miaoyq/events{/privacy}","received_events_url":"https://api.github.com/users/miaoyq/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/miaoyq/docker","description":"Docker - the open-source application container engine","fork":true,"url":"https://api.github.com/repos/miaoyq/docker","forks_url":"https://api.github.com/repos/miaoyq/docker/forks","keys_url":"https://api.github.com/repos/miaoyq/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/miaoyq/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/miaoyq/docker/teams","hooks_url":"https://api.github.com/repos/miaoyq/docker/hooks","issue_events_url":"https://api.github.com/repos/miaoyq/docker/issues/events{/number}","events_url":"https://api.github.com/repos/miaoyq/docker/events","assignees_url":"https://api.github.com/repos/miaoyq/docker/assignees{/user}","branches_url":"https://api.github.com/repos/miaoyq/docker/branches{/branch}","tags_url":"https://api.github.com/repos/miaoyq/docker/tags","blobs_url":"https://api.github.com/repos/miaoyq/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/miaoyq/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/miaoyq/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/miaoyq/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/miaoyq/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/miaoyq/docker/languages","stargazers_url":"https://api.github.com/repos/miaoyq/docker/stargazers","contributors_url":"https://api.github.com/repos/miaoyq/docker/contributors","subscribers_url":"https://api.github.com/repos/miaoyq/docker/subscribers","subscription_url":"https://api.github.com/repos/miaoyq/docker/subscription","commits_url":"https://api.github.com/repos/miaoyq/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/miaoyq/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/miaoyq/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/miaoyq/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/miaoyq/docker/contents/{+path}","compare_url":"https://api.github.com/repos/miaoyq/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/miaoyq/docker/merges","archive_url":"https://api.github.com/repos/miaoyq/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/miaoyq/docker/downloads","issues_url":"https://api.github.com/repos/miaoyq/docker/issues{/number}","pulls_url":"https://api.github.com/repos/miaoyq/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/miaoyq/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/miaoyq/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/miaoyq/docker/labels{/name}","releases_url":"https://api.github.com/repos/miaoyq/docker/releases{/id}","deployments_url":"https://api.github.com/repos/miaoyq/docker/deployments","created_at":"2016-06-24T00:47:35Z","updated_at":"2016-10-28T04:49:16Z","pushed_at":"2017-06-05T05:56:01Z","git_url":"git://github.com/miaoyq/docker.git","ssh_url":"git@github.com:miaoyq/docker.git","clone_url":"https://github.com/miaoyq/docker.git","svn_url":"https://github.com/miaoyq/docker","homepage":"http://www.docker.com","size":127631,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":0,"forks":2,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"moby:master","ref":"master","sha":"7cf17f95f88bd61e677d722229b223cfe76ab6a0","user":{"login":"moby","id":27259197,"avatar_url":"https://avatars2.githubusercontent.com/u/27259197?v=3","gravatar_id":"","url":"https://api.github.com/users/moby","html_url":"https://github.com/moby","followers_url":"https://api.github.com/users/moby/followers","following_url":"https://api.github.com/users/moby/following{/other_user}","gists_url":"https://api.github.com/users/moby/gists{/gist_id}","starred_url":"https://api.github.com/users/moby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moby/subscriptions","organizations_url":"https://api.github.com/users/moby/orgs","repos_url":"https://api.github.com/users/moby/repos","events_url":"https://api.github.com/users/moby/events{/privacy}","received_events_url":"https://api.github.com/users/moby/received_events","type":"Organization","site_admin":false},"repo":{"id":7691631,"name":"moby","full_name":"moby/moby","owner":{"login":"moby","id":27259197,"avatar_url":"https://avatars2.githubusercontent.com/u/27259197?v=3","gravatar_id":"","url":"https://api.github.com/users/moby","html_url":"https://github.com/moby","followers_url":"https://api.github.com/users/moby/followers","following_url":"https://api.github.com/users/moby/following{/other_user}","gists_url":"https://api.github.com/users/moby/gists{/gist_id}","starred_url":"https://api.github.com/users/moby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moby/subscriptions","organizations_url":"https://api.github.com/users/moby/orgs","repos_url":"https://api.github.com/users/moby/repos","events_url":"https://api.github.com/users/moby/events{/privacy}","received_events_url":"https://api.github.com/users/moby/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/moby/moby","description":"Moby Project - a collaborative project for the container ecosystem to assemble container-based systems","fork":false,"url":"https://api.github.com/repos/moby/moby","forks_url":"https://api.github.com/repos/moby/moby/forks","keys_url":"https://api.github.com/repos/moby/moby/keys{/key_id}","collaborators_url":"https://api.github.com/repos/moby/moby/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/moby/moby/teams","hooks_url":"https://api.github.com/repos/moby/moby/hooks","issue_events_url":"https://api.github.com/repos/moby/moby/issues/events{/number}","events_url":"https://api.github.com/repos/moby/moby/events","assignees_url":"https://api.github.com/repos/moby/moby/assignees{/user}","branches_url":"https://api.github.com/repos/moby/moby/branches{/branch}","tags_url":"https://api.github.com/repos/moby/moby/tags","blobs_url":"https://api.github.com/repos/moby/moby/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/moby/moby/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/moby/moby/git/refs{/sha}","trees_url":"https://api.github.com/repos/moby/moby/git/trees{/sha}","statuses_url":"https://api.github.com/repos/moby/moby/statuses/{sha}","languages_url":"https://api.github.com/repos/moby/moby/languages","stargazers_url":"https://api.github.com/repos/moby/moby/stargazers","contributors_url":"https://api.github.com/repos/moby/moby/contributors","subscribers_url":"https://api.github.com/repos/moby/moby/subscribers","subscription_url":"https://api.github.com/repos/moby/moby/subscription","commits_url":"https://api.github.com/repos/moby/moby/commits{/sha}","git_commits_url":"https://api.github.com/repos/moby/moby/git/commits{/sha}","comments_url":"https://api.github.com/repos/moby/moby/comments{/number}","issue_comment_url":"https://api.github.com/repos/moby/moby/issues/comments{/number}","contents_url":"https://api.github.com/repos/moby/moby/contents/{+path}","compare_url":"https://api.github.com/repos/moby/moby/compare/{base}...{head}","merges_url":"https://api.github.com/repos/moby/moby/merges","archive_url":"https://api.github.com/repos/moby/moby/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/moby/moby/downloads","issues_url":"https://api.github.com/repos/moby/moby/issues{/number}","pulls_url":"https://api.github.com/repos/moby/moby/pulls{/number}","milestones_url":"https://api.github.com/repos/moby/moby/milestones{/number}","notifications_url":"https://api.github.com/repos/moby/moby/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/moby/moby/labels{/name}","releases_url":"https://api.github.com/repos/moby/moby/releases{/id}","deployments_url":"https://api.github.com/repos/moby/moby/deployments","created_at":"2013-01-18T18:10:57Z","updated_at":"2017-06-05T05:24:32Z","pushed_at":"2017-06-05T05:56:16Z","git_url":"git://github.com/moby/moby.git","ssh_url":"git@github.com:moby/moby.git","clone_url":"https://github.com/moby/moby.git","svn_url":"https://github.com/moby/moby","homepage":"https://mobyproject.org/","size":130279,"stargazers_count":43948,"watchers_count":43948,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":13050,"mirror_url":null,"open_issues_count":2671,"forks":13050,"open_issues":2671,"watchers":43948,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/moby/moby/pulls/29932"},"html":{"href":"https://github.com/moby/moby/pull/29932"},"issue":{"href":"https://api.github.com/repos/moby/moby/issues/29932"},"comments":{"href":"https://api.github.com/repos/moby/moby/issues/29932/comments"},"review_comments":{"href":"https://api.github.com/repos/moby/moby/pulls/29932/comments"},"review_comment":{"href":"https://api.github.com/repos/moby/moby/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/moby/moby/pulls/29932/commits"},"statuses":{"href":"https://api.github.com/repos/moby/moby/statuses/d0dfffdcec1c1e8beba18663ab8ba7acea77681b"}}}}
{"actor":{"display_login":"miaoyq"}}