Last active
April 15, 2021 14:35
-
-
Save mholtzhausen/10a137274842d185a93bb4ce4236f65e to your computer and use it in GitHub Desktop.
Compact pared down version of npm debug module
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
const debug=require('./qdebug.js') | |
const log1 = debug('log1') | |
const log2 = debug('log2') | |
const sublog = log1.ext('sublog') | |
log1.info('hello world', {test:Math.random()}) | |
log2.info('hello world', {test:Math.random()}) | |
sublog.info('hello world', {test:Math.random()}) | |
`Console Output: | |
2021-04-15T14:31:25.877Z [info] log1 :: hello world { test: 0.44275945387389326 } | |
2021-04-15T14:31:25.898Z [info] log2 :: hello world { test: 0.15378543632861819 } | |
2021-04-15T14:31:25.901Z [info] log1.sublog :: hello world { test: 0.5442912417062558 } | |
` |
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
function qdebug(name, timestamp=true, delimiter=false) { | |
let dbg = function() { | |
let args = [] | |
if(timestamp)args.push((new Date()).toISOString()) | |
args=args.concat([...arguments]) | |
if(delimiter)args.push(`\n${delimiter}`) | |
console.log.apply( | |
console, | |
args | |
) | |
} | |
const subfns=["error", "warn", "info", "http", "verbose", "debug", "silly"] | |
subfns.forEach(subfn => { | |
if (subfn === 'error') { | |
dbg[subfn] = function() { | |
let err | |
try { throw new Error() } catch (e) { err = e } | |
dbg(`[${subfn}] ${name || ''} ::`, ...arguments, err) | |
} | |
} else { | |
dbg[subfn] = function() { | |
dbg(`[${subfn}] ${name || ''} ::`, ...arguments) | |
} | |
} | |
}) | |
dbg.ext = function(subname, subtimestamp, subdelimiter) { | |
if (!subname) return dbg | |
return qdebug( | |
name ? `${name}.${subname}` : subname, | |
subtimestamp==undefined ? timestamp : subtimestamp, | |
subdelimiter==undefined ? delimiter : subdelimiter | |
) | |
} | |
return dbg | |
} | |
module.exports = qdebug |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment