Log Helper
This highly configurable package offers some functions which help you to understand your console logs by colorizing the output and providing the current date.
Installation
npm i log-helper --save
Usage
There are 6 functions available:
success(message, config)
info(message, config)
status(message, config)
warning(message, config)
error(message, config)
setConfig(config)
Each logged message looks like the following:
currentDate + prependString + message;
All of this output is colored via the chalk package, and the color of the output is the main difference of all this functions.
Example
const logHelper =
Configuration
Every function has the following configuration options:
chalk
: This is a string which defines the color of the message output.dateChalk
: This is also a string, which defines the color of the current date.prependString
: You can provide a string which automatically gets inserted between the current date and the message.dateFormat
: This string defines in which format the date of the output gets logged.
NOTE: You do not have to configurate any of this options yourself, only if you don't like the default configuration.
To configurate the chalk
or the dateChalk
option, you do not have to directly use the chalk package. Instead, provide a string where each chalk option is separated with a dot. For example, if you want your message in a green, bold font, use the following as a config object:
chalk: 'bold.green'
To what style options are available, have a look at the chalk package.
The date formatting work is done by the dateformat package. Have a look at the documentation to see the available format options.
There are two ways to configurate this package. You can either provide a config object as a second parameter in each if the five logging functions, or use the setConfig
method. But there is a difference between these two ways: The first way will only configurate the message provided as the first parameter, while the second way will overwrite the default configuration.
const logHelper = ; // will be logged in the default green fontlogHelper; // will be logged in blue fontlogHelper; // will be logged in the default green fontlogHelper; // now the default chalk setting for the success function will be overwrittenlogHelper; // will be logged in a white font on black backgroundlogHelper; // it is also possible to configurate multiple output functions at one timelogHelper
If you use the setConfig
method, the configuration will be used in your whole project:
// firstFile.jsconst logHelper = ; logHelper; // secondFile.jsconst logHelper = ; // will be logged in a yellow fontlogHelper;
If you want to disable logging, use the setConfig
method:
const logHelper = ; logHelper; // will not be loggedlogHelper;
Default configuration
This is the default configuration of all five logging functions.
disableLogging: false success: chalk: 'green' dateChalk: 'white' prependString: null dateFormat: "dd.mm.yyyy HH:MM:ss l'ms'" info: chalk: 'cyan' dateChalk: 'white' prependString: null dateFormat: "dd.mm.yyyy HH:MM:ss l'ms'" status: chalk: 'gray' dateChalk: 'white' prependString: null dateFormat: "dd.mm.yyyy HH:MM:ss l'ms'" warning: chalk: 'bold.yellow' dateChalk: 'white' prependString: null dateFormat: "dd.mm.yyyy HH:MM:ss l'ms'" error: chalk: 'green' dateChalk: 'bold.bgWhite.red' prependString: 'An error occured: ' dateFormat: "dd.mm.yyyy HH:MM:ss l'ms'"