1.1.1 • Published 12 months ago

@carlosmta_/slim-logger v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Slim Logger

Tests Version

A simple logger library which wraps console.log API adding handy functionalities. It can be used both in the browser and server-side.

Logs are colored thanks to the use of ANSI codes so they should be colored both in terminal and in the browser console.

Installation

This is a re-upload of a library that is outdated in another organization from an old account (@carlosmta).

Please use the new organization name to receive updates (@carlosmta_).

npm

npm i @carlosmta_/slim-logger

yarn

yarn add @carlosmta_/slim-logger

pnpm

pnpm install @carlosmta_/slim-logger

Importing/Requiring

The library output is CommonJS for maximum compatibility.

const SlimLogger = require('@carlosmta_/slim-logger').default; // CommonJS
import SlimLogger from '@carlosmta_/slim-logger'; // ES

Usage

const logger = new SlimLogger('tagName');
logger.debug('Hello world!');

// [2023-02-17T16:05:26.217Z] - DEBUG - #tagName: Hello world!

API

Global properties

There are some properties that can be set for all class instances statically.

  • Colors
// Replaces the debug log color
SlimLogger.Globals.logColors = {
  debug: ESlimLoggerColors.BrightCyan,
};

All the possible options are inside this enum, which is exported for convenience:

export enum ESlimLoggerColors {
  Black = '\x1b[30m',
  Red = '\x1b[31m',
  Green = '\x1b[32m',
  Yellow = '\x1b[33m',
  Blue = '\x1b[34m',
  Magenta = '\x1b[35m',
  Cyan = '\x1b[36m',
  White = '\x1b[37m',
  BrightBlack = '\x1b[90m',
  BrightRed = '\x1b[91m',
  BrightGreen = '\x1b[92m',
  BrightYellow = '\x1b[93m',
  BrightBlue = '\x1b[94m',
  BrightMagenta = '\x1b[95m',
  BrightCyan = '\x1b[96m',
  BrightWhite = '\x1b[97m',
  Gray = '\x1b[90m',
  Grey = '\x1b[90m',
  BrightGray = '\x1b[37;1m',
  BrightGrey = '\x1b[37;1m',
}

By default, log colors will be the following:

const defaultSlimLoggerLogColors: SlimLoggerLogColors = {
  verbose: ESlimLoggerColors.Gray,
  info: ESlimLoggerColors.BrightBlue,
  debug: ESlimLoggerColors.BrightMagenta,
  warn: ESlimLoggerColors.BrightYellow,
  success: ESlimLoggerColors.BrightGreen,
  error: ESlimLoggerColors.BrightRed,
};
  • Log level

ESlimLoggerLogLevels.verbose by default.

// Only error logs will be shown
SlimLogger.Globals.logLevel = ESlimLoggerLogLevels.error;
  • App name
SlimLogger.Globals.appName = 'My App';
logger.debug('Hello world!');

// [2023-02-17T16:05:26.217Z] - DEBUG - MY APP - #tagName: Hello world!
  • Dispatcher

A callback function that receives the variables used to create the log message. This function can be useful to send logs to a third party service such as Google or Azure in production.

SlimLogger.Globals.dispatcher = (output: SlimLoggerLogOutput) => {
  if (process.env.NODE_ENV === 'production') {
    myThirdPartyLogsService.log({
      message: output.message,
      tag: output.tag,
    });
  }
};

A 'fire and forget' approach is recommended, i.e. if the function that sends the logs to the third-party service returns a promise, do not wait for its resolution.

Check out the SlimLoggerLogOutput type:

export type SlimLoggerLogOutput = {
  appName?: string;
  color: string;
  message: string;
  callee: string;
  tag: string;
};
  • JSON Pretty print
SlimLogger.Globals.jsonPrettyPrint = true; // default

Parameters

I decided to follow a similar approach to the popular Java logging library: log4j, the second argument that the methods accept to log messages is an array of parameters that will be substituted according to the order in which they are added.

logger.verbose('My name is {1} and I am {2} years old right now', 'Carlos', 28);

// [2023-02-17T16:05:26.225Z] - VERBOSE - #tagName: My name is Carlos and I am 28 years old right now
1.1.1

12 months ago

1.1.0

12 months ago

1.0.5

1 year ago