/
logging.go
112 lines (100 loc) · 4.2 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
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
package main
// @logging.go contains the function that will create logs folder and all logs file
// for storing web requests, jobs processing events and jobs deletion operations.
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"time"
)
// custom logger for web requests.
var weblog *log.Logger
// custom logger for api calls.
var apilog *log.Logger
// custom logger for tracing jobs deletion.
var deletedjobslog *log.Logger
// custom logger for tracing jobs processing.
var jobslog *log.Logger
// setupLoggers ensures the log folder exist and configures all files needed for logging.
func setupLoggers() {
// build log folder based on current launch day.
logsFolderName := fmt.Sprintf("%d%02d%02d.logs", time.Now().Year(), time.Now().Month(), time.Now().Day())
logsFolderPath := filepath.Join(Config.LogFoldersLocation, logsFolderName)
// use current day log folder to store all logs files. if the folder does not exist, create it.
info, err := os.Stat(logsFolderPath)
if errors.Is(err, os.ErrNotExist) {
// folder does not exist. create the full nested path.
err := os.MkdirAll(logsFolderPath, 0755)
if err != nil {
// failed to create directory.
log.Printf("program aborted - failed to create %q logging folder - errmsg: %v", logsFolderPath, err)
// try to remove PID file.
os.Remove(Config.WorkerPidFilePath)
os.Exit(1)
}
} else {
// folder or path already exists but abort if not a directory.
if !info.IsDir() {
log.Printf("%q already exists but it is not a folder so check before continue - errmsg: %v\n", logsFolderPath, err)
// try to remove PID file.
os.Remove(Config.WorkerPidFilePath)
os.Exit(0)
}
}
// create the log file for web server requests.
weblogfile, err := os.OpenFile(filepath.Join(logsFolderPath, Config.WebRequestsLogFile), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Printf("program aborted - failed to create web requests log file - errmsg: %v\n", err)
// try to remove PID file.
os.Remove(Config.WorkerPidFilePath)
os.Exit(1)
}
// setup logging format and parameters.
if Config.EnableLogsTimestampInUTC {
weblog = log.New(weblogfile, "", log.LstdFlags|log.Lshortfile|log.LUTC)
}
weblog = log.New(weblogfile, "", log.LstdFlags|log.Lshortfile)
// create the log file for api calls.
apilogfile, err := os.OpenFile(filepath.Join(logsFolderPath, Config.ApiRequestsLogFile), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Printf("program aborted - failed to create api calls log file - errmsg: %v\n", err)
// try to remove PID file.
os.Remove(Config.WorkerPidFilePath)
os.Exit(1)
}
// setup logging format and parameters.
if Config.EnableLogsTimestampInUTC {
apilog = log.New(apilogfile, "", log.LstdFlags|log.Lshortfile|log.LUTC)
}
apilog = log.New(apilogfile, "", log.LstdFlags|log.Lshortfile)
// create file to log deleted jobs by cleanupMapResults goroutine.
deletedjobslogfile, err := os.OpenFile(filepath.Join(logsFolderPath, Config.JobsDeletionLogFile), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Printf("program aborted - failed to create jobs deletion log file - errmsg: %v\n", err)
// try to remove PID file.
os.Remove(Config.WorkerPidFilePath)
os.Exit(1)
}
// setup logging format and parameters.
if Config.EnableLogsTimestampInUTC {
deletedjobslog = log.New(deletedjobslogfile, "", log.LstdFlags|log.Lshortfile|log.LUTC)
}
deletedjobslog = log.New(deletedjobslogfile, "", log.LstdFlags|log.Lshortfile)
// create file to log jobs related activities.
jobslogfile, err := os.OpenFile(filepath.Join(logsFolderPath, Config.JobsProcessLogFile), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Printf("program aborted - failed to create jobs processing log file - errmsg: %v\n", err)
// try to remove PID file.
os.Remove(Config.WorkerPidFilePath)
os.Exit(1)
}
// setup logging format and parameters.
if Config.EnableLogsTimestampInUTC {
jobslog = log.New(jobslogfile, "", log.LstdFlags|log.Lshortfile|log.LUTC)
}
jobslog = log.New(jobslogfile, "", log.LstdFlags|log.Lshortfile)
log.Printf("created logs folder and files <%s> <%s> <%s> <%s>.\n", Config.WebRequestsLogFile,
Config.ApiRequestsLogFile, Config.JobsProcessLogFile, Config.JobsDeletionLogFile)
}