UNPKG

atma-logger

Version:
2 lines 14.9 kB
!function(n,e){"use strict";var t="undefined"==typeof window?global:window,r=e(t);t.logger=r}(this,function(n){"use strict";function e(){return this}var t;t=require("atma-formatter");var r,o,u,i,l,c=100,a=75,f=50,s=25,g=0,h=-25,v={formatMessage:!0,logCaller:!0,logDate:!1,logMeta:null,color:"ascii",handleExceptions:!1},d=f;!function(){r=function(n){return Object.prototype.toString.call(n).replace("[object ","").replace("]","")},o=function(n){"function"==typeof n&&(n=n.prototype);for(var e,t,r=1,o=arguments.length;o>r;r++){e="function"==typeof arguments[r]?arguments[r].prototype:arguments[r];for(t in e)n[t]=e[t]}return n},u=function(n,e){if(null==e)return n;for(var t in e)n[t]=e[t];return n},i=function(){function n(t){if(null==t)return t;var o,u=r(t);switch(u){case"String":case"Number":case"Date":case"RegExp":case"Function":case"Error":case"Boolean":return t;case"Array":case"Arguments":o=[];for(var i=0,l=t.length;l>i;i++)o[i]=n(t[i]);return o;case"Object":case"global":if(-1!==e.indexOf(t))return"<circular>";e.push(t),o={};for(var c in t)o[c]=n(t[c]);return o;default:return console.warn("obj_dimissCircular: Unknown type",u),t}}var e;return function(t){return"object"==typeof t&&null!=t&&(e=[],t=n(t),e=null),t}}(),l=function(){function n(n){for(var e="",t=0;n>t;t++)e+=" ";return e}return function e(t,o,u){null==u&&(u=0);var i=["",0];if("undefined"==typeof t)return i[1]="<undefined>",o(i,"undefined"),i[1];if(null==t)return i[1]="<null>",o(i,"null"),i[1];var l=r(t);switch(l){case"Array":case"Arguments":for(var c,a="[",f=t,s=0,g=f.length;g>s;s++)c=f[s],"["===a&&(a+="\n"),a+=n(u+1)+e(c,o,u+1)+(g-1>s?",":"")+"\n";return a+=n(u)+"]";case"RegExp":return i[1]=t.toString(),o(i,"regexp"),i[1];case"Date":return i[1]=t.toISOString(),o(i,"date"),i[1];case"String":case"Number":case"Boolean":return i[1]=t,o(i,l.toLowerCase()),i[1];case"Function":return i[1]="<function>",o(i,l.toLowerCase()),i[1];case"Error":return t.toString().red;case"Object":case"global":var h,a="{",v=Object.keys(t);for(s=0,g=v.length;g>s;s++)h=v[s],"{"===a&&(a+="\n"),i[0]=h,i[1]=t[h],o(i),a+=n(u+1)+i[0]+": "+e(t[h],o,u+1)+(g-1>s?",":"")+"\n";return a+=n(u)+"}"}}}()}();var p;!function(){function n(n){")"===n[n.length-1]&&(n=n.substr(n.indexOf("(")+1,n.lastIndexOf(")")-1));var r=/^(.+):(\d+):(\d)/.exec(n);if(null==r)return null;var o=t.path=r[1];return t.file=o.substring(o.search(/[\/\\][^\/\\]+$/)+1),t.line=r[2],null==e&&(e=t.file),t}var e,t={path:null,file:null,line:null};p=function(t,r){for(var o,u=(new Error).stack.split("\n").splice(1),i=u.length,l=-1;++l<i&&(o=n(u[l]),null==o||null==e||o.file===e););return null==o||l===i?"":t.replace("P",o.path).replace("F",o.file).replace("L",o.line)}}();var b;!function(){b=function(n){return"string"!=typeof n&&(n="dd-MM hh:mm:ss"),t(new Date,n)}}();var y,m;!function(){m=function(n,e){if(v.formatMessage===!1)return v.logMeta&&n.unshift(v.logMeta(n)),n;var t=y(n);return null!=e._name&&(t=e._name.color+" "+t),v.logCaller!==!1&&(t+=p(" (F:L)",5)),"function"==typeof v.logMeta&&(t=v.logMeta(n)+" "+t),v.logDate!==!1&&(t=b(v.logDate)+" "+t),t},y=function(n){for(var e,t,r="",o=/%s|%d/,u=n.length>1&&"string"==typeof n[0]&&o.test(n[0]),i=0,l=n.length;l>i;i++)t=n[i],t instanceof String&&(t=String(t)),e="string"==typeof t?t:E.formatJSON(t),i>0&&u&&o.test(r)?r=r.replace(o,e):(""!==r&&(r+=" "),r+=e);return r}}();var w;!function(){function n(n){var t=k.error(n).getTransport();if(null!=t){if(t.flush)return void t.flush(e);if(t.flushAsync)return void t.flushAsync(e)}e()}function e(){process.exit(1)}w=function(e){process.removeListener("uncaughtException",n),e!==!1&&process.on("uncaughtException",n)}}();var _;!function(){_=function(n,e){switch(n){case"level":d=e;break;case"levels":for(var t in e)S(t,e[t]);break;case"transport":O.define(e);break;case"handleExceptions":w(e);break;case"color":E.define(e);case"logCaller":case"logDate":case"formatMessage":case"meta":v[n]=e;break;default:console.error("Logger: unknown configuration",n)}}}();var S,x;!function(){function n(e){var t=e.lastIndexOf(".");return-1===t?o:(e=e.substring(0,t),r.hasOwnProperty(e)&&r[e].strict!==!0?r[e]:n(e))}function e(n,e){var o=/\.\*$/.test(n);o&&(n=n.slice(0,-2)),r[n]={level:e,strict:o===!1},o&&t(n,e,o)}function t(n,e){var t,o=n.length;for(t in r)t.length<=o+1||0===t.indexOf(n+".")&&(r[t]={level:e})}S=function(n,t){e(n,t)},x=function(e){var t=r[e];return t===o?null:(null==t&&(t=r[e]=n(e)),t.level)};var r={},o={level:null}}();var E;!function(){function n(n){return Object.keys(n.value).forEach(function(e){try{Object.defineProperty(String.prototype,e,{get:function(){var t=this,r=n.decorator;null!=r&&t.__wrapped!==!0&&(t=r(t));var o=n.start(e)+t+n.END;return null==r?o:(o=new String(o),o.__wrapped=!0,o)},enumerable:!1,configurable:!0})}catch(t){}}),Object.defineProperty(String.prototype,"color",{get:function(){return u(this,n)},enumerable:!1,configurable:!0}),function(e){return u(e,n)}}E={init:{ascii:function(){e=n(o)},html:function(){e=n(t)},none:function(){e=n(r)}},colorize:function(n){return e(n)},formatJSON:function(n){var e=n;return void 0!==e&&"object"==typeof e&&(e=i(n)),l(e,function(n,e){e&&c[e]&&(n[1]=String(n[1])[c[e]]),n[0]&&(n[0]=n[0].bold)})},define:function(n){return E.init[n]?E.init[n]():void console.error("Invalid Color Type ",n)}};var e,t,r={END:"",value:{red:"",green:"",yellow:"",blue:"",magenta:"",cyan:"",bold:"",italic:"",underline:"",inverse:""},start:function(n){return""}};!function(){t={END:"</span>",value:{red:"color:red",green:"color:green",yellow:"color:yellow",blue:"color:blue",magenta:"color:magenta",cyan:"color:cyan",bg_black:"background-color:black",bg_red:"background-color:red",bg_green:"background-color:green",bg_yellow:"background-color:yellow",bg_blue:"background-color:blue",bg_magenta:"background-color:magenta",bg_cyan:"background-color:cyan",bg_white:"background-color:white",bold:"font-weight:bold",italic:"font-style:italic",underline:"text-decoration:underline",inverse:"color:black;background:white"},start:function(n){var e='<span style="'+this.value[n]+'">';return e},decorator:function(e){return n(e)}};var n;!function(){function e(n){return t[n]}n=function(n){return n.replace(/[&"'<>\/]/g,e)};var t={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","/":"&#x2F;"}}()}();var o={type:"ascii",START:"[",END:"",value:{red:"31m",green:"32m",yellow:"33m",blue:"34m",magenta:"35m",cyan:"36m",white:"37m",black:"30m",bg_black:"40m",bg_red:"41m",bg_green:"42m",bg_yellow:"43m",bg_blue:"44m",bg_magenta:"45m",bg_cyan:"46m",bg_white:"47m",bold:"1m",italic:"3m",underline:"4m",inverse:"7m"},start:function(n){return this.START+this.value[n]}};e=n(o);var u,c={string:"yellow",number:"cyan","boolean":"cyan",regexp:"magenta",date:"magenta","function":"magenta","null":"blue",undefined:"blue"};!function(){function n(n,e,t,r){var o=n.substring(e,t);return r.decorator?r.decorator(o):o}function e(n,e){for(var t,r=1,o=n.length,u=e;o>u;u++)if(t=n.charCodeAt(u),60===t&&r++,62===t&&r--,0===r)return u;return-1}function t(n,e,t){for(var r,o="",u=n.length,i=-1;++i<u;)r=n[i],r.end<e||(o+=t.start(r.key));return o}function r(n){if(null==n.rgx_search){var e="(";for(var t in n.value)e+="("===e?t:"|"+t;e+=")<",n.rgx_search=new RegExp(e,"g")}return n}u=function(o,i){r(i),i.rgx_search.lastIndex=0;for(var l,c,a,f,s="ascii"===i.type,g=s&&[]||null,h="",v=0;;){if(l=i.rgx_search.exec(o),null==l)break;if(c=l[1],null!=i.value[c]){var d,f,p=l.index,b=p+l[0].length;v!==p&&(h+=n(o,v,p,i)),a=e(o,b),v=a+1,-1!==a?(d=i.start(c),f=o.substring(b,a),f=u(f,i),h+=d+f+i.END+(s?t(g,a,i):""),s&&g.push({end:a,key:c}),i.rgx_search.lastIndex=a+1):h+=n(o,p,a,i)}}return v<o.length&&(h+=n(o,v,o.length,i)),h}}()}();var k,O=function(){var n,e,r={};return function(){function n(n,e){var r=t(n);""!==r&&e(n,r)}function t(n){var e=n.buffer;if(0===e.length)return"";var t=n.newLine,r=e.join(t)+t;return e.length=0,r}function r(n,e,t){n&&n(t);var r=e.length,o=-1;if(0!==r){for(;++o<r;)e[o](t);e.length=0}}e={flushAsync:function(n,e,o,u){if(n.busy)return void(null!=u&&n.listeners.push(u));var i=t(n);return""===i?void(u&&u()):(n.busy=!0,void o(n,i,function(t){n.busy=!1,r(u,n.listeners),n.buffer.length>e&&n.flushAsync()}))},flush:function(e,t,r){return e.busy?void e.listeners.push(function(){n(e,t,r)}):(n(e,t),void(r&&r()))}}}(),r.http=function(){var n,t,r,o,u=0;!function(){function e(n,e,t,r,o){if(e=e.toUpperCase(),t=t||{},t["Content-Type"]||(t["Content-Type"]="text/plain"),t["Content-Length"]=Buffer.byteLength(r),"GET"===e){if(r=u(r),null==r)return void(o&&o("Encode failed"));n+=r,r=void 0}var a=c.parse(n);a.headers=t,a.method=e;var f="https:"===a.protocol?i:l,s="",g=f.request(a,function(n){n.setEncoding("utf-8"),n.on("data",function(n){s+=String(n)}).on("end",function(){o&&o(s,n)})});null!=r&&g.write(r),g.end()}function u(n){try{return encodeURIComponent(n)}catch(e){return k.error("Logger encode error",e),null}}o=function(o,u){e(n,t||"POST",r,o,u)};var i=require("https"),l=require("http"),c=require("url")}();var i;return function(){function n(n,e,t){o(e,t)}i={newLine:"\n",busy:!1,listeners:[],buffer:[],write:function(n){n=n.replace(/\n/g,"\\n"),this.buffer.push(n),this.buffer.length>u&&this.flushAsync()},flushAsync:function(t){e.flushAsync(this,u,n,t)}}}(),{write:function(n){i.write(n)},flushAsync:function(n){i.flushAsync(n)},cfg:function(e){null!=e.bufferSize&&(u=e.bufferSize),n=e.url||n,t=e.method||t,r=e.headers||r}}},r.std=function(){return{write:function(n){this.put("\n"+n)},put:function(n){process.stdout.write(n)},cfg:function(){},std:!0}}(),r.stream=function(){var n;return{write:function(e){n.write(e)},put:function(e){n.write(e)},cfg:function(e){var t=e.stream;if(null==t)throw Error("Transport Configuration: No `stream` property");if(null==t.write)throw Error("Transport Configuration: Invalid stream object");n=t}}}(),r.fs=function(){function n(n,e,t){l(n.path,e,t)}function r(n,e){i(n.path,e)}function o(n){try{g.appendFileSync(s.resolve(N,"logger-exceptions.txt"),y([n])+p)}catch(e){"ENOENT"===e.code&&(x(N),o(n))}}var u,i,l,c,a,f,s=require("path"),g=require("fs"),h="logs",d="txt",p=require("os").EOL,b=512e3,m=10,w=0,_=!1;!function(){{var n=require("fs");require("path")}c=function(e){try{return n.lstatSync(e).size}catch(t){return 0}},a=function(e,t){n.unlink(e,function(n){n&&o(n),t(n)})},f=function(e){try{n.unlinkSync(e)}catch(t){o(t)}},l=function(e,t,r){return t?void n.open(e,"a",function(e,u){if(null!=e)return o(e),void r(e);var i=new Buffer(t,"utf8");n.write(u,i,0,i.length,0,function(e){e&&o(e),n.close(u,r)})}):void r()},i=function(e,t){if(t)try{var r=n.openSync(e,"a");n.writeSync(r,t),n.closeSync(r)}catch(u){o(u)}}}();var S,x;!function(){S=function(n){try{return g.readdirSync(n)}catch(e){return o(e),[]}},x=function(n){if(!g.existsSync(n)){x(s.dirname(n));try{g.mkdirSync(n)}catch(e){o(e)}}}}();var E;!function(){function n(n,e){r[e]={write:n.write,end:n.end}}function e(n,e){n.write=r[e].write,n.end=r[e].end}function t(n,e){n.write=n.end=function(n,t){null!=n&&("string"!=typeof n&&(n=n.toString()),"stderr"===e&&(n=n.red),j.write(n))}}var r={};n(process.stdout,"stdout"),n(process.stderr,"stderr"),E=function(n){var r=n!==!1?t:e;r(process.stdout,"stdout"),r(process.stderr,"stderr")}}();var k=function(n,e){this.buffer=[],this.path=s.resolve(N,n)+"."+T,this.size=e!==!0?0:c(this.path)};k.prototype={get newLine(){return u},busy:!1,errored:!1,listeners:[],write:function(n){this.size+=n.length+u.length,this.buffer.push(n),this.buffer.length>w&&this[_?"flush":"flushAsync"](),this.size>=b&&A()},flushAsync:function(t){e.flushAsync(this,w,n,t)},flush:function(n){e.flush(this,r,n)},getBuffer_:function(){if(0===this.buffer.length)return"";var n=this.buffer.join(u)+u;return this.buffer.length=0,n}};var O,A;!function(){O=function(){u="html"===v.color?"<br/>":p,x(N);for(var n,e=S(N).sort(),t=e.length,r=/^\d+_/g;--t>-1&&(n=e[t],!r.test(n)););C=t>-1?new k(n.replace(/\.\w+$/,""),!0):A(),C.size>=b&&A(),e.length>=m&&e.slice(0,e.length-m+1).forEach(function(n){f(s.resolve(N,n))})},A=function(){null!=C&&C.flush();var e=new Date;return C=new k(e.getTime()+"_"+n++ +"_"+t(e,"dd-MM_hh"))};var n=0}();var C,N=h,T=d,j={write:function(n,e){null==C&&O(),C.write(n)},flush:function(n){C.flush(n)},flushAsync:function(n){C.flushAsync(n)},cfg:function(n){T=n.extension||d,N=s.resolve(s.dirname(module.parent.filename),n.directory||h),null!=n.bufferSize&&(w=n.bufferSize),n.filesCount&&(m=n.filesCount),n.fileSize&&(b=n.fileSize),null!=n.sync&&(_=n.sync),n.interceptStd&&E()},interceptStd:function(n){E(n)},exception:o};return j},n=r.std,{define:function(e){var t=e.type;if(n=r[t],null==n)throw Error("Transport type is not supported `"+t+"`");return"function"==typeof n&&(n=n()),n.cfg(e),this},write:function(e){n.write(e)},get:function(e){if(null==e)return n;var t=n[e];if(null==t)throw Error("No transport: "+e);return"function"==typeof t?t():t}}}();return function(){var n,t;!function(){n=function(n,t){var r=t,o=d;(null==n||n instanceof k==!1)&&(n=e);var u=n._scope;if(null!=u){var i=x(u);if(null!=i)return o>=i+(t-f)}return null!=n._level&&(r=n._level),o>=r?!0:!1},t=function(e,t){return function(){if(n(this,e)===!1)return this;var r=m(arguments,this);return t&&"string"==typeof r&&(r=t(r)),O.write(r),this}};var e={_parent:null,_level:null,_name:null,_scope:null}}();var r,o;!function(){r={_parent:null,_level:null,_name:null,cfg:function(n){if(0===arguments.length)return v;if("string"==typeof n)return 1===arguments.length?v[n]:(_(n,arguments[1]),this);for(var e in n)_(e,n[e]);return this},color:E,formatMessage:function(){return y(arguments)},log:t(f),warn:t(s,function(n){return n.yellow.bold}),debug:t(a),trace:t(c),error:t(g,function(n){return n.red.bold}),getTransport:function(){return O.get()},create:function(n,e){return new l(this instanceof k&&this||null,n,e)},level_LOG:f,level_WARN:s,level_TRACE:c,level_DEBUG:a,level_ERROR:g,level_EXCEPTION:h},o=u({},r),o.log=o.warn=o.debug=o.trace=o.error=e}();var i;!function(){i=function(){},i.prototype=o}();var l;!function(){l=function(n,e,t){function r(){return o.log.apply(o,arguments)}this._parent=n,this._name=e,this._level=t,null!=n&&n._name&&(this._name=n._name+"."+e),this._scope=this._name;var o=this;return r.__proto__=o,r},l.prototype=r,u(l,r)}(),k=function(e,t){return"string"==typeof e?new l(this instanceof k&&this||null,e,t):"number"==typeof e?n(null,e)?r:o:"boolean"==typeof e&&e===!0?r:o},k.prototype=r,u(k,r)}(),function(){function n(n){var e=parseInt(t[++o]);return isNaN(e)?(k.error("<atma-logger> invalid cli command --"+n+" NUMBER"),null):e}if("undefined"!=typeof process)for(var e,t=process.argv.slice(2),r=t.length,o=-1;++o<r;)switch(e=t[o].replace(/^\-+/,"")){case"no-color":k.cfg("color","none");break;case"level":var u=n("level");if(null==u)break;k.cfg("level",u);break;default:if(0===e.indexOf("level.")){var u=n("level.SCOPE_NAME");if(null==u)break;e=e.replace("level.","");var i={};i[e]=u,k.cfg("levels",i)}}}(),k}); //# sourceMappingURL=global.js.map