-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
56 lines (50 loc) · 1.36 KB
/
logger.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
package main
/*
* Implements a simple program that outputs a random log statement at a regular interval.
*/
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
var logStatements = [4]string{
`{"Error": true, "Code": 1234, "Message": "error happened with logging system"}`,
`Another test {"Info": "Processing system events", "Code": 101} end`,
`data:100 0.5 true This is an example`,
"Note: nothing happened"}
//Convert a string to an int, but consume any error and use the default instead.
func convertToInt(s string, def int) int {
var result, err = strconv.Atoi(s)
fmt.Println(result)
if err != nil {
fmt.Println(err)
result = def
}
return result
}
//Get a random log statement
func getLogInfo() string {
return logStatements[rand.Intn(4)]
}
//Output the log statement
func logInfo(header string) {
fmt.Println(header + getLogInfo())
}
// Kickoff a timed logger with random messages.
func startLogEvents(timeInterval int, header string) {
for true {
time.Sleep(time.Duration(timeInterval) * time.Second)
logInfo(header)
}
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
// Take the log interval and logging header from the environment
logInterval := convertToInt(os.Getenv("LOG_INTERVAL"), 2)
header := os.Getenv("HEADER")
whenToStart := rand.Intn(10)
time.Sleep(time.Duration(whenToStart) * time.Second)
startLogEvents(logInterval, header)
}