From 355d8a94ae6f91497caabb261ec76b287a38e295 Mon Sep 17 00:00:00 2001 From: Stijn de Witt Date: Wed, 27 Dec 2017 14:06:22 +0100 Subject: [PATCH] 2.0.0-beta.1: API improvements v2 Should be a backward-compatible release. But possibly some (very) old browsers are no longer supported because of the use of Function.prototype.name. Tested on Internet Explorer 11, Edge 41, Opera 49, Chrome 63. Please test before upgrading and report any issues in the [issue tracker](https://github.com/download/ulog/issues). Not tested in Safari because I do not have access to a Mac. Help in testing welcome! * Loggers are functions now. Supports debug-style API * Logger function defaults to level DEBUG * Logger function accepts a level name as first parameter * Added aliases 'verbose' and 'silly' for levels 'log' and 'trace', respectively * Ulog now supports hooks for logger extension and formatters * Ulog should now be a drop-in replacement for [debug](https://npmjs.com/package/debug) --- .travis.yml | 1 + README.md | 8 +- browser.js | 7 +- build/build-umd.js | 8 +- microscopical.spec.js | 2 +- node.js | 25 +- package.json | 7 +- test/test.html | 16 - ulog.js | 66 ++-- ulog.min.js | 2 +- ulog.spec.js | 847 ++++++++++++++++++++++-------------------- ulog.umd.js | 77 ++-- 12 files changed, 539 insertions(+), 527 deletions(-) delete mode 100644 test/test.html diff --git a/.travis.yml b/.travis.yml index da2f588..9308537 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ node_js: - "6" - "5" - "4" + - "0.10" # https://github.com/greenkeeperio/greenkeeper-lockfile before_install: - npm install -g npm@5 diff --git a/README.md b/README.md index e78db27..db63d00 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ulog v1.1.0 +# ulog v2.0.0-beta.1 ### Microscopically small universal logging library [![npm](https://img.shields.io/npm/v/ulog.svg)](https://npmjs.com/package/ulog) @@ -19,8 +19,8 @@ Even with these extra features, ulog is still **very** small, weighing in just over 1 kB minified and gzipped. ## Download -* [ulog.umd.js](https://cdn.rawgit.com/download/ulog/1.1.0/ulog.umd.js) (~3kB, source) -* [ulog.min.js](https://cdn.rawgit.com/download/ulog/1.1.0/ulog.min.js) (~2kB, minified) +* [ulog.umd.js](https://cdn.rawgit.com/download/ulog/2.0.0-beta.1/ulog.umd.js) (~3kB, source) +* [ulog.min.js](https://cdn.rawgit.com/download/ulog/2.0.0-beta.1/ulog.min.js) (~2kB, minified) ## Install ```sh @@ -55,7 +55,7 @@ define(['ulog'], function(ulog){ ### Script tag ```html - + ``` ## Logging methods diff --git a/browser.js b/browser.js index 2a63fc8..141954d 100644 --- a/browser.js +++ b/browser.js @@ -1,4 +1,5 @@ var log = require('./ulog') + var qs = location.search.substring(1), args = qs && qs.split('&'), lvl, dbg, i, m @@ -13,9 +14,9 @@ for (i=0; m=args && args[i] && args[i].split('='); i++) { m[0] == 'debug' ? dbg = m[1] : 0 } -log.con = function(){return window.console && console} +log.con = function(){return window.console} dbg && log.enable(dbg) -var ulog = log() +log() log.level = lvl || log.WARN -module.exports = ulog +module.exports = log diff --git a/build/build-umd.js b/build/build-umd.js index 484eb73..ea714a5 100644 --- a/build/build-umd.js +++ b/build/build-umd.js @@ -4,19 +4,17 @@ var path = require('path') var root = path.resolve(process.cwd()) var commonScript = fs.readFileSync(path.resolve(root, 'ulog.js'), 'utf8') -commonScript = commonScript.replace('module.exports = log', '') +commonScript = commonScript.replace('module.exports = ulog', '') var browserScript = fs.readFileSync(path.resolve(root, 'browser.js'), 'utf8') browserScript = browserScript.replace("var log = require('./ulog')", '') -browserScript = browserScript.replace('module.exports = ulog', 'return ulog') +browserScript = browserScript.replace('module.exports = log', 'return log') var combinedScript = commonScript + browserScript var umdScript = "(function(u,m,d){\n" + - "\tif (typeof define == 'function' && define.amd) define(m,[],d)\n" + - "\telse if (typeof module == 'object' && module.exports) module.exports = d()\n" + - "\telse u[m] = d()\n" + + "\ttypeof define == 'function' && define.amd ? define(m,[],d) : (u[m] = d())\n" + "})(this, \'ulog\', function(){'use strict'\n" + combinedScript + '\n' + "}) // umd\n" diff --git a/microscopical.spec.js b/microscopical.spec.js index 0ec9b54..f606125 100644 --- a/microscopical.spec.js +++ b/microscopical.spec.js @@ -5,6 +5,6 @@ var node = typeof window != 'object' describe('ulog', function(){ it('is microscopically small (~2kB minified, ~1kB gzipped)', function(){ var stats = fs.statSync("ulog.min.js") - expect(stats.size).to.be.below(2000) + expect(stats.size).to.be.below(2500) }) }) diff --git a/node.js b/node.js index 4d2c6bd..453c91f 100644 --- a/node.js +++ b/node.js @@ -1,12 +1,11 @@ var log = require('./ulog') -var level, debug - -if (process.env.LOG ) {level = process.env.LOG} -if (process.env.DEBUG) {debug = process.env.DEBUG} +var lvl, dbg +if (process.env.LOG ) {lvl = process.env.LOG} +if (process.env.DEBUG) {dbg = process.env.DEBUG} var fd = process.env.DEBUG_FD && parseInt(process.env.DEBUG_FD, 10), - c = console; + c = console; if (typeof fd == 'number') { var util = require('util'), stream = createWritableStdioStream(fd), @@ -14,10 +13,20 @@ if (typeof fd == 'number') { c = {error:logger, warn:logger, info:logger, log:logger, trace:logger} } +log.extends.push(function(o,p){ + // makes Node behave like browsers + o.assert = function(){ + var a=[].slice.call(arguments), ok=a.shift() + if (!ok) {o.error.apply(o, a)} + } +}) + log.con = function(){return c} -if (debug) {log.enable(debug)} -module.exports = log() -log.level = level || log.INFO +if (dbg) {log.enable(dbg)} +log() +log.level = lvl || log.INFO + +module.exports = log // SEE https://github.com/visionmedia/debug/blob/2.2.0/node.js#L144 function createWritableStdioStream (fd) { diff --git a/package.json b/package.json index 6620461..293bb2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ulog", - "version": "1.1.0", + "version": "2.0.0-beta.1", "description": "Microscopically small universal logging library", "main": "node.js", "browser": "browser.js", @@ -12,7 +12,8 @@ "build": "cross-env NODE_ENV=production npm run -S build-all", "release": "npm run -S clean && npm run -S build && npm run -S test", "test": "mocha *.spec.js", - "dev": "webpack-dev-server --output-path /test --output-filename ulog.spec.js \"mocha-loader!./ulog.spec.js\" --content-base test --port 8888" + "dev": "webpack-dev-server --output-path /test --output-filename ulog.spec.js \"mocha-loader!./ulog.spec.js\" --content-base test --port 8888", + "debug": "webpack-dev-server --output-path /test --output-filename debug.spec.js \"mocha-loader!./debug.spec.js\" --content-base test --port 8888" }, "keywords": [ "universal-components", @@ -35,7 +36,7 @@ "email": "StijnDeWitt@hotmail.com", "url": "http://StijnDeWitt.com" }, - "copyright": "Copyright 2016 by [Stijn de Witt](http://StijnDeWitt.com). Some rights reserved.", + "copyright": "Copyright 2017 by [Stijn de Witt](http://StijnDeWitt.com). Some rights reserved.", "license": "CC-BY-4.0", "licenseUrl": "https://creativecommons.org/licenses/by/4.0/", "repository": { diff --git a/test/test.html b/test/test.html deleted file mode 100644 index 0980f09..0000000 --- a/test/test.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - Test ulog - - - - Test ulog -

Open the developer console

- - - \ No newline at end of file diff --git a/ulog.js b/ulog.js index 80417f3..3a4da1f 100644 --- a/ulog.js +++ b/ulog.js @@ -3,78 +3,70 @@ // © 2017 by Stijn de Witt, some rights reserved // License: CC-BY-4.0 -function log(name){ - return name - ? (mods[name] ? mods[name] : (mods[name] = enhance({name:name}, log))) - : (log.debug ? log : enhance(log)) +function log(n){ + return !n ? extend(log) : mods[n] || (mods[n] = extend(create(n), log)) } -log.ulog = {version:'1.1.0'} +log.formats = [] +log.extends = [] -log.enable = function(str) { - var i, split = (str || '').split(/[\s,]+/); +log.enable = function(s) { + var i, split = (s || '').split(/[\s,]+/); for (i=0; i 1 && names[a[0]] ? a.shift() : 'debug'; for (var i=0; i= 0 && lvl <= 6) {level = lvl} + if (lvl >= 0 && lvl <= 6) l = lvl } patch(o) - if (!parent) {for (mod in mods) {patch(mods[mod])}} + if (!p) {for (mod in mods) {patch(mods[mod])}} } }) - patch(o, parent) - o.dir = bnd('dir') || nop - o.table = bnd('table') || nop - o.time = bnd('time') || nop - o.timeEnd = bnd('timeEnd') || nop - // makes Node behave like browsers - o.assert = typeof window == 'object' && bnd('assert') || function(){ - var a=[].concat.apply([], arguments), ok=a.shift() - if (!ok) {o.error.apply(o, a)} - } + patch(o) + for (var i=0; i=0&&r<=6&&(i=r)}if(t(e),!n)for(mod in s)t(s[mod])}}),t(e),e.dir=o("dir")||r,e.table=o("table")||r,e.time=o("time")||r,e.timeEnd=o("timeEnd")||r,e.assert="object"==typeof window&&o("assert")||function(){var n=[].concat.apply([],arguments);n.shift()||e.error.apply(e,n)},e}function t(n){for(var t,i=Math.max(n.name&&e.enabled(n.name)&&n.DEBUG||n.level,n.level),l=0;t=c[l];l++)n[t]=i<=l?r:o(t)||"function"==typeof print&&print||r}function o(n,t){return(t=e.con())&&(t[n]||t.log).bind(t)}function r(){}e.ulog={version:"1.1.0"},e.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=0&&r<=6&&(o=r)}if(patch(e),!n)for(mod in mods)patch(mods[mod])}}),patch(e);for(var r=0;r 1 && names[a[0]] ? a.shift() : 'debug'; for (var i=0; i= 0 && lvl <= 6) {level = lvl} + if (lvl >= 0 && lvl <= 6) l = lvl } patch(o) - if (!parent) {for (mod in mods) {patch(mods[mod])}} + if (!p) {for (mod in mods) {patch(mods[mod])}} } }) - patch(o, parent) - o.dir = bnd('dir') || nop - o.table = bnd('table') || nop - o.time = bnd('time') || nop - o.timeEnd = bnd('timeEnd') || nop - // makes Node behave like browsers - o.assert = typeof window == 'object' && bnd('assert') || function(){ - var a=[].concat.apply([], arguments), ok=a.shift() - if (!ok) {o.error.apply(o, a)} - } + patch(o) + for (var i=0; i