@ezuikit/utils-logger
Version:
logger utils
8 lines (7 loc) • 2.99 kB
JavaScript
/*
* @ezuikit/utils-logger v1.1.0
* logger utils
* Copyright (c) 2025-08-29 Ezviz-OpenBiz
* Released under the MIT License.
*/
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):(global="undefined"!=typeof globalThis?globalThis:global||self).Logger=factory()}(this,(function(){"use strict";var LoggerStyle={info:"",log:"background: #4096ff; color: #FFF;",warn:"background: yellow; color: #FFF;",error:"background: red; color: #FFF;"},LoggerCls=function(){function LoggerCls(options){void 0===options&&(options={}),this._options={level:"INFO",showTime:!1},this._levelNum=3,this.info=this._loggerFactory("info",this._levelNum>=3),this.log=this._loggerFactory("log",this._levelNum>=2),this.warn=this._loggerFactory("warn",this._levelNum>=1),this.error=this._loggerFactory("error",this._levelNum>=0),this.setOptions(options)}var _proto=LoggerCls.prototype;return _proto.setOptions=function(options){var _this__options_level;this._options=Object.assign({},this._options,options),this._levelNum=this._matchLevel(null!=(_this__options_level=this._options.level)?_this__options_level:"INFO"),this.info=this._loggerFactory("info",this._levelNum>=3),this.log=this._loggerFactory("log",this._levelNum>=2),this.warn=this._loggerFactory("warn",this._levelNum>=1),this.error=this._loggerFactory("error",this._levelNum>=0)},_proto._matchLevel=function(level){var logLevel=3;switch(level){case"INFO":logLevel=3;break;case"LOG":logLevel=2;break;case"WARN":logLevel=1;break;case"ERROR":logLevel=0}return logLevel},_proto._loggerFactory=function(type,bool){var fn=console[type];if(bool&&fn){var _fn,args0=this._options.name?"%c["+this._options.name+"]%c %c["+type.toUpperCase()+"]":"%c["+type.toUpperCase()+"]",color=[this._options.name?"background: green;color: #fff":null,this._options.name?"":null,LoggerStyle[type]].filter((function(color){return null!=color}));return(_fn=fn).bind.apply(_fn,[].concat([console,args0],color))}return LoggerCls.noop},_proto.getOptions=function(){return this._options},_proto.getVersion=function(){return"1.1.0"},LoggerCls}();function fillTen(num){return(num=+num)<10&&(num="0"+num),num+""}LoggerCls.noop=function(){},LoggerCls.VERSION="1.1.0";var list=["info","log","warn","error"];return function(options){var logger=new LoggerCls(options);return new Proxy(logger,{get:function(target,prop){if(list.includes(prop)){var _target__options;if(null==(_target__options=target._options)?void 0:_target__options.showTime){var time=(now=new Date(Date.now()),year=now.getFullYear(),month=now.getMonth()+1,day=now.getDate(),hour=now.getHours(),min=now.getMinutes(),sec=now.getSeconds(),ms=now.getMilliseconds(),year+"/"+fillTen(month)+"/"+fillTen(day)+" "+fillTen(hour)+":"+fillTen(min)+":"+fillTen(sec)+":"+ms);return target[prop].bind(console,"["+time+"]")}return target[prop].bind(console)}var now,year,month,day,hour,min,sec,ms;return Reflect.get(target,prop)}})}}));