Last active
January 25, 2021 18:11
-
-
Save lidio601/94ffbb0d3fa68f98ffb3457e04ee13d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
import _ from "lodash"; | |
const DEBUG = !process.env.NODE_ENV || process.env.NODE_ENV === "development"; | |
class ConfigHolder { | |
constructor(initialValue) { | |
this.value = initialValue; | |
} | |
set(newValue) { | |
this.value = newValue; | |
} | |
get() { | |
return this.value; | |
} | |
} | |
function makeLogger(prefix, config) { | |
return function (message, optionalParams) { | |
if (config.get()) { | |
var args = Array.prototype.slice.call(arguments); | |
args[0] = `${prefix} :: ${message}`; | |
console.log.apply(console, args); | |
} | |
}; | |
} | |
function makeLoggerE(prefix) { | |
return function (message, optionalParams) { | |
var args = Array.prototype.slice.call(arguments); | |
args[0] = `${prefix} :: ${message}`; | |
console.error.apply(console, args); | |
}; | |
} | |
function makeLoggerW(prefix) { | |
return function (message, optionalParams) { | |
var args = Array.prototype.slice.call(arguments); | |
args[0] = `${prefix} :: ${message}`; | |
console.warn.apply(console, args); | |
}; | |
} | |
export function makeLoggers(prefix, debug) { | |
const config = new ConfigHolder(DEBUG); | |
// DEBUG from dotenv.ts is a global switch | |
// debug parameter can be used to selectively switch on/off a module | |
if (DEBUG) { | |
config.set(_.defaultTo(debug, DEBUG)); | |
} | |
if (!config.get()) { | |
if (DEBUG) { | |
console.log(`${prefix} logger is disabled`); | |
} | |
} | |
return { | |
log: makeLogger(prefix, config), | |
loge: makeLoggerE(prefix, config), | |
logw: makeLoggerW(prefix, config), | |
setDebug: (enabled) => { | |
// ignore when undefined | |
if (_.isNil(enabled)) { | |
return; | |
} | |
// if turned off | |
if (debug && !enabled) { | |
if (DEBUG) { | |
console.log(`${prefix} logger is disabled`); | |
} | |
} | |
debug = enabled; // eslint-disable-line no-param-reassign | |
config.set(enabled); | |
}, | |
}; | |
} | |
export default makeLoggers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _cloneDeep from 'lodash/cloneDeep'; | |
import _defaultTo from 'lodash/defaultTo'; | |
import _extend from 'lodash/extend'; | |
import _isArray from 'lodash/isArray'; | |
import _isNil from 'lodash/isNil'; | |
const DEBUG = !process.env.NODE_ENV || process.env.NODE_ENV === 'development'; | |
export interface ILogger { | |
log: (message: string, ...args: any) => void; | |
logi: (message: string, ...args: any) => void; | |
loge: (message: string, ...args: any) => void; | |
logw: (message: string, ...args: any) => void; | |
fork: (prefix: string) => ILogger; | |
setDebug: (enabled: boolean) => void; | |
} | |
class ConfigHolder<T> { | |
private value: T; | |
constructor(initialValue: T) { | |
this.value = initialValue; | |
} | |
set(newValue: Partial<T>) { | |
if (_isArray(this.value)) { | |
this.value = newValue as T; | |
} else if (typeof this.value === 'object') { | |
_extend(this.value, newValue); | |
} else { | |
this.value = newValue as T; | |
} | |
} | |
get() { | |
return _cloneDeep(this.value); | |
} | |
} | |
function makeLogger(prefix: string, config?: ConfigHolder) { | |
return function (message: string, ...args: any) { | |
if (!config || config.get()) { | |
console.log(...[`${prefix} :: ${message}`, ...args]); | |
} | |
}; | |
} | |
function makeLoggerE(prefix: string) { | |
return function (message: string, ...args: any) { | |
console.error(...[`${prefix} :: ${message}`, ...args]); | |
}; | |
} | |
function makeLoggerW(prefix: string) { | |
return function (message: string, ...args: any) { | |
console.warn(...[`${prefix} :: ${message}`, ...args]); | |
}; | |
} | |
function _makeLoggers( | |
prefix: string, | |
config: ConfigHolder, | |
): ILogger & {logger: ILogger} { | |
let debug = config.get(); | |
const logger = { | |
log: makeLogger(prefix, config), | |
logi: makeLogger(prefix, new ConfigHolder(DEBUG)), | |
loge: makeLoggerE(prefix), | |
logw: makeLoggerW(prefix), | |
fork: (subprefix: string) => { | |
return _makeLoggers(`${prefix} :: ${subprefix}`, config); | |
}, | |
setDebug: (enabled: boolean) => { | |
// ignore when undefined | |
if (_.isNil(enabled)) { | |
return; | |
} | |
// if turned off | |
if (debug && !enabled) { | |
if (DEBUG) { | |
console.log(`${prefix} logger is disabled`); | |
} | |
} | |
debug = enabled; | |
config.set(enabled); | |
}, | |
}; | |
return { | |
...logger, | |
logger, | |
}; | |
} | |
export function makeLoggers(prefix: string, debug?: boolean) { | |
const config = new ConfigHolder(DEBUG); | |
// DEBUG from dotenv.ts is a global switch | |
// debug parameter can be used to selectively switch on/off a module | |
if (DEBUG) { | |
config.set(_.defaultTo(debug, DEBUG)); | |
} | |
if (!config.get()) { | |
if (DEBUG) { | |
console.log(`${prefix} logger is disabled`); | |
} | |
} | |
return _makeLoggers(prefix, config); | |
} | |
export default makeLoggers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment