@content-dynamics/logger v1.0.6
Simple logging for Node.Js with lots of config options.
Contents
Getting Started
To install easy-logger simply run npm install @content-dynamics/logger
in your project.
To get started simply require and initalize the logger:
Logger = require('@content-dynamics/logger')()
The base logger has 4 base types:
Usage:
Logger.Log(message, { options })
Configuration
Options | Description | Default |
---|---|---|
level | Integer, controls the messages debug level | 1 |
debug | Boolean or null, will override the level and add debug values to the log message | null |
important | Boolean, will override the logging level and print to console | false |
flags | Object, Any additional properties you want to add to the debug message | {} |
Examples:
Logger.Log("By default this won't be logged to console or to the logging file");
Logger.Log("This will ensure that the info is logged to console and to the logging file",{ important: true });
Logger.Log("This will add a debug stack to the logging message and write it to the logging file and console",{ important:true, debug: true });
Usage:
Logger.Info(message, { options })
Configuration
Options | Description | Default |
---|---|---|
level | Integer, controls the messages debug level | 5 |
debug | Boolean or null, will override the level and add debug values to the log message | null |
important | Boolean, will override the logging level and print to console | false |
flags | Object, Any additional properties you want to add to the debug message | {} |
Examples:
Logger.Info("This will simply show as info in the log!");
Logger.Info("This will ensure that the info is logged to console",{ important: true });
Logger.Info("Thiswill add a debug stack to the warn message",{ debug: true });
Usage:
Logger.Warn(message, { options })
Configuration
Options | Description | Default |
---|---|---|
level | Integer, controls the messages debug level | 10 |
debug | Boolean or null, will override the level and add debug values to the log message | null |
important | Boolean, will override the logging level and print to console | false |
flags | Object, Any additional properties you want to add to the debug message | {} |
Examples:
Logger.Warn("This will simply show as a warning in the log!");
Logger.Warn("This will ensure that the warning is logged to console",{ important: true });
Logger.Warn("This won't add a stack to the warn message",{ debug: false });
Usage:
Logger.Error(message, { options })
Configuration
Options | Description | Default |
---|---|---|
level | Integer, controls the messages debug level | 20 |
debug | Boolean or null, will override the level and add debug values to the log message | true |
important | Boolean, will override the logging level and print to console | true |
flags | Object, Any additional properties you want to add to the debug message | {} |
Examples:
Logger.Error("This will simply show as an error in the log!");
Logger.Error("This will set the debug message level to 50",{ level: 50 });
Logger.Error("This won't add a stack to the logged message",{ debug: false });
Advanced Configuration
The Logger instance can be defined with other options to match your requirements.
Example
const Logger = require('easylogger')({
logFile:'./logs/main.log',
logLevel:0,
cmode:'json'})
The constructor takes several options passed through an object as defined below:
Option | Description | Default Value |
---|---|---|
mode | String, Logging Mode. Controls how data is logged to the logging file | 'json' |
cmode | String, Console Logging Mode. Controls the logging of data to console | 'string' |
logFile | String, Path to the log file you want to use | './.log' |
logLevel | The lowest level of logging message you would like to log to the logging file | 5 |
debugLevel | The lowest level you would like to automatically add debug info to | 10 |
consoleLevel | The lowest level you would like to automatically log to console | 15 |
preserve | Boolean, If true preserve any previously saved logs in the log file | false |
streamFlags | Object, Any other fs.createWriteStream options you would like to use | {} |
dateFormat | Callback, A callback used to format timestamps... Find out more... | Function |
logFormat | Callback, A callback used to format the logging string... Find out more... | Function Or util.inspect(message) |
consoleFormat | Callback, A Callback used to format the console string... Find out more... | Function Or util.inspect(message) |
Configuring dateFormat
The dateFormat option accepts a callback that takes the default "date-object" created by the helper function:
getDate()
The object returned from getDate()
has the following properties:
Property Name | Type | Description |
---|---|---|
DAY | Object | Returns an object containing 3 props: NUM, SHORT, LONG... Find out more... |
DATE | Int | The numeric date |
MONTH | Int | The numeric month |
YEAR | Int | The numeric year |
HOUR | Int | The numeric hour (24hr) of the day |
MINUTE | Int | The numeric minute of the hour |
SECOND | Int | The numeric second of the minute |
TIMEZONE | Object | Returns an object containing 3 props: HOURS, MINUTES, FULL... Find out more... |
Example callback
dateFormat = ( { DAY, DATE, MONTH, YEAR, HOUR, MINUTE, SECOND } )=>{
return `[ ${DAY.SHORT} | ${DATE}:${MONTH}:${YEAR} | ${HOUR}:${MINUTE}:${SECOND} ]`;
},
Configuring logFormat
Name | Type | Description | |
---|---|---|---|
prefix | String | String prefix for the message | |
timestamp | String | String representaion of timestamp | |
level | Int | The level of the message | |
info | String | The message text | |
debugMsg | Object Or null | The debug object or null, by default debugMsg will contain a prop: stack readmore |
Example callback
logFormat = ( { prefix, timestamp, level, info, debugMsg } )=>{
return `\n${prefix} - ${timestamp} - [LEVEL: ${level}]\nInfo:\n${info}${ debugMsg ? `\nDebug:${ inspect(debugMsg)}\n`:`\n`}`
}
Configuring consoleFormat
Name | Type | Description |
---|---|---|
prefix | String | String prefix for the message |
timestamp | String | String representaion of timestamp |
level | Int | The level of the message |
info | String | The message text |
debugMsg | Object Or null | The debug object or null, by default debugMsg will contain a prop: stack readmore |
Example callback
consoleFormat = ( { prefix, timestamp, level, info, debugMsg } )=>{
const formatMap = (num)=>{
if (num >= 15) return {
'prefix':chalk.bold.red,
'timestamp':chalk.bold.red,
'level':chalk.bold.red,
'info':chalk.bold.yellow,
'debug':chalk.yellow,
}
if (num >= 10) return {
'prefix':chalk.bold.yellow,
'timestamp':chalk.bold.yellow,
'level':chalk.bold.yellow,
'info':chalk.bold.white,
'debug':chalk.yellow,
}
if (num >= 5) return {
'prefix':chalk.bold.green,
'timestamp':chalk.bold.green,
'level':chalk.bold.green,
'info':chalk.white,
'debug':chalk.yellow,
}
return {
'prefix':chalk.bold.green,
'timestamp':chalk.green,
'level':chalk.green,
'info':chalk.white,
'debug':chalk.white,
}
}
const formatFromLevel = ( num, type, message)=>{
return formatMap(num)[type](message);
}
const prefixFormatted = `${formatFromLevel(level, 'prefix', prefix)}`
const timestampFormatted = `${formatFromLevel(level, 'timestamp', timestamp)}`;
const levelFormatted = `${formatFromLevel(level, 'level', `[Level: ${level}]`)}`;
const messageFormatted = `${formatFromLevel(level, 'info', `\nInfo:\n${info}\n`)}`;
const debugFormatted = `${formatFromLevel(level, 'debug', `${debugMsg ? `\nDebug: ${ inspect(debugMsg)}\n`:''}`)}`;
return `${prefixFormatted} - ${timestampFormatted} ${levelFormatted}\n${messageFormatted}${debugFormatted}`;
}
Further Reading
Stack Trace
The stack tracing function used by the logger returns an object with the following properties
Name | Type | Description |
---|---|---|
stack | Array | An array containing all the files within the stack trace. |
callee | String | The path to the calling function that initiated the logging |
The Day object
The day object properties returned by getDate().DAY
Property | Description | |
---|---|---|
NUM | The number of day in the week (0-6 | 0 is Sunday) |
LONG | A long string representation of the day | |
SHORT | A short string representation of the day |
The Timezone object
The timezone object properties returned by getDate().TIMEZONE
Property | Description |
---|---|
HOURS | The HOURLY GMT timezone difference |
MINUTES | The MINUTE DIFFERENCE of HOURLY GMT timezone difference |
FULL | The GMT timezone difference as string |
Dependencies
Used to make console logging fun!
Used to test that the logger is working correctly during development
Contributing
Contributions to the project are more than welcome! Please visit the contributions guide for more information. Feel free to check the issues page if you want to contribute to solving issues.
If you would like to request a new feature please follow the request guide: Request a new feature
Author
:man_technologist: Elliot Gibson
- GitHub: @elliotgibson
- Twitter: @egibson_glass
- Instagram: @egibson_glass
License
Copyright 2021 Elliot Gibson. This project is licensed with the MIT license.