-
Notifications
You must be signed in to change notification settings - Fork 1
/
tinylog.go
101 lines (91 loc) · 2.55 KB
/
tinylog.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
91
92
93
94
95
96
97
98
99
100
101
package tinylog
// tinylog.go
// Contains the private core functions
import (
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"time"
)
// Retrieve a generic writer
func getWriter(logToFile bool, fileOut string, writer io.Writer) io.Writer {
if logToFile && fileOut != "" {
tmp, err := os.OpenFile(fileOut, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
NewLogger(Config{}).Warningf("Failed to open log file for writing: %s", fileOut)
}
if writer != nil {
writer = io.MultiWriter(writer, tmp)
} else {
writer = tmp
}
}
return writer
}
// Retrieve the DebugWriter based on the specified config
func getDebugWriter(cfg Config) io.Writer {
var writer io.Writer
writer = nil
if cfg.LogToOutput {
writer = os.Stdout
}
debugWriter := getWriter(cfg.LogToFile, cfg.DebugFile, writer)
if debugWriter == nil {
debugWriter = ioutil.Discard
}
return debugWriter
}
// Retrieve the InfoWriter based on the specified config
func getInfoWriter(cfg Config) io.Writer {
var writer io.Writer
writer = nil
if cfg.LogToOutput {
writer = os.Stdout
}
infoWriter := getWriter(cfg.LogToFile, cfg.InfoFile, writer)
if infoWriter == nil {
infoWriter = ioutil.Discard
}
return infoWriter
}
// Retrieve the ErrorWriter based on the specified config
func getErrorWriter(cfg Config) io.Writer {
var writer io.Writer
writer = nil
if cfg.LogToOutput {
writer = os.Stderr
}
errorWriter := getWriter(cfg.LogToFile, cfg.ErrorFile, writer)
if errorWriter == nil {
errorWriter = ioutil.Discard
}
return errorWriter
}
// Remove ANSI color sequences from a string
// Useful for quickly disabling terminal color output
func removeAnsi(str string) string {
ansiPattern := "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
removeAnsi := regexp.MustCompile(ansiPattern)
return removeAnsi.ReplaceAllString(str, "")
}
// Create a formatted time string based on the current config
func (l *Logger) generateTimeString() string {
if l.PrintTime {
return l.TimeColor + time.Now().Format(l.TimePattern) + l.ResetColor
}
return ""
}
// Log a message
func (l *Logger) doLog(writer io.Writer, level int, levelText string, msg string) {
if level >= l.LogLevel {
_, _ = writer.Write([]byte(l.generateTimeString() + l.LogPrefix + levelText + msg + l.LogSuffix))
}
}
// Log a formatted message
func (l *Logger) doLogf(writer io.Writer, level int, levelText string, format string, args ...interface{}) {
if level >= l.LogLevel {
l.doLog(writer, level, levelText, fmt.Sprintf(format, args...))
}
}