-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpool_log.go
60 lines (51 loc) · 1.4 KB
/
pool_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
// ** goworkerpool.com **********************************************************************************************
// ** github.com/enriquebris/goworkerpool **
// ** v0.10.0 *******************************************************************************************************
package goworkerpool
import (
"fmt"
"log"
)
// Pool Log data
type PoolLog struct {
Code string
Message string
Error error
}
const (
logError = "log.error"
logChannel = "log.channel"
)
// SetLogChan sets log channel. System logs will be sent over this channel.
func (st *Pool) SetLogChan(ch chan PoolLog) {
st.logChan = ch
}
// log logs to default output and to log channel
func (st *Pool) log(code string, message string, err error) {
st.logToDefaultOutput(code, message, err)
if st.logChan != nil {
select {
case st.logChan <- PoolLog{
Code: code,
Message: message,
Error: err,
}:
default:
st.logToDefaultOutput(logChannel, code+" - "+message, err)
}
}
}
// logToDefaultOutput logs to default output
func (st *Pool) logToDefaultOutput(code string, message string, err error) {
if st.logVerbose {
message := fmt.Sprintf("[%v] :: %v\n", code, message)
if err != nil {
message += fmt.Sprintf("error: %v\n", err.Error())
}
log.Print(message)
}
}
// logError logs a given error
func (st *Pool) logError(message string, err error) {
st.log(logError, message, err)
}