Skip to content

Instantly share code, notes, and snippets.

@Restuta
Created Nov 5, 2019
Embed
What would you like to do?
Logger
const util = require('util');
const R = require('ramda');
const chalk = require('chalk');
const jsondiffpatch = require('jsondiffpatch');
const toJson = require('./to-json');
let prefix;
let coloredOutput = false;
if (
process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'test' ||
process.env.NODE_ENV === 'script' ||
!process.env.NODE_ENV
) {
chalk.enabled = true;
// eslint-disable-next-line global-require
prefix = require('./global-prefix').getProcessWideLogPrefix();
coloredOutput = true;
} else {
chalk.enabled = false;
}
const createCustomChalk = (chalkFunc, ...chalkArgs) => (...args) => {
if (chalkFunc === 'hex') {
return chalk[chalkFunc](R.head(chalkArgs))(...args);
}
return chalk[chalkFunc](...args);
};
const createMsg = (type = '', modulePrefixStr = '') => (chalkFuncName, ...chalkArgs) => {
// for "hex" we expect first argument to be actual hex color
const args = chalkFuncName === 'hex' ? R.tail(chalkArgs) : chalkArgs;
const stringifiedArgs = args.map(arg => {
if (arg instanceof Error) {
const remainingProps = R.omit(['message', 'stack'], arg);
return R.isEmpty(remainingProps)
? `${arg.message}\n${arg.stack}`
: `${arg.message}\n${arg.stack}\nAdditional error props: ${toJson(remainingProps)}`;
}
const logStr = typeof arg === 'object' ? toJson(arg) : arg;
return logStr;
});
const typePrefix = type ? `[${type}]` : '';
const modulePrefix = modulePrefixStr ? `[${modulePrefixStr}]` : '';
const globalPrefix = prefix ? `[${prefix}]` : '';
const customChalk = createCustomChalk(chalkFuncName, ...chalkArgs);
if (modulePrefix) {
return prefix
? customChalk(`${globalPrefix}${typePrefix}${modulePrefix}`, ...stringifiedArgs)
: customChalk(`${typePrefix}${modulePrefix}`, ...stringifiedArgs);
}
if (globalPrefix) {
return customChalk(`${globalPrefix}${typePrefix}`, ...stringifiedArgs);
}
return customChalk(typePrefix, ...stringifiedArgs);
};
let debugAlternateCounter = 0;
/* eslint-disable no-console */
const log = (modulePrefix = '') => ({
error: (...args) => console.error(createMsg('error', modulePrefix)('red', ...args)),
exception: (...args) => console.error(createMsg('error', modulePrefix)('red', ...args)),
trace: (...args) => console.trace(createMsg('trace', modulePrefix)('red', ...args)),
info: (...args) => console.info(createMsg('info', modulePrefix)('gray', ...args)),
success: (...args) => console.log(createMsg('success', modulePrefix)('green', ...args)),
done: (...args) => console.log(createMsg('success', modulePrefix)('green', ...args)),
warn: (...args) => console.warn(createMsg('warn', modulePrefix)('yellow', ...args)),
warnTrace: (...args) => console.trace(createMsg('warn', modulePrefix)('yellow', ...args)),
warning: (...args) => console.warn(createMsg('warn', modulePrefix)('yellow', ...args)),
debug: (...args) => console.log(createMsg('debug', modulePrefix)('blue', ...args)),
debugAlter: (...args) => {
if (debugAlternateCounter % 2 === 0) {
console.log(createMsg('debug', modulePrefix)('cyan', ...args));
} else {
console.log(createMsg('debug', modulePrefix)('magenta', ...args));
}
debugAlternateCounter += 1;
},
time: (...args) => console.time(createMsg('info', modulePrefix)('cyan', ...args)),
timeEnd: (...args) => console.timeEnd(createMsg('info', modulePrefix)('cyan', ...args)),
// colors
cyan: (...args) => console.log(createMsg('info', modulePrefix)('cyan', ...args)),
magenta: (...args) => console.log(createMsg('info', modulePrefix)('magenta', ...args)),
blue: (...args) => console.log(createMsg('info', modulePrefix)('blue', ...args)),
gray: (...args) => console.log(createMsg('info', modulePrefix)('gray', ...args)),
grey: (...args) => console.log(createMsg('info', modulePrefix)('gray', ...args)),
white: (...args) => console.log(createMsg('info', modulePrefix)('white', ...args)),
green: (...args) => console.log(createMsg('', modulePrefix)('green', ...args)),
yellow: (...args) => console.log(createMsg('', modulePrefix)('yellow', ...args)),
red: (...args) => console.log(createMsg('', modulePrefix)('red', ...args)),
hex: (...args) => console.log(createMsg('info', modulePrefix)('hex', ...args)),
json: (x, { colors = coloredOutput, depth = 4 } = {}) =>
process.env.NODE_ENV === 'production'
? console.log(toJson(x))
: console.log(util.inspect(x, { colors, depth })),
jsonDiff: (objA, objB) => {
const diff = jsondiffpatch.diff(objA, objB);
const output = jsondiffpatch.formatters.console.format(diff);
console.log(output);
},
});
/* eslint-enable no-console */
module.exports = log;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment