-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ec86fd
commit 2915e29
Showing
7 changed files
with
181 additions
and
13 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
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
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,46 @@ | ||
const options = { | ||
disableColors: false | ||
}; | ||
|
||
const textToFormat = (open, close, searchRegex, replaceValue) => (txt) => | ||
!options.disableColors | ||
? open + | ||
(~(txt += "").indexOf(close, 4) // skip opening \x1b[ | ||
? txt.replace(searchRegex, replaceValue) | ||
: txt) + | ||
close | ||
: txt | ||
|
||
const init = (open, close) => { | ||
return textToFormat( | ||
`\x1b[${open}m`, | ||
`\x1b[${close}m`, | ||
new RegExp(`\\x1b\\[${close}m`, "g"), | ||
`\x1b[${open}m` | ||
) | ||
} | ||
const colors = { | ||
// modifiers | ||
reset: init(0, 0), | ||
bold: init(1, 22), | ||
dim: init(2, 22), | ||
italic: init(3, 23), | ||
underline: init(4, 24), | ||
|
||
// colors | ||
black: init(30, 39), | ||
red: init(31, 39), | ||
green: init(32, 39), | ||
yellow: init(33, 39), | ||
blue: init(34, 39), | ||
magenta: init(35, 39), | ||
cyan: init(36, 39), | ||
white: init(37, 39), | ||
gray: init(90, 39), | ||
grey: init(90, 39), | ||
|
||
// setting options | ||
options: options | ||
} | ||
|
||
module.exports=colors |
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,104 @@ | ||
const { magenta, blue, green, yellow, red, options } = require('../helpers/colors'); | ||
const trm = console; | ||
|
||
const LEVEL_VERBOSE = 2; | ||
const LEVEL_TRACE = 3; | ||
const LEVEL_DEBUG = 4; | ||
const LEVEL_INFO = 5; | ||
const LEVEL_WARN = 6; | ||
const LEVEL_ERROR = 7; | ||
const LEVEL_SILENT = 8; | ||
|
||
/** | ||
* returns log level value | ||
* @param {string} level - log level | ||
*/ | ||
function getLevelValue(level) { | ||
const logLevel = level.toUpperCase(); | ||
switch (logLevel) { | ||
case 'TRACE': | ||
return LEVEL_TRACE; | ||
case 'DEBUG': | ||
return LEVEL_DEBUG; | ||
case 'INFO': | ||
return LEVEL_INFO; | ||
case 'WARN': | ||
return LEVEL_WARN; | ||
case 'ERROR': | ||
return LEVEL_ERROR; | ||
case 'SILENT': | ||
return LEVEL_SILENT; | ||
case 'VERBOSE': | ||
return LEVEL_VERBOSE; | ||
default: | ||
return LEVEL_INFO; | ||
} | ||
} | ||
|
||
class Logger { | ||
|
||
constructor() { | ||
this.level = process.env.TESTBEATS_LOG_LEVEL || 'INFO'; | ||
this.levelValue = getLevelValue(this.level); | ||
if (process.env.TESTBEATS_DISABLE_LOG_COLORS === 'true') { | ||
options.disableColors = true; | ||
} | ||
} | ||
|
||
/** | ||
* sets log level | ||
* @param {('TRACE'|'DEBUG'|'INFO'|'WARN'|'ERROR')} level - log level | ||
*/ | ||
setLevel(level) { | ||
this.level = level; | ||
this.levelValue = getLevelValue(this.level); | ||
} | ||
|
||
trace(...msg) { | ||
if (this.levelValue <= LEVEL_TRACE) { | ||
process.stdout.write(`[${magenta('T')}] `); | ||
msg.forEach(m => trm.debug(m)); | ||
} | ||
} | ||
|
||
debug(...msg) { | ||
if (this.levelValue <= LEVEL_DEBUG) { | ||
process.stdout.write(`[${blue('D')}] `); | ||
msg.forEach(m => trm.debug(m)); | ||
} | ||
} | ||
|
||
info(...msg) { | ||
if (this.levelValue <= LEVEL_INFO) { | ||
process.stdout.write(`[${green('I')}] `); | ||
msg.forEach(m => trm.info(m)); | ||
} | ||
} | ||
|
||
warn(...msg) { | ||
if (this.levelValue <= LEVEL_WARN) { | ||
process.stdout.write(`[${yellow('W')}] `); | ||
msg.forEach(m => trm.warn(getMessage(m))); | ||
} | ||
} | ||
|
||
error(...msg) { | ||
if (this.levelValue <= LEVEL_ERROR) { | ||
process.stdout.write(`[${red('E')}] `); | ||
msg.forEach(m => trm.error(getMessage(m))); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
function getMessage(msg) { | ||
try { | ||
return typeof msg === 'object' ? JSON.stringify(msg, null, 2) : msg; | ||
} catch (_) { | ||
return msg; | ||
} | ||
} | ||
|
||
|
||
module.exports = new Logger(); |