diff --git a/README.md b/README.md index 348ad65..3e067b5 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,19 @@ A simple logger for all Rainbow Cafe Apps & Libraries +### Usage + +```tsx +import { Logger, LogLevel } from '@rain-cafe/logger'; + +Logger.setLevel(LogLevel.INFO); // This is the default LogLevel + +Logger.silly('hello', 'world!'); // Outputs nothing since its lower then the configured level! +Logger.info('hello', 'world!'); // '[info]: hello world!' +Logger.warn('hello', 'world!'); // '[warn]: hello world!' +Logger.error('hello', 'world!'); // '[error]: hello world!' +``` + [npm-version-image]: https://img.shields.io/npm/v/@rain-cafe/logger.svg [npm-downloads-image]: https://img.shields.io/npm/dm/@rain-cafe/logger.svg [npm-url]: https://npmjs.org/package/@rain-cafe/logger diff --git a/src/logger.ts b/src/logger.ts index 5c49e0d..6f8eeb8 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -22,22 +22,51 @@ export const LEVEL_CHALK: { [LogLevel.SILLY]: magenta, }; +/** + * A simple wrapper around 'console.log' + */ export class Logger { static #level: LogLevel = LogLevel.INFO; + /** + * Returns whether the logger is configured to allow the given log level to output. + * @param level the level to validate + * @returns true if the given level is equal to or higher priority then the configured level + * + * @example + * Logger.setLevel(LogLevel.INFO); + * // Returns true + * Logger.isLevel(LogLevel.ERROR); + * // Also returns true + * Logger.isLevel(LogLevel.INFO); + */ public static isLevel(level?: LogLevel): boolean { return Logger.#level >= level; } + /** + * Returns whether the logger is configured to deny the given log level to output. + * @param level the level to validate + * @returns true if the given level is lower priority then the configured level + */ public static isNotLevel(level?: LogLevel): boolean { return !Logger.isLevel(level); } + /** + * Sets the level to allow to output + * @param level the level to set + */ public static setLevel(level: LogLevel) { Logger.#level = level; } - public static log(level: LogLevel, ...rawMessages: any[]) { + /** + * A wrapper around console.log with color formatting and filtering by the set LogLevel. + * @param level the LogLevel of this message + * @param rawMessages the messages to send + */ + public static log(level: LogLevel, ...rawMessages: any[]): void { if (Logger.isNotLevel(level)) return; const chalk = LEVEL_CHALK[level]; @@ -49,19 +78,35 @@ export class Logger { console.log(chalk(`[${LogLevel[level].toLowerCase()}]:`).padEnd(MAX_LENGTH, ' '), ...messages); } - public static error(...message: any[]) { - Logger.log(LogLevel.ERROR, ...message); + /** + * A helper that automatically calls {@link Logger.log} with {@link LogLevel.ERROR} + * @param messages the messages to send + */ + public static error(...messages: any[]) { + Logger.log(LogLevel.ERROR, ...messages); } - public static warn(...message: any[]) { - Logger.log(LogLevel.WARN, ...message); + /** + * A helper that automatically calls {@link Logger.log} with {@link LogLevel.WARN} + * @param messages the messages to send + */ + public static warn(...messages: any[]) { + Logger.log(LogLevel.WARN, ...messages); } - public static info(...message: any[]) { - Logger.log(LogLevel.INFO, ...message); + /** + * A helper that automatically calls {@link Logger.log} with {@link LogLevel.INFO} + * @param messages the messages to send + */ + public static info(...messages: any[]) { + Logger.log(LogLevel.INFO, ...messages); } - public static silly(...message: any[]) { - Logger.log(LogLevel.SILLY, ...message); + /** + * A helper that automatically calls {@link Logger.log} with {@link LogLevel.SILLY} + * @param messages the messages to send + */ + public static silly(...messages: any[]) { + Logger.log(LogLevel.SILLY, ...messages); } }