1.1.1 • Published 4 years ago

@vandie/customconsole v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

CustomConsole

CustomConsole replicates the functionality of console.log's stringification (and then some) for a custom output method. Useful if you're wanting to output the console to a ui element for say, a game engine. It supports both JavaScript and TypeScript.

Installation

From your command line call npm install @vandie/customconsole or yarn install @vandie/customconsole

Usage

For basic usage it's as simple as creating a new logger and using it as you would console.log.

Creating a logger

To create a logger, call the customLog function with your callback function passed.

import { customLog } from 'customconsole';

// Our callback should take a single string value
const someCallback = (text: string) => { /** output string **/ }
const logger = customLog(someCallback);

Using your logger.

Use your logger exactly as you would console.log. It takes an unlimited number of arguments with any types you want. eg.

logger("Pass any data types you want", { vip: true }, 3);

Advanced Usage

customLog does allow a second argument of type LogOptions. LogOptions is entirely optional, and all fields within it are such as well. LogOptions supports the following attributes:

LogOptions

AttributetypeEffect
includeTimestampBooleanStarts each log with a js ms datestamp. eg. "1434319925275: Example Log"
stringifyObjectsBooleanIf true, objects and arrays will be displayed using JSON.stringify rather than as [object Object]
separatorStringAs with console.log each argument passed to your logger will be split with a new line character (\n). This argument allows you to replace that character with anything you see fit.
customParsersA key value store, where the keys are variable types (as returned by typeof) and the values is a parser function that you wish to call for this typeAllows you to define custom parsers for the logger to use
customTypeChecker(variable: any) => stringSets the function to use, to get the string type of a variable eg. "string" or "number". If not set, will internally use typeof. This allows you to add support for custom typescript types.

console.log replacement

Generally speaking you don't want to replace console.log as modifying prototypes is frowned upon. However, if you are attempting to have console.log calls output to a ui element from code you otherwise cannot modify (eg. a module from a third party), you can call

const inbuiltLogger = console.log;
console.log = customLog(someCallback);
1.1.1

4 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago