-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogging.go
83 lines (68 loc) · 1.57 KB
/
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
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
package spm
import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
"github.com/mattn/go-isatty"
)
type Logging struct {
Prefix string
LogColor int
Logfile *os.File
}
func NewLogging(name string) (*Logging, error) {
fileName := fmt.Sprintf("/tmp/spm_%s.log", name)
// create logfile with given name
logfile, err := os.OpenFile(fileName, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
if err != nil {
return nil, err
}
// create random log color code
code := genColorCode()
prefix := loggerPrefix(code, name)
return &Logging{
Prefix: prefix,
LogColor: code,
Logfile: logfile,
}, nil
}
// Write writes given string into Logfile
func (l *Logging) Write(s string) error {
if _, err := l.Logfile.WriteString(s + "\n"); err != nil {
return err
}
return nil
}
// Output reads the in then writes into both stdout and logfile
func (l *Logging) Output(in *bufio.Scanner) error {
for in.Scan() {
log := l.Prefix + in.Text()
fmt.Fprintln(os.Stdout, log)
l.Write(log)
}
if err := in.Err(); err != nil {
return err
}
return nil
}
func (l *Logging) Close() error {
if err := l.Logfile.Close(); err != nil {
return err
}
return nil
}
// LoggerPrefix wraps given string and time with unix color code, as prefix
func loggerPrefix(code int, s string) string {
t := time.Now().Format("15:04:05 PM")
if isatty.IsTerminal(os.Stdout.Fd()) {
return fmt.Sprintf("\033[38;5;%dm%s %s | \033[0m", code, t, s)
}
return fmt.Sprintf("%s %s | ", t, s)
}
func genColorCode() (code int) {
rand.Seed(int64(time.Now().Nanosecond()))
code = rand.Intn(231) + 1
return
}