Skip to content

Commit

Permalink
fix: added basic docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Feb 23, 2024
1 parent a7961de commit 4cade2c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 54 additions & 9 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}
}

0 comments on commit 4cade2c

Please sign in to comment.