-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
37 lines (31 loc) · 831 Bytes
/
logging.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
package main
import (
"encoding/json"
"log"
)
type LogQuestion struct {
QuestionId int `json:"question_id"`
Question string `json:"question"`
Answer string `json:"answer"`
Status string `json:"status"`
}
type LogEntry struct {
UserToken string `json:"user_token"`
Username string `json:"username"`
Timestamp string `json:"timestamp"`
QuizAttempt []LogQuestion `json:"quiz_attempt"`
Status string `json:"status"`
}
func logAttempt(logEntry LogEntry) {
data, err := json.Marshal(logEntry)
if err != nil {
log.Printf("[logAttempt] failed to serialize log entry: %v\n", err)
return
}
logMutex.Lock()
defer logMutex.Unlock()
_, err = logFile.Write(append(data, '\n'))
if err != nil {
log.Printf("[logAttempt] failed to write log entry: %v\n", err)
}
}