diff --git a/README.md b/README.md index 827193f..c7d8bb5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# screenlog.js [](http://badge.fury.io/js/screenlog) +# screenlogx (forked from https://github.com/chinchang/screenlog.js) _Bring console.log, on the screen_ @@ -36,6 +36,8 @@ Initializes the screen logger. It creates a customizable panel on the screen. - `fontSize` - Font size of logs. Default is `1em`(Your browser's default). - `bgColor` - Background color of the log panel. Default is `black`. - `releaseConsole` - By default console.log is overridden to log on screen. You can avoid this behaviour by setting `releaseConsole` to `true` and instead use `screenLog.log()` API. Default is `false`. +- `minLogLevel` - minimum log level that to be displayed on the screen. Can beL `log`, `info`, `warn`, `error`. Default is `warn` +- `getVisualArgs` - function to allow visual log param customisation: function(logLevel, args) { ... return newArguments; } ### screenLog.[log, warn, error, info](obj1 [, obj2, ..., objN]) diff --git a/dist/screenlog.min.js b/dist/screenlog.min.js deleted file mode 100644 index e5ded37..0000000 --- a/dist/screenlog.min.js +++ /dev/null @@ -1,5 +0,0 @@ -/*! screenlog - v0.3.0 - 2019-01-25 -* https://github.com/chinchang/screenlog.js -* Copyright (c) 2019 Kushagra Gour; Licensed */ - -!function(){function a(a,b){var c=document.createElement(a);return c.style.cssText=b,c}function b(){var b=a("div","z-index:2147483647;font-family:Helvetica,Arial,sans-serif;font-size:"+_options.fontSize+";padding:5px;text-align:left;opacity:0.8;position:fixed;right:0;top:0;min-width:200px;max-height:50vh;overflow:auto;background:"+_options.bgColor+";"+_options.css);return b}function c(b){return function(){var c=a("div","line-height:1.7em;min-height:1.7em;background:"+(o.children.length%2?"rgba(255,255,255,0.1)":"")+";color:"+b),d=[].slice.call(arguments).reduce(function(a,b){return a+" "+("object"==typeof b?JSON.stringify(b):b)},"");c.textContent=d,o.appendChild(c),_options.autoScroll&&(o.scrollTop=o.scrollHeight-o.clientHeight)}}function d(){o.innerHTML=""}function e(){return c(_options.logColor).apply(null,arguments)}function f(){return c(_options.infoColor).apply(null,arguments)}function g(){return c(_options.warnColor).apply(null,arguments)}function h(){return c(_options.errorColor).apply(null,arguments)}function i(a){for(var b in a)a.hasOwnProperty(b)&&_options.hasOwnProperty(b)&&(_options[b]=a[b])}function j(a){p||(p=!0,a&&i(a),o=b(),document.body.appendChild(o),_options.freeConsole||(q.log=console.log,q.clear=console.clear,q.info=console.info,q.warn=console.warn,q.error=console.error,console.log=n(e,"log"),console.clear=n(d,"clear"),console.info=n(f,"info"),console.warn=n(g,"warn"),console.error=n(h,"error")))}function k(){p=!1,console.log=q.log,console.clear=q.clear,console.info=q.info,console.warn=q.warn,console.error=q.error,o.remove()}function l(){if(!p)throw"You need to call `screenLog.init()` first."}function m(a){return function(){return l(),a.apply(this,arguments)}}function n(a,b){return function(){a.apply(this,arguments),"function"==typeof q[b]&&q[b].apply(console,arguments)}}var o,p=!1,q={};_options={bgColor:"black",logColor:"lightgreen",infoColor:"blue",warnColor:"orange",errorColor:"red",fontSize:"1em",freeConsole:!1,css:"",autoScroll:!0},window.screenLog={init:j,log:n(m(e),"log"),clear:n(m(d),"clear"),info:n(m(d),"info"),warn:n(m(g),"warn"),error:n(m(h),"error"),destroy:m(k)}}(); \ No newline at end of file diff --git a/screenlog.js b/dist/screenlogx.js similarity index 78% rename from screenlog.js rename to dist/screenlogx.js index b88ab9c..d609b65 100644 --- a/screenlog.js +++ b/dist/screenlogx.js @@ -1,17 +1,32 @@ +/*! screenlogx - v0.3.2 - 2022-07-14 +* https://github.com/gabriel-deliu/screenlog.js +* Copyright (c) 2022 Kushagra Gour; Licensed */ + (function() { var logEl, isInitialized = false, _console = {}; // backup console obj to contain references of overridden fns. - _options = { + var _options = { bgColor: "black", logColor: "lightgreen", + debugColor: "lightblue", infoColor: "blue", warnColor: "orange", errorColor: "red", fontSize: "1em", freeConsole: false, css: "", - autoScroll: true + autoScroll: true, + getVisualArgs: function(logLevel, args) { return [Date.now(), logLevel].concat(Array.prototype.slice.call(args)); }, + minLogLevel: "info", + filterPattern: /^.*$/ + }; + var logLevel = { + log: 1, + debug: 2, + info: 3, + warn: 4, + error: 5 }; function createElement(tag, css) { @@ -42,6 +57,7 @@ ";color:" + color ); // zebra lines + var val = [].slice.call(arguments).reduce(function(prev, arg) { return ( prev + " " + (typeof arg === "object" ? JSON.stringify(arg) : arg) @@ -66,6 +82,10 @@ return genericLogger(_options.logColor).apply(null, arguments); } + function debug() { + return genericLogger(_options.debugColor).apply(null, arguments); + } + function info() { return genericLogger(_options.infoColor).apply(null, arguments); } @@ -102,11 +122,13 @@ if (!_options.freeConsole) { // Backup actual fns to keep it working together _console.log = console.log; + _console.debug = console.debug; _console.clear = console.clear; _console.info = console.info; _console.warn = console.warn; _console.error = console.error; console.log = originalFnCallDecorator(log, "log"); + console.debug = originalFnCallDecorator(debug, "debug"); console.clear = originalFnCallDecorator(clear, "clear"); console.info = originalFnCallDecorator(info, "info"); console.warn = originalFnCallDecorator(warn, "warn"); @@ -117,6 +139,7 @@ function destroy() { isInitialized = false; console.log = _console.log; + console.debug = _console.debug; console.clear = _console.clear; console.info = _console.info; console.warn = _console.warn; @@ -154,7 +177,25 @@ */ function originalFnCallDecorator(fn, fnName) { return function() { - fn.apply(this, arguments); + + var isMatch = false; + + for(var key in arguments) { + if(typeof arguments[key] === "string") { + if(!_options.filterPattern || _options.filterPattern.test(arguments[key])) { + isMatch = true; + break; + } + } + } + + if(!isMatch) { + return; + } + + if(logLevel[_options.minLogLevel] && logLevel[fnName] >= logLevel[_options.minLogLevel]) { + fn.apply(this, _options.getVisualArgs(fnName, arguments)); + } if (typeof _console[fnName] === "function") { _console[fnName].apply(console, arguments); } @@ -165,6 +206,7 @@ window.screenLog = { init: init, log: originalFnCallDecorator(checkInitDecorator(log), "log"), + debug: originalFnCallDecorator(checkInitDecorator(debug), "debug"), clear: originalFnCallDecorator(checkInitDecorator(clear), "clear"), info: originalFnCallDecorator(checkInitDecorator(clear), "info"), warn: originalFnCallDecorator(checkInitDecorator(warn), "warn"), diff --git a/dist/screenlogx.min.js b/dist/screenlogx.min.js new file mode 100644 index 0000000..466db66 --- /dev/null +++ b/dist/screenlogx.min.js @@ -0,0 +1,5 @@ +/*! screenlogx - v0.3.2 - 2022-07-14 +* https://github.com/gabriel-deliu/screenlog.js +* Copyright (c) 2022 Kushagra Gour; Licensed */ + +!function(){function a(a,b){var c=document.createElement(a);return c.style.cssText=b,c}function b(){var b=a("div","z-index:2147483647;font-family:Helvetica,Arial,sans-serif;font-size:"+s.fontSize+";padding:5px;text-align:left;opacity:0.8;position:fixed;right:0;top:0;min-width:200px;max-height:50vh;overflow:auto;background:"+s.bgColor+";"+s.css);return b}function c(b){return function(){var c=a("div","line-height:1.7em;min-height:1.7em;background:"+(p.children.length%2?"rgba(255,255,255,0.1)":"")+";color:"+b),d=[].slice.call(arguments).reduce(function(a,b){return a+" "+("object"==typeof b?JSON.stringify(b):b)},"");c.textContent=d,p.appendChild(c),s.autoScroll&&(p.scrollTop=p.scrollHeight-p.clientHeight)}}function d(){p.innerHTML=""}function e(){return c(s.logColor).apply(null,arguments)}function f(){return c(s.debugColor).apply(null,arguments)}function g(){return c(s.infoColor).apply(null,arguments)}function h(){return c(s.warnColor).apply(null,arguments)}function i(){return c(s.errorColor).apply(null,arguments)}function j(a){for(var b in a)a.hasOwnProperty(b)&&s.hasOwnProperty(b)&&(s[b]=a[b])}function k(a){q||(q=!0,a&&j(a),p=b(),document.body.appendChild(p),s.freeConsole||(r.log=console.log,r.debug=console.debug,r.clear=console.clear,r.info=console.info,r.warn=console.warn,r.error=console.error,console.log=o(e,"log"),console.debug=o(f,"debug"),console.clear=o(d,"clear"),console.info=o(g,"info"),console.warn=o(h,"warn"),console.error=o(i,"error")))}function l(){q=!1,console.log=r.log,console.debug=r.debug,console.clear=r.clear,console.info=r.info,console.warn=r.warn,console.error=r.error,p.remove()}function m(){if(!q)throw"You need to call `screenLog.init()` first."}function n(a){return function(){return m(),a.apply(this,arguments)}}function o(a,b){return function(){var c=!1;for(var d in arguments)if("string"==typeof arguments[d]&&(!s.filterPattern||s.filterPattern.test(arguments[d]))){c=!0;break}c&&(t[s.minLogLevel]&&t[b]>=t[s.minLogLevel]&&a.apply(this,s.getVisualArgs(b,arguments)),"function"==typeof r[b]&&r[b].apply(console,arguments))}}var p,q=!1,r={},s={bgColor:"black",logColor:"lightgreen",debugColor:"lightblue",infoColor:"blue",warnColor:"orange",errorColor:"red",fontSize:"1em",freeConsole:!1,css:"",autoScroll:!0,getVisualArgs:function(a,b){return[Date.now(),a].concat(Array.prototype.slice.call(b))},minLogLevel:"info",filterPattern:/^.*$/},t={log:1,debug:2,info:3,warn:4,error:5};window.screenLog={init:k,log:o(n(e),"log"),debug:o(n(f),"debug"),clear:o(n(d),"clear"),info:o(n(d),"info"),warn:o(n(h),"warn"),error:o(n(i),"error"),destroy:n(l)}}(); \ No newline at end of file diff --git a/example.html b/example.html index c9083ff..46e467a 100644 --- a/example.html +++ b/example.html @@ -5,10 +5,10 @@