Skip to content

Commit

Permalink
2.0.0-beta.1: API improvements
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Download committed Dec 27, 2017
1 parent aa0380a commit 355d8a9
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 527 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_js:
- "6"
- "5"
- "4"
- "0.10"
# https://github.com/greenkeeperio/greenkeeper-lockfile
before_install:
- npm install -g npm@5
Expand Down
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>v1.1.0</sup></sub>
# ulog <sub><sup>v2.0.0-beta.1</sup></sub>
### Microscopically small universal logging library

[![npm](https://img.shields.io/npm/v/ulog.svg)](https://npmjs.com/package/ulog)
Expand All @@ -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
Expand Down Expand Up @@ -55,7 +55,7 @@ define(['ulog'], function(ulog){

### Script tag
```html
<script src="https://cdn.rawgit.com/download/ulog/1.1.0/ulog.min.js"></script>
<script src="https://cdn.rawgit.com/download/ulog/2.0.0-beta.1/ulog.min.js"></script>
```

## Logging methods
Expand Down
7 changes: 4 additions & 3 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var log = require('./ulog')

var qs = location.search.substring(1),
args = qs && qs.split('&'),
lvl, dbg, i, m
Expand All @@ -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
8 changes: 3 additions & 5 deletions build/build-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion microscopical.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
25 changes: 17 additions & 8 deletions node.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
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),
logger = function(){stream.write(util.format.apply(this, arguments) + '\n')}
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) {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -35,7 +36,7 @@
"email": "[email protected]",
"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": {
Expand Down
16 changes: 0 additions & 16 deletions test/test.html

This file was deleted.

66 changes: 29 additions & 37 deletions ulog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<split.length; i++) {
if (split[i]) {
str = split[i].replace(/\*/g, '.*?')
if (str[0] === '-') skipMods.push(new RegExp('^' + str.substr(1) + '$'))
else dbgMods.push(new RegExp('^' + str + '$'))
s = split[i].replace(/\*/g, '.*?')
if (s[0] === '-') skipMods.push(new RegExp('^' + s.substr(1) + '$'))
else dbgMods.push(new RegExp('^' + s + '$'))
}
}
for (i in mods) patch(mods[i])
}

log.enabled = function(name) {
log.enabled = function(s) {
var i;
for (i=0; i<skipMods.length; i++)
if (skipMods[i].test(name)) return
if (skipMods[i].test(s)) return
for (i=0; i<dbgMods.length; i++)
if (dbgMods[i].test(name)) {return true}
if (dbgMods[i].test(s)) {return true}
}

log.disable = log.enable.bind(log, '')

var LVL = {ERROR:1, WARN:2, INFO:3, LOG:4, DEBUG:5, TRACE:6},
names = Object.keys(LVL).map(function(x){return x.toLowerCase()}),
names = {error:1, warn:2, info:3, log:4, verbose:4, debug:5, trace:6, silly:6, dir:0, table:0, time:0, timeEnd:0, assert:0}
mods = {}, dbgMods = [], skipMods = []

function enhance(o, parent, level) {
function create(n,r) {
eval("r = {'" + n + "': function() {var a = [].slice.call(arguments), m = a.length > 1 && names[a[0]] ? a.shift() : 'debug'; for (var i=0; i<log.formats.length; i++) log.formats[i](mods[n],m,a); return mods[n][m].apply(mods[n], a)}}[n]")
return r.name ? r : Object.defineProperty(r, 'name', {get:function(){return n}})
}

function extend(o,p,l) {
if (o.log) return
o.NONE = 0
o.ulog = {version:'2.0.0-beta.1'}
for (var key in LVL) {o[key] = LVL[key]}
Object.defineProperty(o, 'level', {
get: function(){return level !== undefined ? level : parent && parent.level},
get: function(){return l !== undefined ? l : p && p.level},
set: function(n) {
if ((n === undefined) && parent) {level = undefined}
if ((n === undefined) && p) l = undefined
else {
var lvl = n && (Number(n)!==Number(n) ? o[n.toUpperCase()] : Number(n))
if (lvl >= 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<log.extends.length; i++) log.extends[i](o,p)
return o
}

function patch(o) {
var lvl = Math.max(o.name && log.enabled(o.name) && o.DEBUG || o.level, o.level)
for (var i=0,name; name=names[i]; i++) {
o[name] = lvl <= i
? nop
: (
bnd(name) ||
(typeof print == 'function' && print) ||
nop
)
for (var n in names) {
o[n] = lvl < names[n] ? nop : bnd(n) || (typeof print == 'function' && print) || nop
}
}

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.

Loading

0 comments on commit 355d8a9

Please sign in to comment.