crownix-share-logger.min.js 1.1 KB

1
  1. var winston=require("winston");var moment=require("moment");var fs=require("fs");var schedule=require("node-schedule");module.exports=exports=Logger;function Logger(c){var b=((c.level!==undefined)&&(c.level>=0&&c.level<=7))?c.level:4,i,g=c.timestamp||"YYYY-MM-DD HH:mm:ss",h=c.maxFiles||100;if(c.dir){i=c.dir;try{fs.accessSync(i)}catch(f){if(f.code==="ENOENT"){fs.mkdirSync(i)}}}else{fs.mkdirSync("./logs");i="./logs"}var j=new (winston.Logger)({levels:winston.config.syslog.levels,transports:[new (winston.transports.File)({name:"logger",filename:"share_server."+moment().format("YYYYMMDD")+".log",dirname:i,json:false,timestamp:function(){return moment().format(g)},})]});var a=schedule.scheduleJob("0 1 25 * *",function(){fs.readdir(i,function(e,l){if(e){throw new Error(e)}if(l.length>h){var n=l.length-h,m=l.sort(function(p,o){return p<o?-1:(p>o?1:0)});for(var k=0;k<n;k++){fs.unlink(i+"/"+m[k])}}})});var d=function(e){return winston.config.syslog.levels[e]<=b};this.setLogLevel=function(e){b=e};this.getLogLevel=function(){return b};this.log=function(l,e,k){if(d(l)){j.level=l;e="\r\n"+e+"\r\n===>";if(k&&typeof k==="function"){j.log(l,e,k)}else{j.log(l,e)}}}};