-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
47 lines (45 loc) · 1.07 KB
/
Copy pathlog.go
File metadata and controls
47 lines (45 loc) · 1.07 KB
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
package main
import (
"fmt"
"io"
"os"
"time"
)
// Outputs messages with ISO8601 timestamps and terminal colors for severity levels
func ShellLogger(args []string, stdin io.Reader) error {
var severity, message string
args = args[1:] // We don't need "log"
switch len(args) {
case 1:
severity = "info"
message = args[0]
case 2:
message = args[0]
severity = args[1]
default:
return fmt.Errorf("log needs at least one argument, the message to log. If a second argument is provided, it's the message's severity. info|warning|error|debug have different colors.")
}
severity_map := map[string]ANSIColor{
"err": Red,
"error": Red,
"exception": Red,
"fatal": Red,
"warn": Yellow,
"warning": Yellow,
"ok": Green,
"info": Green,
"debug": Blue,
}
now := time.Now().Format(time.RFC3339) + "\t"
if useANSIColor {
color, ok := severity_map[severity]
if !ok {
// Default
color = Green
}
fmt.Fprintln(os.Stderr, ColorWrap(now+message, color))
return nil
}
fmt.Fprintln(os.Stderr, now+message)
return nil
}