├── .gitignore ├── CHANGELOG.md ├── README.md ├── package.json ├── rollup.config.js ├── src └── index.js └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | dist 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # node-console-group changelog 2 | 3 | ## 0.3.3 4 | 5 | * Add `console.warn` support ([#3](https://github.com/Rich-Harris/node-console-group/issues/3)) 6 | 7 | ## 0.3.2 8 | 9 | * Include dist files! 10 | 11 | ## 0.3.1 12 | 13 | * Update build process etc 14 | 15 | ## 0.3.0 16 | 17 | * Override `console.error` as well (fix `console.trace` as a corollary) 18 | 19 | ## 0.2.1 20 | 21 | * Consistently use `this._stdout` 22 | 23 | ## 0.2.0 24 | 25 | * `jsnext:main` 26 | 27 | ## 0.1.2 28 | 29 | * Better logging output, partly based on [nodejs/io.js#1727](https://github.com/nodejs/io.js/pull/1727/files) 30 | 31 | ## 0.1.1 32 | 33 | * First release 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-console-group 2 | 3 | In browsers, `console.group()` is an incredibly useful debugging tool: 4 | 5 | ```js 6 | function foo (input) { 7 | console.group('we are debugging', 'foo'); 8 | console.log('input value:'); 9 | console.log(input); 10 | console.group('nested group'); 11 | console.log('console.groupception') 12 | console.groupEnd(); 13 | console.groupEnd(); 14 | 15 | return input.answer; 16 | } 17 | 18 | foo({answer: 42}); 19 | ``` 20 | 21 | ![console-group-browser](https://cloud.githubusercontent.com/assets/1162160/7666720/72819c7a-fbbd-11e4-928f-a7b65586c077.png) 22 | 23 | But it doesn't exist in node.js! It was driving me crazy, so I created this package: 24 | 25 | ![console-group-terminal](https://cloud.githubusercontent.com/assets/1162160/7666721/7284b91e-fbbd-11e4-82a1-47ba4522a285.png) 26 | 27 | It's a 5 minute job - highly unsophisticated, doesn't even have a test suite, so YMMV. I'm not planning to actively maintain it (though I'll certainly take pull requests). Be warned! If that doesn't put you off, read on for usage instructions. 28 | 29 | ## Installation and usage 30 | 31 | Install... 32 | 33 | ```bash 34 | npm install console-group 35 | ``` 36 | 37 | ...and use. This overwrites the `console.log` method, and adds `console.group` and `console.groupEnd`. 38 | 39 | ```js 40 | require( 'console-group' ).install(); 41 | 42 | // later, if you want to clean up 43 | require( 'console-group' ).teardown(); 44 | ``` 45 | 46 | 47 | ## License 48 | 49 | MIT. 50 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "console-group", 3 | "version": "0.3.3", 4 | "description": "basic console.group implementation for node", 5 | "main": "dist/console-group.cjs.js", 6 | "module": "dist/console-group.es.js", 7 | "jsnext:main": "dist/console-group.es.js", 8 | "scripts": { 9 | "test": "node test/test.js", 10 | "build": "rollup -c", 11 | "pretest": "npm run build", 12 | "prepublish": "npm test" 13 | }, 14 | "files": [ 15 | "dist", 16 | "README.md" 17 | ], 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/rich-harris/node-console-group" 21 | }, 22 | "keywords": [ 23 | "console" 24 | ], 25 | "author": "Rich Harris", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/rich-harris/node-console-group/issues" 29 | }, 30 | "homepage": "https://github.com/rich-harris/node-console-group", 31 | "devDependencies": { 32 | "rollup": "^0.34.13", 33 | "rollup-plugin-buble": "^0.13.0" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import buble from 'rollup-plugin-buble'; 2 | 3 | const pkg = require( './package.json' ); 4 | 5 | export default { 6 | entry: 'src/index.js', 7 | plugins: [ buble() ], 8 | targets: [ 9 | { dest: pkg.module, format: 'es' }, 10 | { dest: pkg.main, format: 'cjs' } 11 | ] 12 | }; 13 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import * as util from 'util'; 2 | 3 | const originalLog = console.log; 4 | const originalWarn = console.warn; 5 | const originalError = console.error; 6 | 7 | let prefix = ''; 8 | 9 | function format ( args ) { 10 | return util.format( ...args ).replace( /^/gm, prefix ); 11 | } 12 | 13 | function consoleLog () { 14 | this._stdout.write( format( arguments ) + '\n' ); 15 | } 16 | 17 | function consoleError () { 18 | this._stderr.write( format( arguments ) + '\n' ); 19 | } 20 | 21 | function consoleGroup () { 22 | prefix += '⎢ '; 23 | this._stdout.write( '\u001b[1m' + format( arguments ) + '\u001b[22m\n' ); 24 | } 25 | 26 | function consoleGroupEnd () { 27 | prefix = prefix.slice( 0, -2 ); 28 | } 29 | 30 | export function install () { 31 | console.log = consoleLog; 32 | console.warn = console.error = consoleError; 33 | console.group = consoleGroup; 34 | console.groupEnd = consoleGroupEnd; 35 | } 36 | 37 | export function teardown () { 38 | console.log = originalLog; 39 | console.warn = originalWarn; 40 | console.error = originalError; 41 | delete console.group; 42 | delete console.groupEnd; 43 | } 44 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | require( '../' ).install(); 2 | 3 | console.group( 'test' ); 4 | console.log( 'indented' ); 5 | console.error( 'stdout' ); 6 | console.warn( 'also stdout' ); 7 | 8 | console.log( 'another indented line' ); 9 | 10 | console.group( 'test' ); 11 | console.log( 'double indented' ); 12 | console.log({ foo: 'bar', bar: 'baz', baz: 'qux', arr: [ 'a', 'b', 'c', 'd', 'e', 'f' ]}); 13 | 14 | console.trace( 'tracing!' ); 15 | console.groupEnd(); 16 | console.groupEnd(); 17 | --------------------------------------------------------------------------------