├── .editorconfig ├── .eslintrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── bin.js ├── example.sh ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true 4 | }, 5 | "rules": { 6 | // warnings 7 | "no-unused-expressions": 1, 8 | "no-warning-comments": 1, 9 | "no-native-reassign": 1, 10 | "no-invalid-regexp": 1, 11 | "no-debugger": 1, 12 | "no-console": 1, 13 | "no-empty": 1, 14 | "radix": 1, 15 | 16 | // errors 17 | "no-shadow-restricted-names": 2, 18 | "handle-callback-err": 2, 19 | "no-self-compare": 2, 20 | "no-empty-class": 2, 21 | "no-unused-vars": 2, 22 | "no-dupe-keys": 2, 23 | "valid-typeof": 2, 24 | "no-undef": 2, 25 | 26 | // stylistic errors 27 | "quotes": [2, "single", "avoid-escape"], 28 | "no-space-before-semi": 2, 29 | "space-unary-word-ops": 2, 30 | "no-spaced-func": 2, 31 | "yoda": "always", 32 | "new-cap": 2, 33 | 34 | // mute 35 | "no-use-before-define": 0, 36 | "eol-last": 0, 37 | "strict": 0, 38 | "eqeqeq": 0, 39 | "curly": 0 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # tmp files 2 | lib-cov 3 | *.seed 4 | *.log 5 | *.csv 6 | *.dat 7 | *.out 8 | *.pid 9 | *.gz 10 | 11 | # tmp folders 12 | pids/ 13 | logs/ 14 | results/ 15 | coverage/ 16 | 17 | # node.js 18 | node_modules/ 19 | npm-debug.log 20 | 21 | # osx 22 | .DS_Store 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "0.10" 3 | - "0.11" 4 | language: node_js 5 | script: "make test-travis" 6 | after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SRC = index.js 2 | 3 | include node_modules/make-lint/index.mk 4 | 5 | LINT_CONFIG = .eslintrc 6 | TESTS = test.js 7 | 8 | test: lint 9 | @NODE_ENV=test ./node_modules/.bin/mocha \ 10 | --require should \ 11 | $(TESTS) \ 12 | --bail 13 | 14 | test-cov: 15 | @NODE_ENV=test node \ 16 | node_modules/.bin/istanbul cover \ 17 | ./node_modules/.bin/_mocha \ 18 | -- -u exports \ 19 | --require should \ 20 | $(TESTS) \ 21 | --bail 22 | 23 | test-travis: 24 | @NODE_ENV=test node \ 25 | node_modules/.bin/istanbul cover \ 26 | ./node_modules/.bin/_mocha \ 27 | --report lcovonly \ 28 | -- -u exports \ 29 | --require should \ 30 | $(TESTS) \ 31 | --bail 32 | 33 | .PHONY: test 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # debug-to-json 2 | [![NPM version][npm-image]][npm-url] 3 | [![build status][travis-image]][travis-url] 4 | [![Test coverage][coveralls-image]][coveralls-url] 5 | [![Downloads][downloads-image]][downloads-url] 6 | 7 | Convert [`debug`](http://ghub.io/debug) logs to JSON. 8 | 9 | ## Installation 10 | ```bash 11 | npm install debug-to-json 12 | ``` 13 | 14 | ## Usage 15 | ```sh 16 | $ node ./app.js | dtj 17 | {"time":"2014-05-18T23:47:06.545Z","hostname":"tweedy","pid":27374,"level":"debug","name":"mymodule","message":"Starting mymodule#derp()"} 18 | ``` 19 | 20 | ## Why? 21 | [`debug`](http://ghub.io/debug) is a great module, but isn't particularly tied 22 | to any logging. So in order to make `debug` output more parseable we turn it 23 | into JSON first, which in turn can be streamed to your logging location of 24 | choice. Unix all the way down. 25 | 26 | ## See also 27 | - [bole](http://ghub.io/bole) - tiny JSON logger 28 | - [bistre](http://ghub.io/bistre) - pretty bole logs 29 | 30 | ## Contributors 31 | - [Yoshua Wuyts](https://github.com/yoshuawuyts) 32 | - [Hugh Kennedy](https://github.com/hughsk) 33 | 34 | ## License 35 | [MIT](https://tldrlegal.com/license/mit-license) 36 | 37 | [npm-image]: https://img.shields.io/npm/v/debug-to-json.svg?style=flat-square 38 | [npm-url]: https://npmjs.org/package/debug-to-json 39 | [travis-image]: https://img.shields.io/travis/yoshuawuyts/debug-to-json.svg?style=flat-square 40 | [travis-url]: https://travis-ci.org/yoshuawuyts/debug-to-json 41 | [coveralls-image]: https://img.shields.io/coveralls/yoshuawuyts/debug-to-json.svg?style=flat-square 42 | [coveralls-url]: https://coveralls.io/r/yoshuawuyts/debug-to-json?branch=master 43 | [downloads-image]: http://img.shields.io/npm/dm/debug-to-json.svg?style=flat-square 44 | [downloads-url]: https://npmjs.org/package/debug-to-json 45 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var through2 = require('through2'); 8 | var dtj = require('./'); 9 | 10 | /** 11 | * Parse. 12 | */ 13 | 14 | process.stdin.resume(); 15 | process.stdin.setEncoding('utf8'); 16 | process.stdin 17 | .pipe(dtj()) 18 | .pipe(through2.obj(function(data, _, next) { 19 | this.push(JSON.stringify(data)); 20 | this.push('\n'); 21 | next() 22 | })) 23 | .pipe(process.stdout); 24 | -------------------------------------------------------------------------------- /example.sh: -------------------------------------------------------------------------------- 1 | echo "Sun, 07 Dec 2014 09:44:50 GMT mocha:runner run suite ctx.type= with a mime" \ 2 | | node ./bin.js 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies 3 | */ 4 | 5 | var utcRegex = require('regex-utc-date'); 6 | var combine = require('stream-combiner'); 7 | var through2 = require('through2'); 8 | var split = require('split2'); 9 | 10 | /** 11 | * Expose `parse`. 12 | */ 13 | 14 | module.exports = createStream; 15 | 16 | function createStream() { 17 | var sp = split(); 18 | var tr = through2.obj(write); 19 | 20 | return combine(sp, tr); 21 | 22 | function write(data, _, next) { 23 | var parsed = parse(data); 24 | if (parsed) this.push(parsed); 25 | next(); 26 | } 27 | } 28 | 29 | /** 30 | * Parse a string input. 31 | * 32 | * Example input: 33 | * Sun, 07 Dec 2014 09:44:50 GMT mocha:runner run suite ctx.type= with a mime 34 | * 35 | * @param {String} data 36 | * @return {Object} 37 | * @api public 38 | */ 39 | 40 | function parse(data) { 41 | if ('string' != typeof data) return; 42 | 43 | var spl = data.trim().split(' '); 44 | if (!(spl.length >= 7)) return; 45 | 46 | var time = spl.shift(); 47 | var level = 'debug'; 48 | var name = spl.shift(); 49 | var message = spl.join(' '); 50 | 51 | return { 52 | time: time, 53 | level: level, 54 | name: name, 55 | message: message, 56 | }; 57 | } 58 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "debug-to-json", 3 | "version": "2.0.0", 4 | "description": "Convert debug logs to JSON", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "make test" 8 | }, 9 | "repository": "yoshuawuyts/debug-to-json", 10 | "bin": { 11 | "debug-to-json": "./bin.js", 12 | "dtj": "./bin.js" 13 | }, 14 | "keywords": [ 15 | "debug", 16 | "log", 17 | "JSON", 18 | "parse", 19 | "bistre", 20 | "bole" 21 | ], 22 | "license": "MIT", 23 | "dependencies": { 24 | "regex-utc-date": "^1.0.0", 25 | "split2": "^0.2.1", 26 | "stream-combiner": "^0.2.1", 27 | "through2": "^0.6.3" 28 | }, 29 | "devDependencies": { 30 | "istanbul": "^0.3.19", 31 | "make-lint": "^1.0.1", 32 | "mocha": "^2.3.2", 33 | "should": "^4.3.0" 34 | }, 35 | "files": [ 36 | "lib", 37 | "bin", 38 | "bin.js", 39 | "LICENSE", 40 | "index.js", 41 | "HISTORY.md" 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*eslint no-unused-expressions: 0*/ 2 | 3 | /** 4 | * Module dependencies 5 | */ 6 | 7 | var assert = require('assert'); 8 | var dtj = require('./'); 9 | 10 | /** 11 | * Test 12 | */ 13 | describe('debug to json', function() { 14 | it('should parse `debug` output', function(next) { 15 | var stream = dtj(); 16 | var str = '2014-12-07T09:44:50.000Z mocha:runner run suite ctx.type= with a mime'; 17 | 18 | stream.once('data', function(nw) { 19 | assert.deepEqual(nw, { 20 | time: '2014-12-07T09:44:50.000Z', 21 | level: 'debug', 22 | name: 'mocha:runner', 23 | message: 'run suite ctx.type= with a mime' 24 | }); 25 | 26 | next(); 27 | }); 28 | 29 | stream.end(str); 30 | }); 31 | 32 | // it('should return on non valid strings', function() { 33 | // assert.equal('undefined', typeof dtj(123)); 34 | // assert.equal('undefined', typeof dtj('. . . . .')); 35 | // assert.equal('undefined', typeof dtj('Sun, 07 Dec 2014 09:44:50 DERP')); 36 | // }); 37 | }); 38 | --------------------------------------------------------------------------------