forked from c9845/fresher
/
logger.go
90 lines (78 loc) · 2.83 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
82
83
84
85
86
87
88
89
90
package runner
import (
"fmt"
"github.com/mattn/go-colorable"
"log"
"github.com/dityaaa/ignite/config"
)
// Loggers. Different colors for different things to stand out better. These are
// initialized in Configure(). Colorizing also helps makes fresher related logs stand
// out from the binary being run logs.
var (
events coloredLogger //for file changes, build, run.
warn coloredLogger //verbose logging, more details about file changes, builds, etc.
errs coloredLogger //errors
)
// getColorCode returns an escaped color code sequence for use in log.Print... funcs.
// If an unknown color is given, default to white text since most terminals have a
// dark background.
//
// I will admit I do not understand exactly what the Sprintf calls are doing. I know
// it is an escape sequence, that is all.
func getColorCode(color string) string {
switch color {
case "black":
return fmt.Sprintf("\033[%sm", "30")
case "red":
return fmt.Sprintf("\033[%sm", "31")
case "green":
return fmt.Sprintf("\033[%sm", "32")
case "yellow":
return fmt.Sprintf("\033[%sm", "33")
case "blue":
return fmt.Sprintf("\033[%sm", "34")
case "magenta":
return fmt.Sprintf("\033[%sm", "35")
case "cyan":
return fmt.Sprintf("\033[%sm", "36")
case "white":
return fmt.Sprintf("\033[%sm", "37")
default:
log.Println("unknown color, defaulting to white")
return fmt.Sprintf("\033[%sm", "37")
}
}
// coloredLogger stores details about the logger.
type coloredLogger struct {
color string
colorCode string
prefix string
}
// logger handles outputing colored logs. Use standard logging format just to be
// consistent with default golang logging and what users would most likely expect.
//
// The colorable package is needed to handle colorizing Windows logs; if we didn't
// care about Windows we could just use os.Stderr instead.
var loggerFlags = log.LstdFlags // use log.Ldate | log.Ltime | log.Lmicroseconds for debugging.
var logger = log.New(colorable.NewColorableStderr(), "", loggerFlags)
// newLogger returns a coloredLogger for calling Printf on with the resulting log
// colored and prefixed accordingly.
func newLogger(prefix, color string) coloredLogger {
colorCode := getColorCode(color)
return coloredLogger{color, colorCode, prefix}
}
// Printf calls log.Printf with color sequences surrounding some of the text.
func (c *coloredLogger) Printf(format string, v ...interface{}) {
resetCode := fmt.Sprintf("\033[%sm", "0")
format = fmt.Sprintf("%s%s |%s %s", c.colorCode, c.prefix, resetCode, format)
logger.Printf(format, v...)
}
// Verbosef calls Printf if, and only if, verbose logging is enabled. This alleviates
// us from having to put "if" blocks around Printf to check if verbose logging is
// enabled.
func (c *coloredLogger) Verbosef(format string, v ...interface{}) {
if !config.Data().Verbose {
return
}
c.Printf(format, v...)
}