forked from scipipe/scipipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
172 lines (150 loc) · 3.75 KB
/
log.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package scipipe
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
var (
// Trace is a log handler for extremely detailed level logs. It is so far
// sparely used in scipipe.
Trace *log.Logger
// Debug is a log handler for debugging level logs
Debug *log.Logger
// Info is a log handler for information level logs
Info *log.Logger
// Audit is a log handler for audit level logs
Audit *log.Logger
// Warning is a log handler for warning level logs
Warning *log.Logger
// Error is a log handler for error level logs
Error *log.Logger
logExists bool
)
// InitLog initiates logging handlers
func InitLog(
traceHandle io.Writer,
debugHandle io.Writer,
infoHandle io.Writer,
auditHandle io.Writer,
warningHandle io.Writer,
errorHandle io.Writer) {
if !logExists {
Trace = log.New(traceHandle,
"TRACE ",
log.Ldate|log.Ltime|log.Lshortfile)
Debug = log.New(debugHandle,
"DEBUG ",
log.Ldate|log.Ltime|log.Lshortfile)
Info = log.New(infoHandle,
"INFO ",
log.Ldate|log.Ltime)
// This level is the one suggested to use when running scientific workflows, to retain audit
// information
Audit = log.New(auditHandle,
"AUDIT ",
log.Ldate|log.Ltime)
Warning = log.New(warningHandle,
"WARNING ",
log.Ldate|log.Ltime)
Error = log.New(errorHandle,
"ERROR ",
log.Ldate|log.Ltime)
logExists = true
}
}
// InitLogDebug initiates logging with level=DEBUG
func InitLogDebug() {
InitLog(
ioutil.Discard,
os.Stdout,
os.Stdout,
os.Stdout,
os.Stdout,
os.Stderr,
)
}
// InitLogInfo initiates logging with level=INFO
func InitLogInfo() {
InitLog(
ioutil.Discard,
ioutil.Discard,
os.Stdout,
os.Stdout,
os.Stdout,
os.Stderr,
)
}
// InitLogAudit initiate logging with level=AUDIT
func InitLogAudit() {
InitLog(
ioutil.Discard,
ioutil.Discard,
ioutil.Discard,
os.Stdout,
os.Stdout,
os.Stderr,
)
}
// InitLogAuditToFile initiate logging with level=AUDIT, and write that to
// fileName
func InitLogAuditToFile(filePath string) {
dir := filepath.Dir(filePath)
err := os.MkdirAll(dir, 0777)
if err != nil {
fmt.Println("Could not create directory: " + dir + " " + err.Error())
}
logFile, err := os.Create(filePath)
if err != nil {
fmt.Println("Could not create log file: " + filePath + " " + err.Error())
}
multiWrite := io.MultiWriter(os.Stdout, logFile)
InitLog(
ioutil.Discard,
ioutil.Discard,
ioutil.Discard,
multiWrite,
multiWrite,
multiWrite,
)
}
// InitLogWarning initiates logging with level=WARNING
func InitLogWarning() {
InitLog(
ioutil.Discard,
ioutil.Discard,
ioutil.Discard,
ioutil.Discard,
os.Stdout,
os.Stderr,
)
}
// InitLogError initiates logging with level=ERROR
func InitLogError() {
InitLog(
ioutil.Discard,
ioutil.Discard,
ioutil.Discard,
ioutil.Discard,
ioutil.Discard,
os.Stderr,
)
}
// auditLogPattern decides the format of Audit log messages across SciPipe
const auditLogPattern = "| %-32s | %s\n"
// LogAuditln logs a pretty printed log message with the AUDIT log level, where
// componentName is a name of a process, task, workflow or similar that
// generates the message, while message is a custom message (can be specified as
// multiple strings, which will then be formatted in the manner of fmt.Println).
func LogAuditln(componentName string, message string) {
Audit.Printf(auditLogPattern, componentName, message)
}
// LogAuditf logs a pretty printed log message with the AUDIT log level, where
// componentName is a name of a process, task, workflow or similar that
// generates the message, while message and values are formatted in the manner
// of fmt.Printf
func LogAuditf(componentName string, message string, values ...interface{}) {
Audit.Printf(auditLogPattern, componentName, fmt.Sprintf(message, values...))
}