custom-log
Advanced tools
Comparing version 0.2.5 to 0.3.0
@@ -1,46 +0,92 @@ | ||
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
// Generated by CoffeeScript 2.5.1 | ||
(function () { | ||
// custom-log.coffee - A tiny console.log wrapper, written in Coffeescript. | ||
// MIT License | ||
// Copyright (c) 2015 Dennis Raymondo van der Sluis | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
"use strict"; | ||
var customLog, intoArray, | ||
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
slice = [].slice; | ||
intoArray = function(args) { | ||
var CUSTOM_LOG, | ||
customLog, | ||
forceObject, | ||
instanceOf, | ||
intoArray, | ||
hasProp = {}.hasOwnProperty; | ||
CUSTOM_LOG = 'custom-log: '; // taken from types.js | ||
intoArray = function (args) { | ||
if (args.length < 2) { | ||
if (typeof args[0] === 'string') { | ||
args = args.join('').replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ').split(' '); | ||
} else if ((typeof args[0] === 'object') && (args[0] instanceof Array)) { | ||
} else if (typeof args[0] === 'object' && args[0] instanceof Array) { | ||
args = args[0]; | ||
} | ||
} | ||
return args; | ||
}; | ||
customLog = function(init) { | ||
var CUSTOM_LOG, Log, enact, fn, level, log, logInstance, prefix, prefixMsg; | ||
CUSTOM_LOG = 'custom-log: '; | ||
Log = (function() { | ||
function Log(level, prefix) { | ||
this.assert = bind(this.assert, this); | ||
this.enable = bind(this.enable, this); | ||
this.disable = bind(this.disable, this); | ||
var prop, value; | ||
instanceOf = function (type, value) { | ||
return value instanceof type; | ||
}; | ||
forceObject = function (value) { | ||
if (typeof value === 'object' && value !== null && !instanceOf(Boolean, value) && !instanceOf(Number, value) && !instanceOf(Array, value) && !instanceOf(RegExp, value) && !instanceOf(Date, value)) { | ||
return value; | ||
} else { | ||
return {}; | ||
} | ||
}; // prefixes is primarily for creating log.anyPrefix methods | ||
customLog = function (prefixes, settings) { | ||
var Log, enact, level, log, logInstance, prefix, prefixMsg; | ||
if (typeof prefixes === 'string') { | ||
// prefixes as string is for a only single log function with prefix | ||
prefixMsg = prefixes; | ||
} | ||
prefixes = forceObject(prefixes); | ||
settings = forceObject(settings); | ||
Log = class Log { | ||
constructor(level, prefix) { | ||
var prop, ref, value; | ||
this.disable = this.disable.bind(this); | ||
this.enable = this.enable.bind(this); | ||
this.enabled = true; | ||
this.level = level || 'log'; | ||
this.prefix = prefix || ''; | ||
this.log = (function(_this) { | ||
return function() { | ||
var message, ref; | ||
if (_this.enabled) { | ||
message = arguments; | ||
if (_this.prefix) { | ||
message = (ref = [_this.prefix]).concat.apply(ref, arguments); | ||
} | ||
return console.log.apply(console, message); | ||
} | ||
}; | ||
})(this); | ||
for (prop in this) { | ||
value = this[prop]; | ||
if ((this.hasOwnProperty(prop)) && (prop !== 'log')) { | ||
this.log = (...args) => { | ||
var message; | ||
if (this.enabled) { | ||
prefix = typeof this.prefix === 'function' ? this.prefix(...args) : this.prefix; | ||
message = this.prefix ? [prefix].concat(...args) : args; | ||
return console.log.apply(console, message); | ||
} | ||
}; | ||
ref = this; | ||
for (prop in ref) { | ||
if (!hasProp.call(ref, prop)) continue; | ||
value = ref[prop]; | ||
if (prop !== 'log') { | ||
this.log[prop] = value; | ||
@@ -51,48 +97,32 @@ } | ||
Log.prototype.disable = function() { | ||
disable() { | ||
this.enabled = false; | ||
return console.log(CUSTOM_LOG + '.' + this.level + ' is disabled'); | ||
}; | ||
Log.prototype.enable = function() { | ||
if (!settings.silentDisable) { | ||
return console.log(CUSTOM_LOG + '.' + this.level + ' is disabled'); | ||
} | ||
} | ||
enable() { | ||
this.enabled = true; | ||
return console.log(CUSTOM_LOG + '.' + this.level + ' is enabled'); | ||
}; | ||
Log.prototype.assert = function(predicate, description) { | ||
if (description == null) { | ||
description = ''; | ||
if (!settings.silentEnable) { | ||
return console.log(CUSTOM_LOG + '.' + this.level + ' is enabled'); | ||
} | ||
if (typeof predicate === 'string') { | ||
description = predicate; | ||
} | ||
if (description) { | ||
description = '(' + description + ') == '; | ||
} | ||
if (typeof predicate === 'string') { | ||
predicate = eval(predicate); | ||
} | ||
if (predicate) { | ||
predicate = 'TRUE'; | ||
} else { | ||
predicate = 'FALSE'; | ||
} | ||
return this.log('\n\t' + customLog.assertMessage + description + predicate + '\n'); | ||
}; | ||
} | ||
return Log; | ||
}; // end of Log | ||
// create a default log right away | ||
})(); | ||
if (typeof init === 'string') { | ||
prefixMsg = init; | ||
} | ||
logInstance = new Log('log', prefixMsg); | ||
log = logInstance.log; | ||
enact = function() { | ||
var i, len, level, levels, method, results; | ||
method = arguments[0], levels = 2 <= arguments.length ? slice.call(arguments, 1) : []; | ||
log = logInstance.log; // abstract function for enable and disable | ||
enact = function (method, ...levels) { | ||
var i, len, level, results; | ||
levels = intoArray(levels); | ||
results = []; | ||
for (i = 0, len = levels.length; i < len; i++) { | ||
level = levels[i]; | ||
if (level === 'log') { | ||
@@ -106,33 +136,36 @@ results.push(logInstance[method]()); | ||
} | ||
return results; | ||
}; | ||
log.enable = function() { | ||
return enact.apply(null, ['enable'].concat(slice.call(arguments))); | ||
log.enable = function (...args) { | ||
return enact('enable', ...args); | ||
}; | ||
log.disable = function() { | ||
return enact.apply(null, ['disable'].concat(slice.call(arguments))); | ||
}; | ||
if (typeof init === 'object') { | ||
fn = function(level, prefix) { | ||
log.disable = function (...args) { | ||
return enact('disable', ...args); | ||
}; // create log levels/instances from prefixes object | ||
for (level in prefixes) { | ||
prefix = prefixes[level]; | ||
(function (level, prefix) { | ||
switch (level) { | ||
// allow the default log to have a prefix | ||
case 'log': | ||
return logInstance.prefix = prefix; | ||
case 'assert': | ||
return customLog.assertMessage = prefix; | ||
default: | ||
return log[level] = new Log(level, prefix).log; | ||
} | ||
}; | ||
for (level in init) { | ||
prefix = init[level]; | ||
fn(level, prefix); | ||
} | ||
})(level, prefix); | ||
} | ||
return log; | ||
}; | ||
}; // end of customLog | ||
customLog.assertMessage = 'Assert: '; | ||
if ((typeof define !== "undefined" && define !== null) && ('function' === typeof define) && define.amd) { | ||
define('customLog', [], function() { | ||
if (typeof define !== "undefined" && define !== null && 'function' === typeof define && define.amd) { | ||
define('customLog', [], function () { | ||
return customLog; | ||
@@ -145,3 +178,2 @@ }); | ||
} | ||
}).call(this); | ||
}).call(this); |
@@ -1,1 +0,1 @@ | ||
(function(){"use strict";var customLog,intoArray,bind=function(e,t){return function(){return e.apply(t,arguments)}},slice=[].slice;intoArray=function(e){return e.length<2&&("string"==typeof e[0]?e=e.join("").replace(/^\s+|\s+$/g,"").replace(/\s+/g," ").split(" "):"object"==typeof e[0]&&e[0]instanceof Array&&(e=e[0])),e},customLog=function(init){var CUSTOM_LOG,Log,enact,fn,level,log,logInstance,prefix,prefixMsg;if(CUSTOM_LOG="custom-log: ",Log=function(){function Log(e,t){this.assert=bind(this.assert,this),this.enable=bind(this.enable,this),this.disable=bind(this.disable,this);var n,i;this.enabled=!0,this.level=e||"log",this.prefix=t||"",this.log=function(e){return function(){var t,n;return e.enabled?(t=arguments,e.prefix&&(t=(n=[e.prefix]).concat.apply(n,arguments)),console.log.apply(console,t)):void 0}}(this);for(n in this)i=this[n],this.hasOwnProperty(n)&&"log"!==n&&(this.log[n]=i)}return Log.prototype.disable=function(){return this.enabled=!1,console.log(CUSTOM_LOG+"."+this.level+" is disabled")},Log.prototype.enable=function(){return this.enabled=!0,console.log(CUSTOM_LOG+"."+this.level+" is enabled")},Log.prototype.assert=function(predicate,description){return null==description&&(description=""),"string"==typeof predicate&&(description=predicate),description&&(description="("+description+") == "),"string"==typeof predicate&&(predicate=eval(predicate)),predicate=predicate?"TRUE":"FALSE",this.log("\n "+customLog.assertMessage+description+predicate+"\n")},Log}(),"string"==typeof init&&(prefixMsg=init),logInstance=new Log("log",prefixMsg),log=logInstance.log,enact=function(){var e,t,n,i,o,s;for(o=arguments[0],i=2<=arguments.length?slice.call(arguments,1):[],i=intoArray(i),s=[],e=0,t=i.length;t>e;e++)n=i[e],s.push("log"===n?logInstance[o]():null!=log[n]?log[n][o]():void 0);return s},log.enable=function(){return enact.apply(null,["enable"].concat(slice.call(arguments)))},log.disable=function(){return enact.apply(null,["disable"].concat(slice.call(arguments)))},"object"==typeof init){fn=function(e,t){switch(e){case"log":return logInstance.prefix=t;case"assert":return customLog.assertMessage=t;default:return log[e]=new Log(e,t).log}};for(level in init)prefix=init[level],fn(level,prefix)}return log},customLog.assertMessage="Assert: ","undefined"!=typeof define&&null!==define&&"function"==typeof define&&define.amd?define("customLog",[],function(){return customLog}):"undefined"!=typeof module?module.exports=customLog:"undefined"!=typeof window&&(window.customLog=customLog)}).call(this); | ||
(function(){"use strict";var e,n,t,i,o={}.hasOwnProperty;i=function(e){return e.length<2&&("string"==typeof e[0]?e=e.join("").replace(/^\s+|\s+$/g,"").replace(/\s+/g," ").split(" "):"object"==typeof e[0]&&e[0]instanceof Array&&(e=e[0])),e},t=function(e,n){return n instanceof e},n=function(e){return"object"!=typeof e||null===e||t(Boolean,e)||t(Number,e)||t(Array,e)||t(RegExp,e)||t(Date,e)?{}:e},e=function(e,t){var l,s,r,f,u,a;for(r in"string"==typeof e&&(a=e),e=n(e),t=n(t),u=new(l=class{constructor(e,n){var t,i;for(t in this.disable=this.disable.bind(this),this.enable=this.enable.bind(this),this.enabled=!0,this.level=e||"log",this.prefix=n||"",this.log=(...e)=>{var t;if(this.enabled)return n="function"==typeof this.prefix?this.prefix(...e):this.prefix,t=this.prefix?[n].concat(...e):e,console.log.apply(console,t)},this,this)o.call(this,t)&&(i=this[t],"log"!==t&&(this.log[t]=i))}disable(){if(this.enabled=!1,!t.silentDisable)return console.log("custom-log: ."+this.level+" is disabled")}enable(){if(this.enabled=!0,!t.silentEnable)return console.log("custom-log: ."+this.level+" is enabled")}})("log",a),f=u.log,s=function(e,...n){var t,o,l,s;for(s=[],t=0,o=(n=i(n)).length;t<o;t++)"log"===(l=n[t])?s.push(u[e]()):null!=f[l]?s.push(f[l][e]()):s.push(void 0);return s},f.enable=function(...e){return s("enable",...e)},f.disable=function(...e){return s("disable",...e)},e)!function(e,n){switch(e){case"log":return u.prefix=n;default:f[e]=new l(e,n).log}}(r,e[r]);return f},"undefined"!=typeof define&&null!==define&&"function"==typeof define&&define.amd?define("customLog",[],(function(){return e})):"undefined"!=typeof module?module.exports=e:"undefined"!=typeof window&&(window.customLog=e)}).call(this); |
{ | ||
"name": "custom-log", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"description": "A tiny flexible logger", | ||
"main": "custom-log.min.js", | ||
"scripts": { | ||
"uglify": "uglifyjs custom-log.js -c -m -o custom-log.min.js", | ||
"build": "coffee -c custom-log.coffee && npm run uglify", | ||
"terser": "terser custom-log.js -c -m -o custom-log.min.js", | ||
"build": "coffee -c --transpile custom-log.coffee && npm run terser", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "latest", | ||
"@babel/preset-env": "latest" | ||
}, | ||
"repository": { | ||
@@ -12,0 +16,0 @@ "type": "git", |
@@ -31,7 +31,7 @@ custom-log | ||
// customLog is global when loaded in browser, for node use: | ||
var customLog= require( 'custom-log' ); | ||
const customLog= require( 'custom-log' ); | ||
// create a single log function without prefix: | ||
var log= customLog(); | ||
const log= customLog(); | ||
@@ -41,4 +41,11 @@ log( 'hey!' ); | ||
// or create a log function with multiple custom log functions attached to it: | ||
var log= customLog({ | ||
// create a log function with multiple custom log functions attached to it | ||
// you can silently enable/disable by adding settings if needed: | ||
const settings = { | ||
silentDisable : false // default | ||
silentEnable : false // default | ||
}; | ||
const log= customLog({ | ||
info : 'INFO: ', | ||
@@ -49,4 +56,5 @@ warning : 'WARNING: ', | ||
dal : 'DAL: ' | ||
// whateverNameYouMayNeed: 'whatever prefix message.. ' | ||
}); | ||
// use the result of a function as prefix for logs | ||
time : () => new Date() | ||
}, settings ); | ||
@@ -82,2 +90,10 @@ log( 'hello' ); | ||
0.3.0 | ||
- removes log.assert | ||
- adds dynamic function prefix | ||
- adds 'silentEnable' and 'silentDisable' initialization modes | ||
--- | ||
0.2.3 | ||
@@ -115,2 +131,2 @@ | ||
MIT | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15798
142
128
2