consoleback
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

consoleback

Improved logging for Javascript applications

 

  • Uses console.log, console.warn, console.error, console.info, and console.debug
  • Adds timestamp and log type to message (if desired)
    • Time formatted to a local string with date-fns
  • Allows callback that gives the application access to the log.

 

Usage

// ES6
import consoleback from "consoleback";

// CommonJS
const consoleback = require("consoleback");


// Usage
consoleback()

// All logs are now captured by consoleback

consoleback: (opts?: {
    callback?: (type: string, message: any, ...optionalParams: any[]) => void;
    showMsgType?: boolean;
    showTime?: boolean;
}) => terminate

terminate: () => void

All options are optional.

callback?: (type: string, message: any, ...optionalParams: any[]) => void;

The callback that will provide the logged data.

  • type: The type of log (log, warn, error)
  • message/optionalParams: The logged information

showMsgType?: boolean;

Whether to show a message type or not (defaults to true)

When true, [ LOG ], [ WRN ], or [ ERR ] will appear appended to the start of each log.

showTime?: boolean;

Whether to show time information (defaults to false)

terminate: () => void

Restores normal logging.

 

Example

const terminateLog = consoleback({
  callback: (type, message, optionalParams) => {
    // Perform actions on logged messages
  },
  showMsgType: true,
  showTime: true
})

console.log("The time is...");

// [ 2019-10-01T20:38:04.586Z ] [ LOG ] The time is...

terminateLog();
console.log("Goodbye!");

// Goodbye!

 

License

This project is MIT Licensed.

Readme

Keywords

Package Sidebar

Install

npm i consoleback

Weekly Downloads

2

Version

0.3.1

License

MIT

Unpacked Size

315 kB

Total Files

10

Last publish

Collaborators

  • lindsaykwardell