-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: can load custom log4js config
log4js.json
- Load custom `log4js.json` if exists in config directory - Load default `log4js.default.json` config when `log4js.json` is not exists
- Loading branch information
1 parent
0a46a5a
commit c05c372
Showing
3 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const { promises, statSync, Stats } = require("fs"); | ||
|
||
/** | ||
* @typedef Error | ||
* @property {string} code | ||
*/ | ||
|
||
/** | ||
* @typedef Options | ||
* @property {boolean} includeDirectories | ||
*/ | ||
|
||
|
||
/** | ||
* | ||
* @param {Error} e | ||
* @returns | ||
*/ | ||
function handleError(e) { | ||
if (e.code === "ENOENT") { | ||
return false; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {Stats} result | ||
* @param {Options} options | ||
* @returns | ||
*/ | ||
function handleResult(result, options) { | ||
return ( | ||
result.isFile() || | ||
Boolean(options?.includeDirectories && result.isDirectory()) | ||
); | ||
} | ||
|
||
/** | ||
* asynchronous function for checking file is exists | ||
* @param {string} path | ||
* @param {Options} options | ||
* @returns {boolean} | ||
*/ | ||
async function fileExists( | ||
path, | ||
options = {} | ||
) { | ||
return promises | ||
.stat(path) | ||
.then((result) => { | ||
return handleResult(result, options); | ||
}) | ||
.catch((e) => { | ||
return handleError(e); | ||
}); | ||
} | ||
|
||
/** | ||
* synchronous function for checking file is exists | ||
* @param {string} path | ||
* @param {Options} options | ||
* @returns {boolean} | ||
*/ | ||
function fileExistsSync( | ||
path, | ||
options | ||
) { | ||
try { | ||
const result = statSync(path); | ||
return handleResult(result, options); | ||
} catch (e) { | ||
return handleError(e); | ||
} | ||
} | ||
|
||
module.exports.fileExists = fileExists; | ||
module.exports.fileExistsSync = fileExistsSync; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { fileExistsSync } = require("./fileExist"); | ||
const { configure, getLogger } = require('log4js'); | ||
configure(path.join(__dirname, "../config/log4js.default.json")); | ||
const USER_LOG_CONFIG_FILENAME = path.join(__dirname, "../config/log4js.json"); | ||
const DEFAULT_LOG_CONFIG_FILENAME = path.join(__dirname, "../config/log4js.default.json"); | ||
if (fileExistsSync(USER_LOG_CONFIG_FILENAME)) { | ||
configure(USER_LOG_CONFIG_FILENAME); | ||
} else { | ||
configure(DEFAULT_LOG_CONFIG_FILENAME); | ||
} | ||
let raccoonLogger = getLogger(); | ||
|
||
module.exports.logger = raccoonLogger; |