forked from lateefj/fresh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
45 lines (37 loc) · 924 Bytes
/
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
package runner
import (
"fmt"
logPkg "log"
"os"
"strings"
"time"
)
type logFunc func(string, ...interface{})
var logger = logPkg.New(os.Stderr, "", 0)
const timeFormat = "15:04:05.000"
func newLogFunc(prefix string) func(string, ...interface{}) {
color, clear := "", ""
if settings["colors"] == "1" {
color = fmt.Sprintf("\033[%sm", logColor(prefix))
clear = fmt.Sprintf("\033[%sm", colors["reset"])
}
return func(format string, v ...interface{}) {
now := time.Now()
timeString := now.Format(timeFormat)
format = fmt.Sprintf("%s %s%s%s %s", timeString, color, prefix, clear, format)
logger.Printf(format, v...)
}
}
func fatal(err error) {
logger.Fatal(err)
}
type appLogWriter struct{}
func (a appLogWriter) Write(p []byte) (n int, err error) {
for _, line := range strings.Split(string(p), "\n") {
if strings.TrimSpace(line) == "" {
continue
}
appLog(line)
}
return len(p), nil
}