diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..5497bf4 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,17 @@ +{ + "plugins": [ + "mocha" + ], + "extends": "@aptoma/eslint-config", + "rules": { + "object-curly-spacing": ["error", "always"] + }, + "parserOptions": { + "ecmaVersion": 9 + }, + "env": { + "node": true, + "mocha": true, + "es6": true + } +} diff --git a/.gitignore b/.gitignore index ba2a97b..a525206 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules coverage +package-lock.json +.nyc_output diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 89bac83..0000000 --- a/.jshintrc +++ /dev/null @@ -1,47 +0,0 @@ -{ - "browser" : false, - "devel" : true, - "node" : true, - "asi" : false, - "bitwise" : false, - "boss" : false, - "curly" : true, - "debug" : false, - "eqeqeq" : true, - "eqnull" : true, - "evil" : false, - "forin" : false, - "immed" : true, - "laxbreak" : false, - "maxerr" : 100, - "maxlen" : 0, - "maxcomplexity": 10, - "maxdepth": 3, - "maxparams": 5, - "maxstatements": 15, - "newcap" : false, - "noarg" : true, - "noempty" : false, - "nonew" : false, - "nomen" : false, - "nonbsp": true, - "onevar" : false, - "passfail" : false, - "plusplus" : false, - "regexp" : false, - "undef" : true, - "unused" : "vars", - "sub" : true, - "strict" : true, - "globalstrict": true, - "white" : true, - "expr" : true, - "smarttabs": true, - "predef" : [ - "-Promise", - "describe", - "it", - "beforeEach", - "afterEach" - ] -} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 21e9db8..1cde613 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,4 @@ language: node_js node_js: - - 0.10 - -script: - - npm run ci + - 12 + - 14 diff --git a/README.md b/README.md index dd1e1b9..5aed4ca 100644 --- a/README.md +++ b/README.md @@ -28,70 +28,72 @@ Show specific names & levels ## Examples - var log = require('aut')(); - log('foo bar'); - log('foo bar', { a: 'b' }); - log.error(new Error('Shit')); - log.debug('hmm %s thats cool', 'wow'); - log.warn('oh my'); - log.info('aha..'); - - // 13 Nov 16:41:53 - foo bar - // 13 Nov 16:41:53 - foo bar { a: 'b' } - // 13 Nov 16:41:53 [error] - Error: Shit - // at Object. (/Users/martin/foo/test.js:4:11) - // at Module._compile (module.js:456:26) - // at Object.Module._extensions..js (module.js:474:10) - // at Module.load (module.js:356:32) - // at Function.Module._load (module.js:312:12) - // at Function.Module.runMain (module.js:497:10) - // at startup (node.js:119:16) - // at node.js:906:3 - // 13 Nov 16:41:53 [debug] - hmm wow thats cool - // 13 Nov 16:41:53 [warn] - oh my - // 13 Nov 16:41:53 [info] - aha.. - - var log2 = require('aut')('log2'); - log2('foo bar'); - log2('foo bar', { a: 'b' }); - log2.error(new Error('Shit')); - log2.debug('hmm %s thats cool', 'wow'); - log2.warn('oh my'); - log2.info('aha..'); - - // 13 Nov 16:41:53 log2 - foo bar - // 13 Nov 16:41:53 log2 - foo bar { a: 'b' } - // 13 Nov 16:41:53 log2 [error] - Error: Shit - // at Object. (/Users/martin/foo/test.js:27:12) - // at Module._compile (module.js:456:26) - // at Object.Module._extensions..js (module.js:474:10) - // at Module.load (module.js:356:32) - // at Function.Module._load (module.js:312:12) - // at Function.Module.runMain (module.js:497:10) - // at startup (node.js:119:16) - // at node.js:906:3 - // 13 Nov 16:41:53 log2 [debug] - hmm wow thats cool - // 13 Nov 16:41:53 log2 [warn] - oh my - // 13 Nov 16:41:53 log2 [info] - aha.. - - var log3 = require('aut')('custom', { - date: function () { return 'My Custom Date'; }, - write: function (msg) { console.log(msg); }, - levels: ['mega', 'alpha'], - format: function (data) { - return Object.keys(data).map(function (k) { - return k + ': ' + data[k]; - }).join(', '); - } - }); - - log3('foo bar'); - log3('foo bar', { a: 'b' }); - log3.mega(new Error('Shit')); - log3.alpha('hmm'); - - // date: My Custom Date, prefix: custom, args: [object Arguments] - // date: My Custom Date, prefix: custom, args: [object Arguments] - // date: My Custom Date, prefix: custom, args: [object Arguments], level: mega - // date: My Custom Date, prefix: custom, args: [object Arguments], level: alpha +```javascript +const log = require('aut')(); +log('foo bar'); +log('foo bar', { a: 'b' }); +log.error(new Error('Shit')); +log.debug('hmm %s thats cool', 'wow'); +log.warn('oh my'); +log.info('aha..'); + +// 13 Nov 16:41:53 - foo bar +// 13 Nov 16:41:53 - foo bar { a: 'b' } +// 13 Nov 16:41:53 [error] - Error: Shit +// at Object. (/Users/martin/foo/test.js:4:11) +// at Module._compile (module.js:456:26) +// at Object.Module._extensions..js (module.js:474:10) +// at Module.load (module.js:356:32) +// at Function.Module._load (module.js:312:12) +// at Function.Module.runMain (module.js:497:10) +// at startup (node.js:119:16) +// at node.js:906:3 +// 13 Nov 16:41:53 [debug] - hmm wow thats cool +// 13 Nov 16:41:53 [warn] - oh my +// 13 Nov 16:41:53 [info] - aha.. + +const log2 = require('aut')('log2'); +log2('foo bar'); +log2('foo bar', { a: 'b' }); +log2.error(new Error('Shit')); +log2.debug('hmm %s thats cool', 'wow'); +log2.warn('oh my'); +log2.info('aha..'); + +// 13 Nov 16:41:53 log2 - foo bar +// 13 Nov 16:41:53 log2 - foo bar { a: 'b' } +// 13 Nov 16:41:53 log2 [error] - Error: Shit +// at Object. (/Users/martin/foo/test.js:27:12) +// at Module._compile (module.js:456:26) +// at Object.Module._extensions..js (module.js:474:10) +// at Module.load (module.js:356:32) +// at Function.Module._load (module.js:312:12) +// at Function.Module.runMain (module.js:497:10) +// at startup (node.js:119:16) +// at node.js:906:3 +// 13 Nov 16:41:53 log2 [debug] - hmm wow thats cool +// 13 Nov 16:41:53 log2 [warn] - oh my +// 13 Nov 16:41:53 log2 [info] - aha.. + +const log3 = require('aut')('custom', { + date: function () { return 'My Custom Date'; }, + write: function (msg) { console.log(msg); }, + levels: ['mega', 'alpha'], + format: function (data) { + return Object.keys(data).map(function (k) { + return k + ': ' + data[k]; + }).join(', '); + } +}); + +log3('foo bar'); +log3('foo bar', { a: 'b' }); +log3.mega(new Error('Shit')); +log3.alpha('hmm'); + +// date: My Custom Date, prefix: custom, args: [object Arguments] +// date: My Custom Date, prefix: custom, args: [object Arguments] +// date: My Custom Date, prefix: custom, args: [object Arguments], level: mega +// date: My Custom Date, prefix: custom, args: [object Arguments], level: alpha +``` diff --git a/index.js b/index.js index 8b16b07..41d409e 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,12 @@ 'use strict'; -var moment = require('moment'), - inspect = require('util').inspect, - printf = require('util').format, - colors = require('colors'); -var emptyFn = function () {}, - useColor = Boolean(process.stdout.isTTY); +const moment = require('moment'); +const inspect = require('util').inspect; +const printf = require('util').format; +const colors = require('colors'); + +const emptyFn = function () {}; +const useColor = Boolean(process.stdout.isTTY); colors.setTheme({ error: 'red', @@ -28,8 +29,8 @@ function parseFilter(type, keys, enable, skip) { } enable.all[type] = false; - var split = keys.split(/[\s,]+/); - split.forEach(function (key) { + const split = keys.split(/[\s,]+/); + split.forEach((key) => { if (!key) { return; } @@ -97,7 +98,7 @@ function date() { * @param {String} msg */ function write(msg) { - console.log.apply(console, [msg]); + console.log(msg); } /** @@ -106,7 +107,7 @@ function write(msg) { * @return {String} */ function format(data) { - var args = Array.prototype.slice.call(data.args, 0).map(function (arg) { + const args = data.args.map((arg) => { if (arg instanceof Error) { return arg.stack; } @@ -117,9 +118,9 @@ function format(data) { return arg; }); - var msg = printf.apply(printf, args), - fmt = ['%s'], - vals = [colorize('grey', data.date)]; + const msg = printf.apply(printf, args); + const fmt = ['%s']; + const vals = [colorize('grey', data.date)]; if (data.name) { fmt.push('%s'); @@ -149,40 +150,40 @@ function format(data) { * @return {Function} */ function log(name, opts) { - if (typeof(name) === 'object') { + if (typeof (name) === 'object') { opts = name; name = undefined; } else { opts = opts || {}; } - var dateFn = opts.date || date, - formatFn = opts.format || format, - writeFn = opts.write || write, - levels = ['error', 'warn', 'info', 'debug'].concat(opts.levels || []), - enable = { all: {}, name: {}, level: {} }, - skip = { name: {}, level: {} }; + const dateFn = opts.date || date; + const formatFn = opts.format || format; + const writeFn = opts.write || write; + const levels = ['error', 'warn', 'info', 'debug'].concat(opts.levels || []); + const enable = { all: {}, name: {}, level: {} }; + const skip = { name: {}, level: {} }; parseFilter('name', opts.nameFilter || process.env.LOGNAMES, enable, skip); parseFilter('level', opts.levelFilter || process.env.LOGLEVELS, enable, skip); - var logger = function (level) { + const logger = function (level) { if (!enabled(name, level, enable, skip)) { return emptyFn; } - return function () { + return (...args) => { writeFn(formatFn({ date: dateFn(), name: name, - args: arguments, + args: args || [], level: level })); }; }; - var fn = logger(); + const fn = logger(); - levels.forEach(function (level) { + levels.forEach((level) => { fn[level] = logger(level); }); @@ -195,7 +196,6 @@ function log(name, opts) { return fn; } - module.exports = function (name, opts) { - return log(name, opts); -}; \ No newline at end of file + return log(name, opts); +}; diff --git a/package.json b/package.json index 835091e..d9398b7 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "description": "Simple logging library with filtering using environment variables", "main": "index.js", "scripts": { - "test": "jshint test/ index.js && istanbul test --preload-sources _mocha -- -u exports -R spec 'test/**/*.test.js'", - "ci": "npm test --coverage && istanbul report cobertura", + "lint": "eslint --ext '.js' index.js test", + "watch": "mocha --watch 'test/**/*.js' 'index.js' --timeout 500", + "test": "npm run lint && nyc --reporter=text-summary --reporter=lcov mocha", "release": "npm test && release-it -n -i patch", "release:minor": "npm test && release-it -n -i minor", "release:major": "npm test && release-it -n -i major" @@ -14,6 +15,9 @@ "type": "git", "url": "https://github.com/martinj/node-aut" }, + "engines": { + "node": ">=12.0.0" + }, "keywords": [ "log", "logging" @@ -25,14 +29,16 @@ }, "homepage": "https://github.com/martinj/node-aut", "dependencies": { - "colors": "^1.0.3", - "moment": "^2.8.3" + "colors": "^1.4.0", + "moment": "^2.29.1" }, "devDependencies": { - "istanbul": "^0.3.2", - "jshint": "^2.5.6", - "mocha": "^1.21.4", - "release-it": "0.0.11", - "should": "^4.0.4" + "@aptoma/eslint-config": "^7.0.1", + "eslint": "^7.20.0", + "eslint-plugin-mocha": "^8.0.0", + "mocha": "^8.3.0", + "nyc": "^15.1.0", + "release-it": "^14.4.1", + "should": "^13.2.3" } -} \ No newline at end of file +} diff --git a/test/index.test.js b/test/index.test.js index b424c49..4148c9a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -2,19 +2,19 @@ process.env.NODE_ENV = 'test'; require('should'); -describe('aut', function () { - it('should add custom levels', function () { - var log = require('../')({ +describe('aut', () => { + it('should add custom levels', () => { + const log = require('../')({ levels: ['foo'] }); log.foo.should.be.a.Function; }); - it('should format output', function (done) { - var log = require('../')('test', { - date: function () { return 'Date'; }, - write: function (str) { + it('should format output', (done) => { + const log = require('../')('test', { + date: () => 'Date', + write(str) { str.should.equal('\u001b[90mDate\u001b[39m test - foobar'); done(); } @@ -23,8 +23,8 @@ describe('aut', function () { log('foo%s', 'bar'); }); - it('should filter names', function () { - var log = require('../')({ + it('should filter names', () => { + let log = require('../')({ nameFilter: '*,-log2' }); @@ -40,8 +40,8 @@ describe('aut', function () { log.enabled('log3').should.be.false; }); - it('should filter levels', function () { - var log = require('../')({ + it('should filter levels', () => { + let log = require('../')({ levelFilter: '*,-warn' }); log.enabled(undefined, 'debug').should.be.true; @@ -55,4 +55,4 @@ describe('aut', function () { log.enabled(undefined, 'debug').should.be.true; log.enabled(undefined, 'error').should.be.false; }); -}); \ No newline at end of file +});