在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
package main import ( "os" "errors" "math/rand" "time" "log" "strconv" ) func init() { rand.Seed(time.Now().Unix()) file := "/www/logs/test.log"; logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) if err != nil { panic(err) } log.SetOutput(logFile) // 将文件设置为log输出的文件 log.SetPrefix("[elk-test]") log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC) } func main() { for { go func() { strs := []string{"浏览页面", "评论商品", "加入收藏", "加入购物车", "提交订单", "使用优惠券", "领取优惠券", "搜索", "查看订单"} a, _ := Random(strs, 1) now := time.Now() date := now.Format("2006-01-02 15:04:05") userId := rand.Intn(10000) result := "brady|" +strconv.Itoa(userId+1) + "|" + a + "|" + date; log.Println(result) // log 还是可以作为输出的前缀 }() time.Sleep(1*time.Second) } } func Random(strings []string, length int) (string, error) { if len(strings) <= 0 { return "", errors.New("the length of the parameter strings should not be less than 0") } if length <= 0 || len(strings) <= length { return "", errors.New("the size of the parameter length illegal") } for i := len(strings) - 1; i > 0; i-- { num := rand.Intn(i + 1) strings[i], strings[num] = strings[num], strings[i] } str := "" for i := 0; i < length; i++ { str += strings[i] } return str, nil }
结果 [elk-test]2020/07/05 01:10:22 main.go:42: brady|1203|加入收藏|2020-07-05 09:10:22 [elk-test]2020/07/05 01:10:23 main.go:42: brady|6144|领取优惠券|2020-07-05 09:10:23 [elk-test]2020/07/05 01:10:24 main.go:42: brady|7903|提交订单|2020-07-05 09:10:24 [elk-test]2020/07/05 01:10:25 main.go:42: brady|9768|领取优惠券|2020-07-05 09:10:25 [elk-test]2020/07/05 01:10:26 main.go:42: brady|6031|查看订单|2020-07-05 09:10:26 [elk-test]2020/07/05 01:10:27 main.go:42: brady|7422|评论商品|2020-07-05 09:10:27 [elk-test]2020/07/05 01:10:28 main.go:42: brady|497|查看订单|2020-07-05 09:10:28 [elk-test]2020/07/05 01:10:29 main.go:42: brady|6295|搜索|2020-07-05 09:10:29 [elk-test]2020/07/05 01:10:30 main.go:42: brady|7660|搜索|2020-07-05 09:10:30 [elk-test]2020/07/05 01:10:31 main.go:42: brady|1622|加入购物车|2020-07-05 09:10:31 [elk-test]2020/07/05 01:10:32 main.go:4 |
请发表评论