Logbench

A tiny tool for warning developers.

Installation

npm install logbench

Documentation

When developing a library it can be useful to warn consumers of issues with their usage of your library. This can be done with console.warn but it can be difficult to see these warnings in the console when there are many other logs.

Logbench provides a way to log warnings to the console in a way that stands out and disable these warnings in production.

Example

Setup

import { Logbench } from 'logbench'
 
 
export const logger = new Logbench({
  warnFn: (message) => console.log(
    %c`${message}`,
    `
      font-size: 18px;
      background: black;
      color: white; 
    `
  )
})

Usage

import { logger } from '@/lib/logger'
 
// will only log in development
// and styled in a way that stands out
logger.warn('This is a warning')

API

When constructing a new instance of Logbench you can pass in an object with the following properties:

Options

NameTypeDescriptionDefault
logFn(message: KSerialized) => voidA function that will be called when a log is logged.--
warnFn(message: KSerialized) => voidA function that will be called when a warning is logged.--
errorFn(message: KSerialized) => voidA function that will be called when an error is logged.--
isProductionbooleanWhether or not warnings should be logged.process.env.NODE_ENV === 'production'
stringifybooleanWhether or not to stringify the log message.true
serializeFn(log: TPayload) => KSerializedA function that will be called to serialize the log message.JSON.stringify