/
log.go
83 lines (73 loc) · 2.49 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
package pgengine
import (
"context"
"fmt"
"os"
"time"
)
const (
// black = 30
red = 31
green = 32
yellow = 33
// purple = 34
// magenta = 35
blue = 36
gray = 37
)
var levelColors = map[string]int{
"PANIC": red,
"ERROR": red,
"REPAIR": red,
"USER": yellow,
"LOG": blue,
"NOTICE": green,
"DEBUG": gray}
// VerboseLogLevel specifies if log messages with level LOG should be logged
var VerboseLogLevel = true
func getColorizedLevel(level string) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", levelColors[level], level)
}
// GetLogPrefix perform formatted logging
func GetLogPrefix(level string) string {
return fmt.Sprintf("[%v | %s | %-15s]: %%s", time.Now().Format("2006-01-02 15:04:05.000"), ClientName, getColorizedLevel(level))
}
// GetLogPrefixLn perform formatted logging with new line at the end
func GetLogPrefixLn(level string) string {
return GetLogPrefix(level) + "\n"
}
const logTemplate = `INSERT INTO timetable.log(pid, client_name, log_level, message) VALUES ($1, $2, $3, $4)`
// LogToDB performs logging to configuration database ConfigDB initiated during bootstrap
func LogToDB(level string, msg ...interface{}) {
if !VerboseLogLevel {
switch level {
case
"DEBUG", "NOTICE":
return
}
}
s := fmt.Sprintf(GetLogPrefix(level), fmt.Sprint(msg...))
fmt.Println(s)
if ConfigDb != nil {
_, err := ConfigDb.Exec(logTemplate, os.Getpid(), ClientName, level, fmt.Sprint(msg...))
if err != nil && ConfigDb.Ping() != nil {
if ReconnectDbAndFixLeftovers(context.TODO()) {
_, _ = ConfigDb.Exec(logTemplate, os.Getpid(), ClientName, level, fmt.Sprint(msg...))
}
}
}
}
// LogChainElementExecution will log current chain element execution status including retcode
func LogChainElementExecution(chainElemExec *ChainElementExecution, retCode int, output string) {
_, err := ConfigDb.Exec("INSERT INTO timetable.execution_log (chain_execution_config, chain_id, task_id, name, script, "+
"kind, last_run, finished, returncode, pid, output, client_name) "+
"VALUES ($1, $2, $3, $4, $5, $6, clock_timestamp() - $7 :: interval, clock_timestamp(), $8, $9, "+
"NULLIF($10, ''), $11)",
chainElemExec.ChainConfig, chainElemExec.ChainID, chainElemExec.TaskID, chainElemExec.TaskName,
chainElemExec.Script, chainElemExec.Kind,
fmt.Sprintf("%d microsecond", chainElemExec.Duration),
retCode, os.Getpid(), output, ClientName)
if err != nil {
LogToDB("ERROR", "Error occurred during logging current chain element execution status including retcode: ", err)
}
}