logdash
Simple and customizable log functions that can integrate with lodash
.
Features
- 5 pre-defined log functions:
-
logn
: Prints uncolored[NONE]
tag viaconsole.log
-
logi
: Prints blue[INFO]
tag viaconsole.info
-
logs
: Prints green[SUCCESS]
tag viaconsole.log
-
logw
: Prints orange[WARN]
tag viaconsole.warn
-
loge
: Prints red[ERROR]
tag viaconsole.error
-
- Add and customize logs by color, console, tag, and more
- Use log functions directly or easily integrate for lodash chain logging
- Toggle log functions on/off
- Helper functions for easily mapping over and customizing log type objects
Install
npm install @surajs02/logdash
Basic Usage
All log functions can be used directly (i.e., without lodash
integration)
import { loadLogdash } from '@surajs02/logdash'; // TypeScript
// const { loadLogdash } = require('logdash'); // JavaScript.
const { logn, logi, logs, logw, loge } = loadLogdash().logFuncs;
logn('A standard log');
logi('This might be useful');
logs('That went well');
logw('There may be a problem...');
loge('There was an error');
The above logs would be styled and displayed with a colored tag:
All log functions support multiple arguments:
logn('String1', 1, true, [1], { a: 1 }, null, undefined);
lodash
Integrating with Pass a lodash
instance as a lodashForMixin
option to make log functions available via lodash
:
const _ = require('lodash');
loadLogdash({ lodashForMixin: _, });
_.logn('Logging directly via logdash');
Log functions can be used indirectly in lodash
chains:
const doubled = _.chain([1, 2, 3])
.logi() // Prints [1, 2, 3].
.map(n => n * 2)
.logi() // Prints [2, 4, 8].
.value();
_.logi(doubled.length); // Prints 3.
Customization
All log functions are enabled by default but can be disabled via the disableAllLogs
option:
const { logi } = loadLogdash({ disableAllLogs: true, }).logFuncs;
_.logi("This won't be printed");
Log types are customized via the customizeLogTypes
option, which is a function that
accepts a ILogTypeMap
to return a new ILogTypeMap
:
interface ILogTypeMap {
[key: string]: ILogType;
}
ILogTypeMap
maps log type names to ILogType
values:
interface ILogType {
color?: Function;
consoleType?: Function;
tag?: String;
func?: ILogFunc;
enabled?: boolean;
}
New log types can be added and accessed via log<initial-letter-of-log-type>
:
const { loga } = loadLogdash({
customizeLogTypes: (logTypes: ILogTypeMap) => ({
...logTypes,
added: { color: chalk.cyan }, // `added` will become `loga`.
}),
}).logFuncs;
loga('This will print in cyan!');
Existing log types can be customized easily via the mapObjValues
helper:
import { loadLogdash, mapObjValues, ILogType } from '@surajs02/logdash';
const { logi } = loadLogdash({
customizeLogTypes: (logTypes: ILogTypeMap) => mapObjValues(
logTypes, (t: ILogType) => ({...logTypes, color: chalk.magenta }),
}),
}).logFuncs;
logi('This and other log functions will now print in magenta!');
Testing
Setup
git clone https://github.com/surajs02/logdash.git && cd logdash && npm install
Run all tests
npm run test
Test only log functions
npm run testLogFuncs
Test only logdash
options (e.g., lodash
integration)
npm run testOptions