diff --git a/README.md b/README.md index be0a6f6..0f1c9e4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ulog v2.0.0-beta.5 +# ulog v2.0.0-beta.6 ### Microscopically small universal logging library [![npm](https://img.shields.io/npm/v/ulog.svg)](https://npmjs.com/package/ulog) @@ -65,8 +65,8 @@ You should also be able to use e.g. Yarn etc. If you want the file for the browser to include in your project yourself, you can download it from here. -* [ulog.umd.js](https://cdn.rawgit.com/download/ulog/2.0.0-beta.5/ulog.umd.js) (~3kB, source) -* [ulog.min.js](https://cdn.rawgit.com/download/ulog/2.0.0-beta.5/ulog.min.js) (~2kB, minified) +* [ulog.umd.js](https://cdn.rawgit.com/download/ulog/2.0.0-beta.6/ulog.umd.js) (~3kB, source) +* [ulog.min.js](https://cdn.rawgit.com/download/ulog/2.0.0-beta.6/ulog.min.js) (~2kB, minified) ## Include in your app @@ -94,7 +94,7 @@ const log = ulog('my-module') If you want, you can import `ulog` with a script tag: ```html - + ``` *myscript.js* diff --git a/debug.js b/debug.js index eadec99..a7ba226 100644 --- a/debug.js +++ b/debug.js @@ -5,12 +5,61 @@ ulog.formats.push(format); module.exports = ulog; function format(logger, method, args) { - args.unshift(time(), logger.name.substring(0,18).padEnd(20, ' ')) + var t = new Date().getTime(), + p = format.prev && format.prev.t || t + format.prev = { t } + args.unshift( + time(t,1,1,1), + diff(t,p), + name(logger.name) + ) } -function time(){ - var d = new Date(), - hr = d.getHours().toString().padStart(2,'0'), - min = d.getMinutes().toString().padStart(2,'0'); - return `${hr}:${min} ` +function time(t,h,m,s){ + var d = new Date(t) + return '' + + (h ? pad(LEFT, d.getHours(), 2, '0') + (m || s ? ':' : '') : '') + + (m ? pad(LEFT, d.getMinutes(), 2, '0') + (s ? ':' : '') : '') + + (s ? pad(LEFT, d.getSeconds(), 2, '0') : '') +} + +function diff(t,p) { + var d = new Date(t - p), + days = d.getUTCDate() - 1, + h = d.getUTCHours(), + m = d.getUTCMinutes(), + s = d.getUTCSeconds(), + f = Math.floor, + ms = (t - p - s*1000) + return pad(LEFT, ( + t-p<1000?(t-p>10? '.' + pad(LEFT, f((t-p)/10), 2, '0') : '') : // ms + days ? days + 'd' + pad(LEFT, h, 2, '0') : + h ? h + 'h' + pad(LEFT, m, 2, '0') : + m ? m + 'm' + pad(LEFT, s, 2, '0') : + s ? s + '.' + pad(LEFT, f(ms/10), 2, '0') : + '' + ), 5) +} + +function name(n) { + return pad(RIGHT, n.substring(0, 24), 24) +} + +var LEFT=1, + RIGHT=0 + +function pad(s,x,l,p) { + x = String(x) + p = p || ' ' + if (x.length > l || (s && x.length == l)) return x + l -= x.length + p = (p + repeat(p, l / p.length)).slice(0,l) + return (s?p:'') + x + (s?'':p); +} + +function repeat(x,c,s) { + if (!x.length || !c) return '' + s = '' + while(c>0) {s+=x; c--} + return s; } diff --git a/debug.spec.js b/debug.spec.js index 7d335d7..a0559d1 100644 --- a/debug.spec.js +++ b/debug.spec.js @@ -31,14 +31,14 @@ describe('debug = require(\'ulog/debug\')(named)', function(){ debug(message) expect(testConsole.debug.callCount).to.equal(1) expect(testConsole.debug.getCall(0).args[0]).to.be.a('string') - expect(testConsole.debug.getCall(0).args[0].length).to.equal(6) + expect(testConsole.debug.getCall(0).args[0].length).to.equal(8) } finally { debug.level = oldLevel testConsole.reset() } }) - it('adds the logger name to the log call', function(){ + it('adds the time elapsed since the previous call to the log call', function(){ testConsole.reset() try { debug.level = ulog.DEBUG @@ -48,8 +48,25 @@ describe('debug = require(\'ulog/debug\')(named)', function(){ debug(message) expect(testConsole.debug.callCount).to.equal(1) expect(testConsole.debug.getCall(0).args[1]).to.be.a('string') - expect(testConsole.debug.getCall(0).args[1].length).to.equal(20) - expect(testConsole.debug.getCall(0).args[1]).to.equal(debug.name.padEnd(20, ' ')) + expect(testConsole.debug.getCall(0).args[1].length).to.equal(5) + } finally { + debug.level = oldLevel + testConsole.reset() + } + }) + + it('adds the logger name to the log call', function(){ + testConsole.reset() + try { + debug.level = ulog.DEBUG + expect(debug.level).to.equal(debug.DEBUG) + expect(testConsole.debug.callCount).to.equal(0) + var message = 'Message' + debug(message) + expect(testConsole.debug.callCount).to.equal(1) + expect(testConsole.debug.getCall(0).args[2]).to.be.a('string') + expect(testConsole.debug.getCall(0).args[2].length).to.equal(24) + expect(testConsole.debug.getCall(0).args[2]).to.equal(debug.name.padEnd(24, ' ')) } finally { debug.level = oldLevel testConsole.reset() diff --git a/package-lock.json b/package-lock.json index a93dca7..8335532 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ulog", - "version": "2.0.0-beta.5", + "version": "2.0.0-beta.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 264432d..61631b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ulog", - "version": "2.0.0-beta.5", + "version": "2.0.0-beta.6", "description": "Microscopically small universal logging library", "main": "node.js", "unpkg": "ulog.min.js", diff --git a/ulog.js b/ulog.js index 73ae54c..0e67a22 100644 --- a/ulog.js +++ b/ulog.js @@ -43,7 +43,7 @@ function create(n,r) { function extend(o,p,l) { if (o.log) return o.NONE = 0 - o.ulog = {version:'2.0.0-beta.5'} + o.ulog = {version:'2.0.0-beta.6'} for (var key in LVL) {o[key] = LVL[key]} Object.defineProperty(o, 'level', { get: function(){return l !== undefined ? l : p && p.level}, diff --git a/ulog.min.js b/ulog.min.js index 336e13d..9df845f 100644 --- a/ulog.min.js +++ b/ulog.min.js @@ -1 +1 @@ -!function(e,n,o){"function"==typeof define&&define.amd?define(n,[],o):e[n]=o()}(this,"ulog",function(){"use strict";function log(e){return e?mods[e]||(mods[e]=extend(create(e),log)):extend(log)}log.formats=[],log.extends=[],log.enable=function(e){var n,o=(e||"").split(/[\s,]+/);for(n=0;n 1 && names[a[0]] ? a.shift() : 'debug'; for (var i=0; i 1 && names[a[0]] ? a.shift() : 'debug'; for (var i=0; i