-
Notifications
You must be signed in to change notification settings - Fork 0
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
81bb0dd
commit 25a12a3
Showing
8 changed files
with
166 additions
and
8 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,85 @@ | ||
const formatCodes = Object.freeze({ | ||
color: { | ||
RED: '\x1b[31m', | ||
YELLOW: '\x1b[33m', | ||
BLUE: '\x1b[34m', | ||
}, | ||
style: { | ||
UNDERLINE: '\x1b[4m', | ||
}, | ||
RESET: '\x1b[0m', | ||
}); | ||
|
||
const logType = Object.freeze({ | ||
INFO: `${formatCodes.color.BLUE}[INFO]${formatCodes.RESET}`, | ||
WARN: `${formatCodes.color.YELLOW}[WARNING]${formatCodes.RESET}`, | ||
ERROR: `${formatCodes.color.RED}[ERROR]${formatCodes.RESET}`, | ||
}); | ||
|
||
/** | ||
* A utility class for logging messages to the console. | ||
*/ | ||
export default class Logger { | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* Logs an informational message to the console. | ||
* @param {string} message - The message to log. | ||
* @param {string|object} [extra=''] - Additional information to log. | ||
*/ | ||
static info(message, extra = '') { | ||
this.#printMessage(logType.INFO, message, extra); | ||
} | ||
|
||
/** | ||
* Logs a warning message to the console. | ||
* @param {string} message - The message to log. | ||
* @param {string|object} [extra=''] - Additional information to log. | ||
*/ | ||
static warn(message, extra = '') { | ||
this.#printMessage(logType.WARN, message, extra); | ||
} | ||
|
||
/** | ||
* Logs an error message to the console. | ||
* @param {string} message - The message to log. | ||
* @param {string|object} [extra=''] - Additional information to log. | ||
*/ | ||
static error(message, extra = '') { | ||
this.#printMessage(logType.ERROR, message, extra); | ||
} | ||
|
||
/** | ||
* Logs an empty line to the console. | ||
*/ | ||
static emptyLine() { | ||
console.log(); | ||
} | ||
|
||
/** | ||
* Private method for printing a message to the console. | ||
* @param {string} type - The type of message to log. | ||
* @param {string} message - The message to log. | ||
* @param {string|object} extra - Additional information to log. | ||
* @private | ||
*/ | ||
static #printMessage(type, message, extra) { | ||
console.log(`${type} ${message} ${this.#getExtraFormatted(extra)}`); | ||
if (typeof extra === 'object') { | ||
console.log(extra); | ||
} | ||
} | ||
|
||
/** | ||
* Private method for formatting the extra parameter. | ||
* @param {string|object} extraParameter - The extra parameter to format. | ||
* @returns {string} The formatted extra parameter. | ||
* @private | ||
*/ | ||
static #getExtraFormatted(extraParameter) { | ||
if (!extraParameter || typeof extraParameter === 'object') { | ||
return ''; | ||
} | ||
return `${formatCodes.style.UNDERLINE}${extraParameter}${formatCodes.RESET}`; | ||
} | ||
} |
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,8 @@ | ||
export const messages = Object.freeze({ | ||
CONFIG_FILE_NOT_FOUND: 'Could not find the configuration file', | ||
CURRENT_CONFIG: 'Current configuration:', | ||
DIRECTORY_NOT_FOUND: 'Could not find the directory', | ||
SAVED_CONFIG: 'Saved configuration:', | ||
SOME_SCHEMA_DOES_NOT_EXIST: 'Some schema does not exist', | ||
USING_PROVIDED_CONFIG: 'Using provided configuration:', | ||
}); |
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,55 @@ | ||
import { expect, use } from 'chai'; | ||
import { stub } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import Logger from '../../../src/utils/logger.js'; | ||
|
||
use(sinonChai); | ||
|
||
describe('Logger', () => { | ||
let consoleSpy; | ||
|
||
beforeEach(() => { | ||
consoleSpy = stub(console, 'log'); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleSpy.restore(); | ||
}); | ||
|
||
it('should log an informational message to the console with no extra', () => { | ||
const message = 'test message'; | ||
Logger.info(message); | ||
expect(consoleSpy).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should log an informational message to the console with string text as extra', () => { | ||
const message = 'test message'; | ||
const extra = 'extra text'; | ||
Logger.info(message, extra); | ||
expect(consoleSpy).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should log an informational message to the console with an object as extra', () => { | ||
const message = 'test message'; | ||
const extra = { test: 'extra object' }; | ||
Logger.info(message, extra); | ||
expect(consoleSpy).to.have.been.calledTwice; | ||
}); | ||
|
||
it('should log a warning message to the console with no extra', () => { | ||
const message = 'test message'; | ||
Logger.warn(message); | ||
expect(consoleSpy).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should log an error message to the console with no extra', () => { | ||
const message = 'test message'; | ||
Logger.error(message); | ||
expect(consoleSpy).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should log an empty line to the console', () => { | ||
Logger.emptyLine(); | ||
expect(consoleSpy).to.have.been.calledOnce; | ||
}); | ||
}); |