-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (59 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"fmt"
"os"
"runtime"
"strconv"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
func main() {
//log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.DebugLevel)
logFilePath := "logs.txt"
noOfGoRoutines := 10
if len(os.Args) > 1 {
logFilePath = os.Args[1]
}
if len(os.Args) > 2 {
noOfGoRoutines, _ = strconv.Atoi(os.Args[2])
}
var file, err = os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
fmt.Println("Could Not Open Log File : " + err.Error())
}
log.SetOutput(file)
var wg sync.WaitGroup
for i := 0; i < noOfGoRoutines; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup, identifier int) {
defer wg.Done()
for {
log.WithFields(
log.Fields{
"goid": getGID(),
"identifier": identifier,
},
).Info("Something happened")
log.WithFields(
log.Fields{
"goid": getGID(),
"identifier": identifier,
},
).Debug("Debugging something")
time.Sleep(1 * time.Second)
}
}(&wg, i)
}
wg.Wait()
}
func getGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}