@@ -24,6 +24,7 @@ function getDefaultDirectories() {
24
24
const directories = [ ] ;
25
25
26
26
const driverDirectory = getDriverDirectory ( ) ;
27
+ Logger . getInstance ( ) . debug ( `Detected driver directory: ${ driverDirectory } ` ) ;
27
28
28
29
if ( driverDirectory ) {
29
30
directories . push (
@@ -37,6 +38,7 @@ function getDefaultDirectories() {
37
38
}
38
39
39
40
const homedir = os . homedir ( ) ;
41
+ Logger . getInstance ( ) . debug ( `Detected home directory: ${ homedir } ` ) ;
40
42
41
43
if ( exists ( homedir ) ) {
42
44
directories . push (
@@ -46,9 +48,10 @@ function getDefaultDirectories() {
46
48
}
47
49
) ;
48
50
} else {
49
- Logger . getInstance ( ) . warn ( 'Home directory of the user is not present ' ) ;
51
+ Logger . getInstance ( ) . warn ( 'Home directory of the user is not defined ' ) ;
50
52
}
51
53
54
+ Logger . getInstance ( ) . debug ( `Detected default directories: ${ driverDirectory } ` ) ;
52
55
return directories ;
53
56
}
54
57
@@ -91,6 +94,8 @@ class ConfigurationError extends Error {
91
94
function levelFromString ( value ) {
92
95
const level = value . toUpperCase ( ) ;
93
96
if ( ! allLevels . includes ( level ) ) {
97
+
98
+ Logger . getInstance ( ) . error ( `Tried to create unsupported log level from string: ${ value } ` ) ;
94
99
throw new Error ( 'Unknown log level: ' + value ) ;
95
100
}
96
101
return level ;
@@ -110,46 +115,69 @@ function ConfigurationUtil(fsPromisesModule, processModule) {
110
115
* @return {Promise<ClientConfig> } Client configuration.
111
116
*/
112
117
this . getClientConfig = async function ( configFilePath ) {
118
+ Logger . getInstance ( ) . debug ( 'Retrieving client config' ) ;
119
+
113
120
const path = await findConfig ( configFilePath ) ;
114
121
if ( ! exists ( path ) || path === '' ) {
122
+ Logger . getInstance ( ) . info ( 'No config file path found. Client config will not be used.' ) ;
115
123
return null ;
116
124
}
117
125
118
126
const isFileOk = await isFileNotWritableByGroupOrOthers ( path , fsPromises ) . catch ( err => {
127
+ Logger . getInstance ( ) . warn ( 'Failed to inspect config file path permissions. Client config will not be used.' ) ;
119
128
throw new ConfigurationError ( 'Finding client configuration failed' , err ) ;
120
129
} ) ;
121
130
122
131
if ( ! isFileOk ) {
132
+ Logger . getInstance ( ) . warn ( `Config file path permissions are invalid. File: ${ path } can be modified by group or others. Client config will not be used.` ) ;
123
133
throw new ConfigurationError ( `Configuration file: ${ path } can be modified by group or others` , 'IncorrectPerms' ) ;
124
134
}
135
+ Logger . getInstance ( ) . debug ( `Config file path permissions are valid. Path: ${ path } ` ) ;
125
136
126
137
const configFileContents = await readFileConfig ( path ) ;
138
+ Logger . getInstance ( ) . info ( 'Using client configuration from path: %s' , path ) ;
139
+
127
140
return configFileContents == null ? null : parseConfigFile ( path , configFileContents ) ;
128
141
} ;
129
142
130
143
function readFileConfig ( filePath ) {
144
+ Logger . getInstance ( ) . debug ( `Reading config file. Path: ${ filePath } ` ) ;
145
+
131
146
if ( ! filePath ) {
147
+ Logger . getInstance ( ) . trace ( `Path of config file is not specified. Nothing to read. Path: ${ filePath } ` ) ;
132
148
return Promise . resolve ( null ) ;
133
149
}
134
150
return fsPromises . readFile ( filePath , { encoding : 'utf8' } )
135
151
. catch ( err => {
152
+ Logger . getInstance ( ) . debug ( `Reading configuration from the file failed. Path: ${ filePath } ` ) ;
136
153
throw new ConfigurationError ( 'Finding client configuration failed' , err ) ;
137
154
} ) ;
138
155
}
139
156
140
157
function parseConfigFile ( path , configurationJson ) {
158
+ Logger . getInstance ( ) . debug ( 'Parsing config file: %s' , path ) ;
141
159
try {
142
160
const parsedConfiguration = JSON . parse ( configurationJson ) ;
161
+ Logger . getInstance ( ) . trace ( 'Config file contains correct JSON structure. Validating the input.' ) ;
162
+
143
163
checkUnknownEntries ( parsedConfiguration ) ;
144
164
validate ( parsedConfiguration ) ;
145
- return new ClientConfig (
165
+
166
+ Logger . getInstance ( ) . debug ( 'Config file contains valid configuration input.' ) ;
167
+
168
+ const clientConfig = new ClientConfig (
146
169
path ,
147
170
new ClientLoggingConfig (
148
171
getLogLevel ( parsedConfiguration ) ,
149
172
getLogPath ( parsedConfiguration )
150
173
)
151
174
) ;
175
+
176
+ Logger . getInstance ( ) . info ( 'Client Configuration created with Log Level: %s and Log Path: %s' , clientConfig . loggingConfig . logLevel , clientConfig . loggingConfig . logPath ) ;
177
+ return clientConfig ;
178
+
152
179
} catch ( err ) {
180
+ Logger . getInstance ( ) . error ( 'Parsing client configuration failed. Used config file from path: %s' , path ) ;
153
181
throw new ConfigurationError ( 'Parsing client configuration failed' , err ) ;
154
182
}
155
183
}
@@ -170,21 +198,27 @@ function ConfigurationUtil(fsPromisesModule, processModule) {
170
198
function validateLogLevel ( configuration ) {
171
199
const logLevel = getLogLevel ( configuration ) ;
172
200
if ( logLevel == null ) {
201
+ Logger . getInstance ( ) . debug ( 'Log level is not specified.' ) ;
173
202
return ;
174
203
}
175
204
if ( ! isString ( logLevel ) ) {
176
- throw new Error ( 'Log level is not a string' ) ;
205
+ const errorMessage = 'Log level is not a string.' ;
206
+ Logger . getInstance ( ) . error ( errorMessage ) ;
207
+ throw new Error ( errorMessage ) ;
177
208
}
178
209
levelFromString ( logLevel ) ;
179
210
}
180
211
181
212
function validateLogPath ( configuration ) {
182
213
const logPath = getLogPath ( configuration ) ;
183
214
if ( logPath == null ) {
215
+ Logger . getInstance ( ) . debug ( 'Log path is not specified' ) ;
184
216
return ;
185
217
}
186
218
if ( ! isString ( logPath ) ) {
187
- throw new Error ( 'Log path is not a string' ) ;
219
+ const errorMessage = 'Log path is not a string.' ;
220
+ Logger . getInstance ( ) . error ( errorMessage ) ;
221
+ throw new Error ( errorMessage ) ;
188
222
}
189
223
}
190
224
@@ -197,21 +231,22 @@ function ConfigurationUtil(fsPromisesModule, processModule) {
197
231
}
198
232
199
233
async function findConfig ( filePathFromConnectionString ) {
234
+ Logger . getInstance ( ) . trace ( `findConfig() called with param: ${ filePathFromConnectionString } ` ) ;
200
235
if ( exists ( filePathFromConnectionString ) ) {
201
- Logger . getInstance ( ) . info ( 'Using client configuration path from a connection string: %s' , filePathFromConnectionString ) ;
236
+ Logger . getInstance ( ) . info ( 'Found client configuration path in a connection string. Path : %s' , filePathFromConnectionString ) ;
202
237
return filePathFromConnectionString ;
203
238
}
204
239
const filePathFromEnvVariable = await getFilePathFromEnvironmentVariable ( ) ;
205
240
if ( exists ( filePathFromEnvVariable ) ) {
206
- Logger . getInstance ( ) . info ( 'Using client configuration path from an environment variable: %s' , filePathFromEnvVariable ) ;
241
+ Logger . getInstance ( ) . info ( 'Found client configuration path in an environment variable. Path : %s' , filePathFromEnvVariable ) ;
207
242
return filePathFromEnvVariable ;
208
243
}
209
244
const fileFromDefDirs = await searchForConfigInDefaultDirectories ( ) ;
210
245
if ( exists ( fileFromDefDirs ) ) {
211
- Logger . getInstance ( ) . info ( 'Using client configuration path from %s directory: %s' , fileFromDefDirs . dirDescription , fileFromDefDirs . configPath ) ;
246
+ Logger . getInstance ( ) . info ( 'Found client configuration path in %s directory. Path : %s' , fileFromDefDirs . dirDescription , fileFromDefDirs . configPath ) ;
212
247
return fileFromDefDirs . configPath ;
213
248
}
214
- Logger . getInstance ( ) . info ( 'No client config file found in default directories ' ) ;
249
+ Logger . getInstance ( ) . info ( 'No client config detected. ' ) ;
215
250
return null ;
216
251
}
217
252
@@ -224,12 +259,15 @@ function ConfigurationUtil(fsPromisesModule, processModule) {
224
259
}
225
260
226
261
async function searchForConfigInDefaultDirectories ( ) {
262
+ Logger . getInstance ( ) . debug ( `Searching for config in default directories: ${ defaultDirectories } ` ) ;
227
263
for ( const directory of defaultDirectories ) {
228
264
const configPath = await searchForConfigInDictionary ( directory . dir , directory . dirDescription ) ;
229
265
if ( exists ( configPath ) ) {
266
+ Logger . getInstance ( ) . debug ( `Config found in the default directory: ${ directory . dir } . Path: ${ configPath } ` ) ;
230
267
return { configPath : configPath , dirDescription : directory . dirDescription } ;
231
268
}
232
269
}
270
+ Logger . getInstance ( ) . debug ( 'Unable to find config in any default directory.' ) ;
233
271
return null ;
234
272
}
235
273
0 commit comments