Skip to content

Commit

Permalink
2.0.0-beta.6
Browse files Browse the repository at this point in the history
Made 'ulog/debug' use only ES5 code
Added formatting of time elapsed since last call
  • Loading branch information
Download committed Dec 3, 2018
1 parent dacbc0d commit d91a29a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 21 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ulog <sub><sup>v2.0.0-beta.5</sup></sub>
# ulog <sub><sup>v2.0.0-beta.6</sup></sub>
### Microscopically small universal logging library

[![npm](https://img.shields.io/npm/v/ulog.svg)](https://npmjs.com/package/ulog)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -94,7 +94,7 @@ const log = ulog('my-module')
If you want, you can import `ulog` with a script tag:

```html
<script src="https://cdn.rawgit.com/download/ulog/2.0.0-beta.5/ulog.min.js"></script>
<script src="https://cdn.rawgit.com/download/ulog/2.0.0-beta.6/ulog.min.js"></script>
<script src="myscript.js"></script>
```
*myscript.js*
Expand Down
61 changes: 55 additions & 6 deletions debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 21 additions & 4 deletions debug.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion ulog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion ulog.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ulog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ describe('var log = require(\'ulog\')(\'my-module-name\')', function(){
it('can prefix log messages with the logger name', function(){
testConsole.reset()
format = sinon.spy(function(l,m,a){
console.info('format before', a)
a.splice(0,0,l.name)
console.info('format after', a)
})
log.formats.push(format)
try {
Expand Down
2 changes: 1 addition & 1 deletion ulog.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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},
Expand Down

0 comments on commit d91a29a

Please sign in to comment.