Console helpers
console.log
helpers
Basically...
This lib exports some helpers you can either import and use directly or add to console
(the global guy)
- if adding to
global.console
, calladdToGlobalConsole
in a file close to your entry point (likeindex.js
) - uses util.inspect. Highly recommended to read the docs to understand the options
- lib also exports a few functions to set options for logging
Usage
logs: (...args: any[]) => void
log string
- nested object that you want to log
- want to make console.log easier to read
logs
// console log output console.logs { foo: { bar: { baz: 22 } } }
uses util.inspect
logk: (...args: any[]) => void
log keys
- big object that you want to understand what's inside
- complex props with several hocs
- props with data from server, but the internals of the data are not so important
logk
// console log output console.logk [ 'foo', 'bar', 'baz' ]
addToGlobalConsole: (isProd?: boolean | undefined) => void
- easy access to library functions with no import needed
// index.js or App.js // console.logs is now defined// console.logk is now definedaddToGlobalConsole
// if dev it adds logs and logk to consoleaddToGlobalConsole/*isDev*/ false // if prod it adds () => {} insteadaddToGlobalConsole/*isProd*/ true
types
declare addToGlobalConsole // types are okconsole.logs// types are okconsole.logk
Customizing the output
logOptionsDefault: () => InspectOptions
- you want to see what the lib thinks are good default options it passes to util.inspect
// defaults are: { showHidden: false, depth: null, colors: true, compact: false }console.loglogOptionsDefault
types
// @types/node/globals.d.ts
logOptions: () => InspectOptions;
- returns log options currently set
// trueconsole.loglogOptions.toString === logOptionsDefault.toString
setLogOptions: (options: InspectOptions) => InspectOptions;
- set log options to your provided object
// logOptions now are: { showHidden: false, colors: false, compact: true, breakLength: 120 }console.logsetLogOptions
addLogOptions: (options: InspectOptions) => InspectOptions;
- combine defaults with your object, giving your object priority
// logOptions now are defaults + { colors: false, breakLength: 120 }: { showHidden: false, depth: null, colors: false, compact: false, breakLength: 120 }console.logaddLogOptions
resetLogOptions: () => InspectOptions;
- reset log options to lib default
addLogOptions// false because { colors: false, breakLength: 120 } was merged inlogOptions.toString === logOptionsDefault.toString// logOptions now are defaults: { showHidden: false, depth: null, colors: true, compact: false }console.logresetLogOptions// true because resetlogOptions.toString === logOptionsDefault.toString
To do
Better browser support with pretty format