This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlogger.go
81 lines (72 loc) · 1.62 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Everything logging
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"time"
)
const TimeFormat = "2006-01-02 15:04:05"
type LogMessage struct {
Severity Severity
Stream
Result
}
func LogKeeper(verbose bool) {
var skip error
var logw *bufio.Writer
defer func() {
if r := recover(); r != nil {
fmt.Println("Logger trace:", r)
}
}()
logq = make(chan LogMessage, 1024)
logf, skip := os.Create(cfg.ErrorLog)
if skip == nil {
logw = bufio.NewWriter(logf)
fmt.Printf("Error log: %s\n", cfg.ErrorLog)
} else {
println("Can't create file for error log. Error logging to file skiped.")
}
for {
timeout := make(chan bool, 1)
go func() {
time.Sleep(2 * time.Second)
timeout <- true
}()
select {
case msg := <-logq:
if skip == nil {
logw.WriteString(msg.Started.Format(TimeFormat))
logw.WriteRune(' ')
switch msg.Severity {
case WARNING:
logw.WriteString("warning")
case ERROR:
logw.WriteString("error")
}
logw.WriteString(": ")
logw.WriteString(StreamErr2String(msg.Result.ErrType))
logw.WriteRune(' ')
logw.WriteString(strconv.Itoa(msg.HTTPCode))
logw.WriteRune(' ')
logw.WriteString(strconv.FormatInt(msg.ContentLength, 10))
logw.WriteRune(' ')
logw.WriteString(msg.Elapsed.String())
logw.WriteRune(' ')
logw.WriteString(msg.Group)
logw.WriteString(": ")
logw.WriteString(msg.Name)
logw.WriteRune('\n')
}
case <-timeout:
if skip == nil {
_ = logw.Flush()
}
}
}
}
func Log(severity Severity, stream Stream, taskres Result) {
logq <- LogMessage{Severity: severity, Stream: stream, Result: taskres}
}