├── .gitignore ├── History.md ├── README.md ├── console-trace.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.2.1 / 2012-07-06 3 | ================== 4 | 5 | * Added Buffer support [kilianc] 6 | * Fixed missing default color for trace method [kilianc] 7 | * Fixed missing padding [kilianc] 8 | 9 | 0.2.0 / 2012-07-06 10 | ================== 11 | 12 | * Added custom colors 13 | * Fixed output redirection bug 14 | * Misc fixes and improvements [kilianc] 15 | * Added right alignment for callsite info [kilianc] 16 | * Added `console.traced`, removed `console.trace` 17 | 18 | 0.1.0 / 2012-03-26 19 | ================== 20 | 21 | * Initial release 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # console-trace 2 | 3 | Extends the native Node.JS `console` object to prefix logging functions 4 | with the [CallSite](http://github.com/visionmedia/callsite) information. 5 | 6 | To read more about runtime stack trace introspection you can refer to [this 7 | article](http://www.devthought.com/2011/12/22/a-string-is-not-an-error/#beyond). 8 | 9 | ![](http://f.cl.ly/items/1T2K0H0i2H2J0C3q3H2u/console-trace.png) 10 | 11 | ## Installation 12 | 13 | $ npm install console-trace 14 | 15 | ### Syntax: 16 | 17 | ```javascript 18 | require('console-trace')([options]) 19 | ``` 20 | 21 | ### Available Options: 22 | 23 | * __always__ - (`Boolean`: defaults to false) always print the callsite info even without accessing methods from the `t` or `traced` getters. 24 | * __cwd__ - (`String`: defaults to `process.cwd()`) the path that will be stripped from the callsite info 25 | * __colors__ - (`Boolean|Object`: defaults to true) terminal colors support flag or a custom color object 26 | * __right__ - (`Boolean`: defaults to false) callsite alignment flag, when true prints infos on the right 27 | 28 | ### Examples: 29 | 30 | ```javascript 31 | require('console-trace') 32 | ``` 33 | 34 | You can add the `t` or `traced` getter to your calls to obtain a stacktrace: 35 | 36 | ```javascript 37 | console.t.log('a'); 38 | console.traced.log('a'); 39 | ``` 40 | 41 | You can also make every console call trace: 42 | 43 | ```javascript 44 | require('console-trace')({ 45 | always: true, 46 | }) 47 | 48 | ... 49 | 50 | console.log('a'); // tracing 51 | console.error('a'); // tracing 52 | ``` 53 | 54 | You can align the callsite infos to the right 55 | 56 | ```javascript 57 | require('console-trace')({ 58 | always: true, 59 | right: true 60 | }) 61 | 62 | ... 63 | 64 | console.log('a'); // tracing right 65 | console.error('a'); // tracing right 66 | ``` 67 | 68 | You can change defaults colors too 69 | 70 | ```javascript 71 | require('./console-trace')({ 72 | always: true, 73 | colors: { 74 | warn: '35', 75 | info: '32' 76 | } 77 | }) 78 | 79 | ... 80 | 81 | console.warn('a'); // magenta 82 | console.info('a'); // green 83 | ``` 84 | 85 | To customize the string that's prefixed to the calls, override the 86 | `console.traceFormat` function. 87 | 88 | ## Beyond console 89 | 90 | If you have more sophisticated logging needs, or don't wish to extend 91 | `console`, I suggest you look at [tracer](https://github.com/baryon/tracer). 92 | 93 | ## Credits 94 | 95 | * [Guillermo Rauch](https://github.com/guille) 96 | * [Kilian Ciuffolo](https://github.com/kilianc) 97 | * [Nicholas Manousos](https://github.com/nmanousos) 98 | 99 | ## License 100 | 101 | (The MIT License) 102 | 103 | Copyright (c) 2012 Guillermo Rauch <guillermo@learnboost.com> 104 | 105 | Permission is hereby granted, free of charge, to any person obtaining 106 | a copy of this software and associated documentation files (the 107 | 'Software'), to deal in the Software without restriction, including 108 | without limitation the rights to use, copy, modify, merge, publish, 109 | distribute, sublicense, and/or sell copies of the Software, and to 110 | permit persons to whom the Software is furnished to do so, subject to 111 | the following conditions: 112 | 113 | The above copyright notice and this permission notice shall be 114 | included in all copies or substantial portions of the Software. 115 | 116 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 117 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 118 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 119 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 120 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 121 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 122 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /console-trace.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | var callsite = require('callsite') 7 | , tty = require('tty') 8 | , isatty = Boolean(tty.isatty() && process.stdout.getWindowSize) 9 | , defaultColors = { log: '90', error: '91', warn: '93', info: '96', trace: '90' } 10 | 11 | console.traceOptions = Object.create(null); 12 | console.traceOptions.cwd = process.cwd() + '/'; 13 | console.traceOptions.colors = true; 14 | 15 | /** 16 | * Store custom options 17 | * 18 | * @param {Object} options 19 | * @api public 20 | */ 21 | 22 | module.exports = function (options) { 23 | if (options) { 24 | options.cwd = options.cwd || console.traceOptions.cwd; 25 | console.traceOptions = options; 26 | } 27 | } 28 | 29 | /** 30 | * Overrides the console methods. 31 | */ 32 | 33 | ;['error', 'log', 'info', 'warn', 'trace'].forEach(function (name) { 34 | var fn = console[name]; 35 | console[name] = function () { 36 | if (console._trace || console.traceOptions.always) { 37 | if (Buffer.isBuffer(arguments[0])) { 38 | arguments[0] = arguments[0].inspect() 39 | } else if (typeof arguments[0] === 'object') { 40 | arguments[0] = JSON.stringify(arguments[0], null, ' '); 41 | } 42 | var pad = (arguments[0] && !console.traceOptions.right || !isatty ? ' ' : ''); 43 | arguments[0] = console.traceFormat(__stack[1], name) + pad + arguments[0]; 44 | } 45 | console._trace = false; 46 | return fn.apply(this, arguments); 47 | } 48 | }); 49 | 50 | /** 51 | * Overridable formatting function. 52 | * 53 | * @param {CallSite} 54 | * @param {String} calling method 55 | * @api public 56 | */ 57 | 58 | console.traceFormat = function (call, method) { 59 | var basename = call.getFileName().replace(console.traceOptions.cwd, '') 60 | , str = '[' + basename + ':' + call.getLineNumber() + ']' 61 | , color = '99' 62 | 63 | if (!isatty) { 64 | return str; 65 | } 66 | 67 | if (console.traceOptions.colors !== false) { 68 | if (console.traceOptions.colors === undefined || console.traceOptions.colors[method] === undefined) { 69 | color = defaultColors[method]; 70 | } else { 71 | color = console.traceOptions.colors[method]; 72 | } 73 | } 74 | 75 | if (console.traceOptions.right) { 76 | var rowWidth = process.stdout.getWindowSize()[0]; 77 | return '\033[s' + // save current position 78 | '\033[' + rowWidth + 'D' + // move to the start of the line 79 | '\033[' + (rowWidth - str.length) + 'C' + // align right 80 | '\033[' + color + 'm' + str + '\033[39m' + 81 | '\033[u'; // restore current position 82 | } else { 83 | return '\033[' + color + 'm' + str + '\033[39m'; 84 | } 85 | } 86 | 87 | /** 88 | * Adds trace getter to the `console` object. 89 | * 90 | * @api public 91 | */ 92 | 93 | function getter () { 94 | this._trace = true; 95 | return this; 96 | } 97 | 98 | console.__defineGetter__('t', getter); 99 | console.__defineGetter__('traced', getter); 100 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "console-trace" 3 | , "version": "0.2.1" 4 | , "description": "Adds a handy `trace` flag to the console object to prepend the file and line number" 5 | , "main": "./console-trace" 6 | , "dependencies": { 7 | "callsite": "*" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | require('./console-trace') 2 | require('./console-trace')({}) // should work even if I require it twice 3 | 4 | process.stdout.write('\n'); 5 | 6 | ;['error', 'log', 'info', 'warn'].forEach(function (name) { 7 | process.stdout.write(' '); 8 | console[name]('regular console.%s, no clue where it came from', name); 9 | }); 10 | 11 | process.stdout.write('\n---------------------------------------------------------\n\n'); 12 | 13 | ;['error', 'log', 'info', 'warn'].forEach(function (name) { 14 | process.stdout.write(' '); 15 | console.traced[name]('this is a traced console.%s', name); 16 | }); 17 | 18 | process.stdout.write('\n---------------------------------------------------------\n\n'); 19 | 20 | console.traceOptions.colors = false; 21 | 22 | ;['error', 'log', 'info', 'warn'].forEach(function (name) { 23 | process.stdout.write(' '); 24 | console.traced[name]('this is an uncolored traced console.%s', name); 25 | }); 26 | 27 | process.stdout.write('\n---------------------------------------------------------\n\n'); 28 | 29 | console.traceOptions.right = true; 30 | console.traceOptions.colors = true; 31 | 32 | ;['error', 'log', 'info', 'warn'].forEach(function (name) { 33 | process.stdout.write(' '); 34 | console.traced[name]('this is a colored right aligned traced console.%s', name); 35 | }); 36 | 37 | process.stdout.write('\n---------------------------------------------------------\n\n'); 38 | 39 | console.traceOptions.always = true; 40 | console.traceOptions.colors = false; 41 | 42 | ;['error', 'log', 'info', 'warn'].forEach(function (name) { 43 | process.stdout.write(' '); 44 | console[name]('this is an uncolored right aligned traced console.%s', name); 45 | }); 46 | 47 | process.stdout.write('\n---------------------------------------------------------\n\n'); 48 | 49 | require('./console-trace')({ 50 | colors: { 51 | log: '35', 52 | warn: '35', 53 | error: '35', 54 | trace: '35', 55 | info: '35' 56 | } 57 | }) 58 | 59 | ;['error', 'log', 'info', 'warn', 'trace'].forEach(function (name) { 60 | process.stdout.write(' '); 61 | console.traced[name]('this is a magenta traced console.' + name); 62 | }); 63 | 64 | process.stdout.write('\n---------------------------------------------------------\n\n'); 65 | 66 | console.traceOptions.colors = true; 67 | 68 | process.stdout.write(' '); 69 | console.traced.log({ 1: 'works', 2: 'with', 3: 'Object' }); 70 | 71 | process.stdout.write(' '); 72 | console.traced.log(['Works', 'with', 'Array']); 73 | 74 | process.stdout.write(' '); 75 | console.traced.log('Works with Buffer', Buffer('FooBar')); --------------------------------------------------------------------------------