https:\/\/github.com\/lestrrat\/go-file-rotatelogs<\/a> for log rotation. <\/p>\n\n\n\nExample:<\/strong> In the example below we are setting <\/p>\n\n\n\n- MaxAge<\/strong> to 10 seconds. It sets the max-age of a log file before it is purged from the file system. Meaning the file will be deleted after 10 seconds. See<\/li><\/ul>\n\n\n\n
rotatelogs.WithMaxAge(time.Second*10)<\/code><\/pre>\n\n\n\n- RotationTime <\/strong>is 1 second. <\/strong>It sets the time when the file will be rotated. So the file will be rotated every second. See <\/li><\/ul>\n\n\n\n
rotatelogs.WithRotationTime(time.Second*1)<\/code><\/pre>\n\n\n\n- Location <\/strong>is \/var\/log\/service\/ . See<\/li><\/ul>\n\n\n\n
path := \"\/var\/log\/service\/\"<\/code><\/pre>\n\n\n\n- Pattern<\/strong> of the file is “old.UTC..2019-11-30.22:40:10” . See<\/li><\/ul>\n\n\n\n
fmt.Sprintf(\"%s.%s\", path, \"%Y-%m-%d.%H:%M:%S\")<\/code><\/pre>\n\n\n\n- Link Path <\/strong>is\/var\/log\/service\/current. This file would be a symlink for the actual file. See<\/li><\/ul>\n\n\n\n
rotatelogs.WithLinkName(\"\/var\/log\/service\/current\")<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Code:<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n \"fmt\"\n \"log\"\n \"time\"\n rotatelogs \"github.com\/lestrrat\/go-file-rotatelogs\"\n \"github.com\/sirupsen\/logrus\"\n)\n\nfunc initiLogger() {\n path := \"\/var\/log\/service\/old.UTC.\"\n writer, err := rotatelogs.New(\n fmt.Sprintf(\"%s.%s\", path, \"%Y-%m-%d.%H:%M:%S\"),\n rotatelogs.WithLinkName(\"\/var\/log\/service\/current\"),\n rotatelogs.WithMaxAge(time.Second*10),\n rotatelogs.WithRotationTime(time.Second*1),\n )\n if err != nil {\n log.Fatalf(\"Failed to Initialize Log File %s\", err)\n }\n log.SetOutput(writer)\n return\n}\n\nfunc main() {\n initiLogger()\n for i := 0; i < 100; i++ {\n time.Sleep(time.Second * 1)\n log.Printf(\"Hello, World!\")\n }\n fmt.Scanln()\n}<\/code><\/pre>\n\n\n\nGo to the location \/var\/log\/service\/. You will notice<\/p>\n\n\n\n
- Files are rotated every second.<\/li>
- Max age of the file is 10 seconds. So every file will be deleted after 10 seconds.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"
This article is about how we rotate log files from within the go app without having to worry about the disk full alerts. We are going to use https:\/\/github.com\/lestrrat\/go-file-rotatelogs for log rotation….<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4,95,96],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang","tag-logger","tag-rotate"],"yoast_head":"\n
Go Logger Rotation - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n