Skip to content

Instantly share code, notes, and snippets.

@CatTail
Last active August 3, 2016 04:38
Show Gist options
  • Save CatTail/2b7755719c1b73f052759d28b57b18f0 to your computer and use it in GitHub Desktop.
Save CatTail/2b7755719c1b73f052759d28b57b18f0 to your computer and use it in GitHub Desktop.
A little console wrapper to log with filename and line number (without console.log('xxxxx') anymore)
$ node test.js
at Object.<anonymous> (/Users/cattail/workspace/upyun/choppe/test.js:3:9) hello
at Object.<anonymous> (/Users/cattail/workspace/upyun/choppe/test.js:5:9) world
require('./xxxx')
console.log('hello')
console.log('world')
var isProduction = process.env.NODE_ENV === 'production'
var methods = ['log', 'info', 'warn', 'error']
if (!isProduction) {
methods.forEach(function (method) {
var original = console[method]
console[method] = function() {
var error = new Error()
stack = error.stack.split('\n')
var args = [].slice.call(arguments)
args.unshift(stack[2].trim())
original.apply(console, args)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment