-
Notifications
You must be signed in to change notification settings - Fork 125
/
easy_logging.go
191 lines (176 loc) · 5.29 KB
/
easy_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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package gosnowflake
import (
"errors"
"fmt"
"io"
"os"
"path"
"runtime"
"strings"
"sync"
)
type initTrials struct {
everTriedToInitialize bool
clientConfigFileInput string
configureCounter int
mu sync.Mutex
}
var easyLoggingInitTrials = initTrials{
everTriedToInitialize: false,
clientConfigFileInput: "",
configureCounter: 0,
mu: sync.Mutex{},
}
func (i *initTrials) setInitTrial(clientConfigFileInput string) {
i.everTriedToInitialize = true
i.clientConfigFileInput = clientConfigFileInput
}
func (i *initTrials) increaseReconfigureCounter() {
i.configureCounter++
}
func initEasyLogging(clientConfigFileInput string) error {
easyLoggingInitTrials.mu.Lock()
defer easyLoggingInitTrials.mu.Unlock()
if !allowedToInitialize(clientConfigFileInput) {
logger.Info("Skipping Easy Logging initialization as it is not allowed to initialize")
return nil
}
logger.Infof("Trying to initialize Easy Logging")
config, configPath, err := getClientConfig(clientConfigFileInput)
if err != nil {
logger.Errorf("Failed to initialize Easy Logging, err: %s", err)
return easyLoggingInitError(err)
}
if config == nil {
logger.Info("Easy Logging is disabled as no config has been found")
easyLoggingInitTrials.setInitTrial(clientConfigFileInput)
return nil
}
var logLevel string
logLevel, err = getLogLevel(config.Common.LogLevel)
if err != nil {
logger.Errorf("Failed to initialize Easy Logging, err: %s", err)
return easyLoggingInitError(err)
}
var logPath string
logPath, err = getLogPath(config.Common.LogPath)
if err != nil {
logger.Errorf("Failed to initialize Easy Logging, err: %s", err)
return easyLoggingInitError(err)
}
logger.Infof("Initializing Easy Logging with logPath=%s and logLevel=%s from file: %s", logPath, logLevel, configPath)
err = reconfigureEasyLogging(logLevel, logPath)
if err != nil {
logger.Errorf("Failed to initialize Easy Logging, err: %s", err)
}
easyLoggingInitTrials.setInitTrial(clientConfigFileInput)
easyLoggingInitTrials.increaseReconfigureCounter()
return err
}
func easyLoggingInitError(err error) error {
return &SnowflakeError{
Number: ErrCodeClientConfigFailed,
Message: errMsgClientConfigFailed,
MessageArgs: []interface{}{err.Error()},
}
}
func reconfigureEasyLogging(logLevel string, logPath string) error {
newLogger := CreateDefaultLogger()
err := newLogger.SetLogLevel(logLevel)
if err != nil {
return err
}
var output io.Writer
var file *os.File
output, file, err = createLogWriter(logPath)
if err != nil {
return err
}
newLogger.SetOutput(output)
err = newLogger.CloseFileOnLoggerReplace(file)
if err != nil {
logger.Errorf("%s", err)
}
logger.Replace(&newLogger)
return nil
}
func createLogWriter(logPath string) (io.Writer, *os.File, error) {
if strings.EqualFold(logPath, "STDOUT") {
return os.Stdout, nil, nil
}
logFileName := path.Join(logPath, "snowflake.log")
file, err := os.OpenFile(logFileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0640)
if err != nil {
return nil, nil, err
}
return file, file, nil
}
func allowedToInitialize(clientConfigFileInput string) bool {
triedToInitializeWithoutConfigFile := easyLoggingInitTrials.everTriedToInitialize && easyLoggingInitTrials.clientConfigFileInput == ""
isAllowedToInitialize := !easyLoggingInitTrials.everTriedToInitialize || (triedToInitializeWithoutConfigFile && clientConfigFileInput != "")
if !isAllowedToInitialize && easyLoggingInitTrials.clientConfigFileInput != clientConfigFileInput {
logger.Warnf("Easy logging will not be configured for CLIENT_CONFIG_FILE=%s because it was previously configured for a different client config", clientConfigFileInput)
}
return isAllowedToInitialize
}
func getLogLevel(logLevel string) (string, error) {
if logLevel == "" {
logger.Warn("LogLevel in client config not found. Using default value: OFF")
return levelOff, nil
}
return toLogLevel(logLevel)
}
func getLogPath(logPath string) (string, error) {
logPathOrDefault := logPath
if logPath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("user home directory is not accessible, err: %w", err)
}
logPathOrDefault = homeDir
logger.Warnf("LogPath in client config not found. Using user home directory as a default value: %s", logPathOrDefault)
}
pathWithGoSubdir := path.Join(logPathOrDefault, "go")
exists, err := dirExists(pathWithGoSubdir)
if err != nil {
return "", err
}
if !exists {
err = os.MkdirAll(pathWithGoSubdir, 0700)
if err != nil {
return "", err
}
}
logDirPermValid, perm, err := isDirAccessCorrect(pathWithGoSubdir)
if err != nil {
return "", err
}
if !logDirPermValid {
logger.Warnf("Log directory: %s could potentially be accessed by others. Directory chmod: 0%o", pathWithGoSubdir, *perm)
}
return pathWithGoSubdir, nil
}
func isDirAccessCorrect(dirPath string) (bool, *os.FileMode, error) {
if runtime.GOOS == "windows" {
return true, nil, nil
}
dirStat, err := os.Stat(dirPath)
if err != nil {
return false, nil, err
}
perm := dirStat.Mode().Perm()
if perm != 0700 {
return false, &perm, nil
}
return true, &perm, nil
}
func dirExists(dirPath string) (bool, error) {
stat, err := os.Stat(dirPath)
if err == nil {
return stat.IsDir(), nil
}
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}