-
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.
Added Logger class, fixed lint and typecheck errors, release v1.2.0
- Loading branch information
Showing
18 changed files
with
213 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@vpmedia/simplify", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "@vpmedia/simplify", | ||
"author": "Andras Csizmadia <[email protected]> (www.vpmedia.hu)", | ||
"license": "MIT", | ||
|
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,113 @@ | ||
import { getURLParam } from '../util/getURLParam.js'; | ||
|
||
export const LEVEL_DEBUG = 4; | ||
export const LEVEL_INFO = 3; | ||
export const LEVEL_WARN = 2; | ||
export const LEVEL_ERROR = 1; | ||
export const LEVEL_SILENT = 0; | ||
|
||
export class Logger { | ||
/** | ||
* @type {(error) => {}} | ||
*/ | ||
static exceptionHandler = null; | ||
|
||
/** | ||
* Creates a new Logger instance. | ||
* @param {string} name - The logger name. | ||
*/ | ||
constructor(name) { | ||
this.name = name; | ||
const appEnvironment = process.env.APP_ENVIRONMENT || 'local'; | ||
const isProduction = appEnvironment === 'production' || appEnvironment === 'release'; | ||
const defaultLevel = isProduction ? LEVEL_SILENT : LEVEL_DEBUG; | ||
const parameterName = `log_${this.name.toLowerCase()}`; | ||
this.level = Number.parseInt(getURLParam(parameterName, defaultLevel), 10); | ||
const allLevel = Number.parseInt(getURLParam('log_all', LEVEL_SILENT), 10); | ||
this.level = allLevel || this.level; | ||
} | ||
|
||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
debug(message, extraData = null) { | ||
if (this.level < LEVEL_DEBUG) { | ||
return; | ||
} | ||
const logMessage = `${Date.now()} [${this.name}] ${message}`; | ||
if (extraData !== null) { | ||
console.debug(logMessage, extraData); | ||
return; | ||
} | ||
console.debug(logMessage); | ||
} | ||
|
||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
info(message, extraData = null) { | ||
if (this.level < LEVEL_INFO) { | ||
return; | ||
} | ||
const logMessage = `${Date.now()} [${this.name}] ${message}`; | ||
if (extraData !== null) { | ||
console.info(logMessage, extraData); | ||
return; | ||
} | ||
console.info(logMessage); | ||
} | ||
|
||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
warn(message, extraData = null) { | ||
if (this.level < LEVEL_WARN) { | ||
return; | ||
} | ||
const logMessage = `${Date.now()} [${this.name}] ${message}`; | ||
if (extraData !== null) { | ||
console.warn(logMessage, extraData); | ||
return; | ||
} | ||
console.warn(logMessage); | ||
} | ||
|
||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
error(message, extraData = null) { | ||
if (this.level < LEVEL_ERROR) { | ||
return; | ||
} | ||
const logMessage = `${Date.now()} [${this.name}] ${message}`; | ||
if (extraData !== null) { | ||
console.error(logMessage, extraData); | ||
return; | ||
} | ||
console.error(logMessage); | ||
} | ||
|
||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {Error} exception - TBD. | ||
*/ | ||
exception(message, exception) { | ||
if (Logger.exceptionHandler) { | ||
Logger.exceptionHandler(exception); | ||
} | ||
if (this.level < LEVEL_ERROR) { | ||
return; | ||
} | ||
const logMessage = `${Date.now()} [${this.name}] ${message}`; | ||
console.error(logMessage, exception); | ||
} | ||
} |
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,14 @@ | ||
import { LEVEL_DEBUG, Logger } from './Logger.js'; | ||
|
||
test('Tests Logger', () => { | ||
const logger = new Logger('test'); | ||
expect(Logger.exceptionHandler).toBe(null); | ||
expect(logger.level).toBe(LEVEL_DEBUG); | ||
let handledError = null; | ||
const exceptionHandler = (error) => { | ||
handledError = error; | ||
}; | ||
Logger.exceptionHandler = exceptionHandler; | ||
logger.exception('test', 'error'); | ||
expect(handledError).toBe('error'); | ||
}); |
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { capitalize } from './util/capitalize.js'; | ||
import { getObjValueByPath } from './util/getObjValueByPath.js'; | ||
import { getRandomInt } from './util/getRandomInt.js'; | ||
import { getURLParam } from './util/getURLParam.js'; | ||
import { sanitizeURLParam } from './util/sanitizeURLParam.js'; | ||
import { setObjValueByPath } from './util/setObjValueByPath.js'; | ||
import { underscoreToCamelCase } from './util/underscoreToCamelCase.js'; | ||
export { capitalize, getObjValueByPath, getRandomInt, getURLParam, sanitizeURLParam, setObjValueByPath, underscoreToCamelCase }; | ||
export { Logger } from "./logging/Logger.js"; | ||
export { addLeadingZero } from "./util/addLeadingZero.js"; | ||
export { capitalize } from "./util/capitalize.js"; | ||
export { getObjValueByPath } from "./util/getObjValueByPath.js"; | ||
export { getRandomInt } from "./util/getRandomInt.js"; | ||
export { getURLParam } from "./util/getURLParam.js"; | ||
export { sanitizeURLParam } from "./util/sanitizeURLParam.js"; | ||
export { setObjValueByPath } from "./util/setObjValueByPath.js"; | ||
export { underscoreToCamelCase } from "./util/underscoreToCamelCase.js"; | ||
//# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,49 @@ | ||
export const LEVEL_DEBUG: 4; | ||
export const LEVEL_INFO: 3; | ||
export const LEVEL_WARN: 2; | ||
export const LEVEL_ERROR: 1; | ||
export const LEVEL_SILENT: 0; | ||
export class Logger { | ||
/** | ||
* @type {(error) => {}} | ||
*/ | ||
static exceptionHandler: (error) => {}; | ||
/** | ||
* Creates a new Logger instance. | ||
* @param {string} name - The logger name. | ||
*/ | ||
constructor(name: string); | ||
name: string; | ||
level: number; | ||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
debug(message: string, extraData?: object): void; | ||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
info(message: string, extraData?: object): void; | ||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
warn(message: string, extraData?: object): void; | ||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {object} extraData - TBD. | ||
*/ | ||
error(message: string, extraData?: object): void; | ||
/** | ||
* TBD. | ||
* @param {string} message - TBD. | ||
* @param {Error} exception - TBD. | ||
*/ | ||
exception(message: string, exception: Error): void; | ||
} | ||
//# sourceMappingURL=Logger.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
/** | ||
* TBD. | ||
* @param {number|string} value - TBD. | ||
* @param {number} size - TBD. | ||
* @returns {string} TBD. | ||
*/ | ||
export function addLeadingZero(value: number | string, size?: number): string; | ||
//# sourceMappingURL=addLeadingZero.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.