diff --git a/app.js b/app.js index b07ccdb..e632e4d 100644 --- a/app.js +++ b/app.js @@ -1,17 +1,23 @@ require('app-module-path').addPath(__dirname); const express = require('express'); const app = express(); -const conf = require('utils/config'); const utils = require('utils/utils'); -var log = require('./utils/log'); -var constants = require('./utils/constants'); - -var testObj = {one:1,two:2,three:3}; -var testArray = [5,6]; -var testObj2 = {test:{ - lv2 : 10 -}}; -console.log(utils.testUnit(testObj,4,testArray,testObj2)); +// const conf = require('utils/config'); +// var log = require('./utils/log'); +// var constants = require('./utils/constants'); + +var core = require('./utils/core'); +// var stats = core.stats; +var log = core.log; +var constants = core.constants; +var conf = core.config; +core.init(app); +//init +// app.use(function (req, res, next) { +// core.init(app); +// next(); +// }); + // console.log(conf.get('redis.host')); // setup generate request-id middleware @@ -124,7 +130,8 @@ app.use(bodyParser.json()); // app.use(passport.session()); -var logg = require('./utils/commonlog').init(conf.get("configlog"),app); + +// var logg = require('./utils/cores/commonlog').init(conf.get("configlog"),app); app.use(function (req, res, next) { // add generated request-id to session //req.session.reqId = req.id; diff --git a/controllers/preference/index.js b/controllers/preference/index.js index 820f2b5..4413a9d 100644 --- a/controllers/preference/index.js +++ b/controllers/preference/index.js @@ -1,10 +1,17 @@ let express = require('express'); let router = express.Router(); + +//core +var core = require('../../utils/core'); +var stats = core.stats; +var log = core.log; +var constants = core.constants; + +//utils +var utils = require("../../utils/utils") let connection = require("../../utils/mongoDB") -var utils = require('../../utils/utils'); -var stats = require('../../utils/stats'); -var log = require('../../utils/log'); -var constants = require('../../utils/constants'); + + var allow = ['general','holiday','shift','service','job','specialist','room']; diff --git a/utils/commonlog.js b/utils/commonlog.js deleted file mode 100644 index c3122f6..0000000 --- a/utils/commonlog.js +++ /dev/null @@ -1,673 +0,0 @@ -const fs = require('fs'); -const os = require("os"); -const path = require("path"); -let rfs = require('rotating-file-stream'); -const mkdirp = require('mkdirp'); -const onHeaders = require('on-headers'); - -const dateFMT = 'yyyymmdd HH:MM:ss.l'; -const dateFMT_SQL = 'yyyy-mm-dd HH:MM:ss.l'; -const fileFMT = 'yyyymmddHHMMss'; -const dateFormat = require('dateformat'); -const sqlite3 = require('sqlite3').verbose(); -process.env.pm_id = process.env.pm_id | '0'; -let db; -//const db = new sqlite3.Database( path.resolve(__dirname, `statDB/stat_${process.env.pm_id}.db`) ); -const cron = require('node-cron'); -// import cron from 'node-cron'; - -let conf = {}; -conf.projectName = 'PROJECT_NAME'; - -conf.log = {}; -conf.log.time = null; //min -conf.log.size = null; //maxsize per file, k -conf.log.path = './appLogPath/'; -conf.log.level = 'debug'; //debug,info,warn,error -conf.log.console = false; -conf.log.file = true; - -conf.summary = {}; -conf.summary.time = 15; -conf.summary.size = null; -conf.summary.path = './summaryPath/'; -conf.summary.console = false; -conf.summary.file = true; - -conf.detail = {}; -conf.detail.time = 15; -conf.detail.size = null; -conf.detail.path = './detailPath/'; -conf.detail.console = false; -conf.detail.file = true; - -conf.stat = {}; -conf.stat.time = 15; -conf.stat.size = 15; -conf.stat.path = './statPath/'; -conf.stat.mode = 0; //0 == file, 1== :memory: -conf.stat.pathDB = undefined; //optional, folder path DB -conf.stat.statInterval = 15; -conf.stat.console = false; -conf.stat.file = true; -// conf.stat.process = [{ -// name:'stat_name_1', -// threshold: 10 -// },{ -// name:'stat_name_2', -// threshold: 10 -// }]; -// conf.alarm = {}; -// conf.alarm.time = 15; -// conf.alarm.size = 15; -// conf.alarm.path = './alarmPath/'; -// conf.alarm.console = false; -// conf.alarm.file = true; - -let log = { - initLog: false -} - - - -function getLogFileName(date, index) { - return os.hostname() + '_' - + conf.projectName - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') - + '.log'; -} -function getStatFileName(date, index) { - return os.hostname() + '_' - + conf.projectName - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') - + '.' + process.env.pm_id - + '.stat'; -} -function getSummaryFileName(date, index) { - return os.hostname() + '_' - + conf.projectName - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') - + '.summary'; -} -function getDetailFileName(date, index) { - return os.hostname() + '_' - + conf.projectName - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') - + '.detail'; -} -function getConf(type) { - if (type === 'app') return conf['log']; - else if (type === 'stt') return conf['stat']; - else if (type === 'smr') return conf['summary']; - else if (type === 'dtl') return conf['detail']; -} -function generator(type) { - return (time, index) => { - if (type === 'app') return getLogFileName(time, index); - else if (type === 'stt') return getStatFileName(time, index); - else if (type === 'smr') return getSummaryFileName(time, index); - else if (type === 'dtl') return getDetailFileName(time, index); - } -} -function createOpts(conf) { - let o = { - path: conf.path - }; - if (conf.size) o.size = conf.size + 'K'; - if (conf.time) o.interval = conf.time + 'm'; - return o; -} -function createStream(type) { - let conf = getConf(type); - let stream = rfs(generator(type), createOpts(conf)); - return stream; -} - -var streamTask = { - app: [], - stt: [], - smr: [], - dtl: [] -}; - -function toStr(txt) { - // console.log(txt + 'txt instanceof Object ' + (txt instanceof Object)); - // console.log(txt + 'txt instanceof Array ' + (txt instanceof Array)); - - if ( txt instanceof Error ) { - return txt.message + ', ' + txt.stack; - } else if ( txt instanceof Object ) { - return JSON.stringify(txt); - } else { - return txt; - } -} - - -function printTxtOrError( _txt ){ - if (_txt instanceof Error) { - return _txt; - }else{ - return _txt; - } -} - -function processApplog(lvlAppLog, ..._txt) { - let session; - let rtxt = ''; - if (_txt instanceof Array) { - if (_txt.length > 1) { - //index0 == session, index1 == text - session = _txt[0]; - rtxt = toStr(_txt[1]) - for (let i = 2; i < _txt.length; i++) { - rtxt += ' ' + toStr(_txt[i]); - } - } else { - session = ''; - rtxt = _txt[0]; - } - } else { - - session = ''; - rtxt = toStr(_txt); - } - return `${getDateTimeLogFormat(new Date())}|${session}|${lvlAppLog}|${rtxt}`; -} - - -function write(type, txt) { - for (const stream of streamTask[type]) { - stream.write(txt + '\r\n'); - } -} - -function getDateTimeLogFormat(currentDates) { - var years = currentDates.getFullYear(); - var months = currentDates.getMonth() + 1; - var day = currentDates.getDate(); - var hours = currentDates.getHours(); - var minutes = currentDates.getMinutes(); - var second = currentDates.getSeconds(); - var millisecs = currentDates.getMilliseconds(); - var monthFormatted = months < 10 ? "0" + months : months; - var dayFormatted = day < 10 ? "0" + day : day; - var hourFormatted = hours < 10 ? "0" + hours : hours; - var minFormatted = minutes < 10 ? "0" + minutes : minutes; - var secFormatted = second < 10 ? "0" + second : second; - var milliFormatted = null; - - if (millisecs < 10) { - milliFormatted = "00" + millisecs; - } - else if (millisecs < 100) { - milliFormatted = "0" + millisecs; - } - else { - milliFormatted = millisecs; - } - var detail = '' + years + monthFormatted + dayFormatted + ' ' + hourFormatted + ':' + minFormatted + ':' + secFormatted + '.' + milliFormatted + '|' + os.hostname() + '|' + conf.projectName; - return detail; -} - - - - - -function initDB() { - if (conf.stat.mode === 0) { - db = new sqlite3.cached.Database(':memory:'); - - } else if (conf.stat.mode === 1) { - if (!conf.stat.pathDB) { - conf.stat.pathDB = path.resolve(__dirname, 'statDB'); - } - if (!fs.existsSync(conf.stat.pathDB)) { - fs.mkdirSync(conf.stat.pathDB); - } - db = new sqlite3.cached.Database(`${conf.stat.pathDB}/stat_${process.env.pm_id}.db`); - } - - db.serialize(function () { - db.run("CREATE TABLE IF NOT EXISTS stat (stat_time TEXT, stat_name TEXT);"); - }); -} - -function initLog() { - if (conf.log) { - if (conf.log.file) { - if (!fs.existsSync(conf.log.path)) { - //fs.mkdirSync(conf.log.path); - mkdirp.sync(conf.log.path); - } - streamTask['app'].push(createStream('app')); - } - if (conf.log.console) streamTask['app'].push(process.stdout); - } - if (conf.stat) { - if (conf.log.file){ - if (!fs.existsSync(conf.stat.path)) { - //fs.mkdirSync(conf.stat.path); - mkdirp.sync(conf.stat.path); - } - streamTask['stt'].push(createStream('stt')); - } - if (conf.log.console) streamTask['stt'].push(process.stdout); - } - if (conf.summary) { - if (conf.log.file){ - if (!fs.existsSync(conf.summary.path)) { - // fs.mkdirSync(conf.summary.path); - mkdirp.sync(conf.summary.path); - } - streamTask['smr'].push(createStream('smr')); - } - if (conf.log.console) streamTask['smr'].push(process.stdout); - } - if (conf.detail) { - if (conf.log.file){ - if (!fs.existsSync(conf.detail.path)) { - // fs.mkdirSync(conf.detail.path); - mkdirp.sync(conf.detail.path); - } - streamTask['dtl'].push(createStream('dtl')); - } - if (conf.log.console) streamTask['dtl'].push(process.stdout); - } -} - -function addStat(stat_name) { - db.serialize(function () { - //db.run("CREATE TABLE lorem (info TEXT, xx TEXT)"); - /*var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); - for (var i = 0; i < 10; i++) { - stmt.run("Ipsum " + i); - } - stmt.finalize(); - - db.run("INSERT INTO stat(stat_time, stat_name) VALUES (?)", ['xx',stat_name], function() { - console.log( 'x' ); - getStat(); - });*/ - - /*db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name], function() { - getStat(); - });*/ - - db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name]); - - /*db.run("INSERT INTO stat VALUES (DATETIME('now'), '"+stat_name+"')", function() { - console.log( 'x' ); - getStat(); - });*/ - - - }); - -} - - - -//var prevStatDateRange; -function fushStat() { - var end = new Date(); - end.setMilliseconds(0); - - //var start = new Date(end.getTime()); - //start.setMinutes(end.getMinutes() - conf.log.statTime); - - var en = dateFormat(end, dateFMT_SQL); - //var st = dateFormat(prevStatDateRange, dateFMT_SQL); - - //prevStatDateRange = end; //assign for next loop - //console.log(st + '|' + en); - //var path = getStatFileName(end); - - db.serialize(function () { - //var stream = fs.createWriteStream(path, { 'flags': 'a' }); - write('stt', en); - - db.each("SELECT stat_name, count(stat_name) as count FROM stat WHERE stat_time < ? group by stat_name order by stat_time", [en], function (err, row) { - if (err) { - console.log(err); - return; - } - //console.log(path+' '+ row.stat_name + ' ' + row.count ); - write('stt', row.stat_name + ' ' + row.count); - }); - - db.run("DELETE FROM stat WHERE stat_time < ?", [en]); - - /*db.each("SELECT stat_name, count(stat_name) as count FROM stat WHERE stat_time BETWEEN ? AND ? group by stat_name order by stat_name", [st, en], function (err, row) { - if (err) { - console.log(err); - return; - } - //console.log(path+' '+ row.stat_name + ' ' + row.count ); - write(end, 'stt', row.stat_name + ' ' + row.count, false); - }); - - db.run("DELETE FROM stat WHERE stat_time BETWEEN ? AND ?", [st, en]);*/ - - }); -} - -function getIntervalTime(type) { - if (type === 'app') { - return conf.log.time; - } else if (type === 'stt') { - return conf.stat.time; - } else if (type === 'smr') { - return conf.summary.time; - } else if (type === 'dtl') { - return conf.detail.time; - } - return null; -} - - - -log.stat = function (stat_name) { - addStat(stat_name); -} - -log.debug = function (..._log) { - if (conf.log.level > 0) return; - write('app', processApplog('debug', ..._log)); -} - -log.info = function (..._log) { - if (conf.log.level > 1) return; - write('app', processApplog('info', ..._log)); -} -log.warn = function (..._log) { - if (conf.log.level > 2) return; - write('app', processApplog('warn', ..._log)); -} -log.error = function (..._log) { - if (conf.log.level > 3) return; - write('app', processApplog('error', ..._log)); -} - -log.detail = function (detailLog) { - write('dtl', detailLog); -} - - -// log.detail = function (session, scenario, identity) { -// var startTimeDate = new Date(); -// var inputTime; -// var outputTime; - -// var detailLog = { -// Session: session, -// InitInvoke: '', //only equinox platform -// Scenario: scenario, -// Identity: identity, -// InputTimeStamp: null, -// Input: [], -// OutputTimeStamp: null, -// Output: [], -// ProcessingTime: null, -// addInputRequest: function (node, cmd, rawData, data) { -// this.addInput(node, cmd, 'REQ', rawData, data); -// }, -// addInputResponse: function (node, cmd, rawData, data, resTime) { -// this.addInput(node, cmd, 'RES', rawData, data, resTime); -// }, -// addInputResponseTimeout: function (node, cmd) { -// this.addInput(node, cmd, 'RES_TIMEOUT'); -// }, -// addInputResponseError: function (node, cmd) { -// this.addInput(node, cmd, 'RES_ERROR'); -// }, -// addInput: function (node, cmd, type, rawData, data, resTime) { -// inputTime = new Date(); -// if (typeof resTime === 'number') { -// resTime = resTime.toLocaleString() + ' ms'; -// } -// var input = { -// Invoke: null, -// Event: node + '.' + cmd, -// Type: type, -// RawData: rawData, -// Data: data, -// ResTime: resTime -// }; -// this.Input.push(input); -// }, - -// addOutputRequest: function (node, cmd, rawData, data) { -// this.addOutput(node, cmd, 'REQ', rawData, data); -// }, -// addOutputResponse: function (node, cmd, rawData, data) { -// this.addOutput(node, cmd, 'RES', rawData, data); -// }, -// addOutputRequestRetry: function (node, cmd, rawData, data, total, maxCount) { -// this.addOutput(node, cmd, ('REQ_RETRY_' + total + '/' + maxCount), rawData, data); -// }, -// addOutput: function (node, cmd, type, rawData, data) { -// outputTime = new Date(); -// var input = { -// Invoke: null, -// Event: node + '.' + cmd, -// Type: type, -// RawData: rawData, -// Data: data -// }; - -// this.Output.push(input); -// }, - -// end: function () { -// let currentTime = new Date(); -// detailLog.ProcessingTime = new Date().getTime() - startTimeDate.getTime(); -// detailLog.InputTimeStamp = dateFormat(inputTime, dateFMT); -// detailLog.OutputTimeStamp = dateFormat(outputTime, dateFMT);; - -// write('dtl', JSON.stringify(detailLog)); -// startTimeDate = currentTime; -// detailLog._clr(); -// }, - -// _clr: function (){ -// detailLog.ProcessingTime = null; -// detailLog.InputTimeStamp = null; -// detailLog.OutputTimeStamp= null; -// detailLog.Input=[]; -// detailLog.Output=[]; -// } -// } - -// return detailLog; -// } - -log.summary = function (session, cmd, identity) { - var now = new Date(); - - var summaryLog = { - requestTime: now, - session: session, - initInvoke: '', //only equinox platform - cmd: cmd, - identity: identity, - /*resultCode: null, - resultDescription: null,*/ - blockDetail: [], - - addSuccessBlock: function (node, cmd, resultCode, resultDesc) { - //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); - addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); - }, - - addErrorBlock: function (node, cmd, resultCode, resultDesc) { - //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); - addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); - }, - - end: function (resultCode, resultDescription) { - var endTime = new Date(); - - var blockDetailTxt = '['; - for (var j = 0; j < this.blockDetail.length; j++) { - var i = this.blockDetail[j]; - var aa = i.node + '; ' + i.cmd + '(' + i.count + '); ['; - - for (var k = 0; k < i.result.length; k++) { - var bb = i.result[k].resultCode + '; ' + i.result[k].resultDesc + '(' + i.result[k].count + ')'; - - if (k === i.result.length - 1) { - aa += bb; - } else { - aa += (bb + ', '); - } - } - - if (j === this.blockDetail.length - 1) { - aa += ']'; - } else { - aa += '], '; - } - blockDetailTxt += aa; - } - blockDetailTxt += ']'; - - - var txt = dateFormat(this.requestTime, dateFMT) + '|'; - txt += this.session + '|'; - txt += this.initInvoke + '|' - txt += this.cmd + '|' - txt += this.identity + '|' - txt += resultCode + '|' - txt += resultDescription + '|' - txt += blockDetailTxt + '|' - txt += dateFormat(endTime, dateFMT) + '|' - txt += (endTime.getTime() - this.requestTime.getTime()); - - write('smr', txt); - } - }; - return summaryLog; -} - -function addBlock(store, node, cmd, resultCode, resultDesc) { - var found = null; - - for (var i = 0; i < store.length; i++) { - if (store[i].node === node && store[i].cmd === cmd) { - found = store[i]; - store[i].count++; - break; - } - } - - if (!found) { - var result = { - resultCode: resultCode, - resultDesc: resultDesc, - count: 1 - } - - var b = { - node: node, - cmd: cmd, - count: 1, - result: [result] - } - store.push(b); - - } else { - var foundResult = false; - for (var j = 0; j < found.result.length; j++) { - if (found.result[j].resultCode === resultCode && - found.result[j].resultDesc === resultDesc) { - - found.result[j].count++; - foundResult = true; - break; - } - } - - if (!foundResult) { - var result = { - resultCode: resultCode, - resultDesc: resultDesc, - count: 1 - } - found.result.push(result); - } - } -} - -log.init = function (_conf, express) { - log.initLog = true; - if (_conf) { - conf = _conf; - } - - - - if (conf.log) { - if (conf.log.level === 'debug') { - conf.log.level = 0; - } else if (conf.log.level === 'info') { - conf.log.level = 1; - } else if (conf.log.level === 'warn') { - conf.log.level = 2; - } else if (conf.log.level === 'error') { - conf.log.level = 3; - } else { - conf.log.level = 4; - } - - //enable only debug - if (express && conf.log.level === 0) { - express.use(function (req, res, next) { - req._reqTimeForLog = Date.now(); - let sid; - if(typeof log.sessionID === 'function'){ - sid = log.sessionID(req, res); - } - - let txtLogReq = 'INCOMING|__Method=' +req.method - + ' __URL=' + req.url - + ' __Headers=' + JSON.stringify(req.headers ) - //+ ' __Params=' + JSON.stringify(req.params ) - + ' __Body=' + JSON.stringify(req.body ); - - if(sid){ - log.debug(sid, txtLogReq); - }else{ - log.debug(txtLogReq); - } - - onHeaders(res, ()=>{ - let txtLogRes = 'OUTGOING|__Statuscode=' + res.statusCode - + ' __Headers=' + JSON.stringify(res._headers ) - + ' __Body=' + req.res.resBody - + ' __Restime=' + ( Date.now() - req._reqTimeForLog ); - - if(sid){ - log.debug(sid, txtLogRes); - }else{ - log.debug(txtLogRes); - } - }); - next(); - }); - } - } - - initLog(); - - if (conf.stat) { - initDB(); - cron.schedule('*/' + conf.stat.statInterval + ' * * * *', function () { - fushStat(); - }); - } - return log; -}; - -module.exports = log; -// module.exports = commonLog(); diff --git a/utils/config.js b/utils/config.js deleted file mode 100644 index b7bcd22..0000000 --- a/utils/config.js +++ /dev/null @@ -1,28 +0,0 @@ - -const nodeEnv = process.env.NODE_ENV || 'default'; - -// setup config -const NodeJsonConfig = require('node-json-config'); -const conf = new NodeJsonConfig('app.config.json'); - -let defaultConfObj = conf.get('default'); -// console.log('defaultConfObj'); -// console.log(defaultConfObj); - -let envConfObj = conf.get(nodeEnv); -// console.log('envConfObj'); -// console.log(envConfObj); - -let currentConfObj = Object.assign(defaultConfObj, envConfObj); - -// console.log('currentConfObj'); -// console.log(currentConfObj); - -conf.put(nodeEnv, currentConfObj); - -conf.getOld = conf.get; -conf.get = function(configName) { - return conf.getOld(nodeEnv + '.' + configName); -}; - -module.exports = conf; \ No newline at end of file diff --git a/utils/constants.js b/utils/constants.js deleted file mode 100644 index 39963bc..0000000 --- a/utils/constants.js +++ /dev/null @@ -1,90 +0,0 @@ -module.exports = Object.freeze({ - REQUEST: 'Request', - RESPONSE: 'Response', - SUCCESS: 'Success', - ERROR: 'Error', - FAIL: 'Fail', - URLKEYWORD : ["filter","offset","orderby","limit","fields"], - METHOD:{ - GET : 'GET', - POST : 'POST', - PUT : 'PUT', - DELETE : 'DELETE' - }, - RESPONSECONN:{ - STATS:{ - ETIMEDOUT:'ETIMEDOUT', - ECONNREFUSED : 'ECONNREFUSED', - ESOCKETTIMEDOUT : 'ESOCKETTIMEDOUT', - MISSING : 'Missing' - }, - MESSAGE:{ - ETIMEDOUT:{ - resultCode : 'null', - resultDescription : 'Time out' - }, - ECONNREFUSED:{ - resultCode : 'null', - resultDescription : 'Reject' - }, - ESOCKETTIMEDOUT:{ - resultCode : 'null', - resultDescription : 'Time out' - }, - EOTHERERROR:{ - resultCode : 'null', - resultDescription : 'Error' - }, - ERROR:{ - resultCode : '50000', - resultDescription : 'System error' - }, - SUCCESS:{ - resultCode : '20000', - resultDescription : 'Success' - }, - EXCEED:{ - resultCode : '40302', - resultDescription : 'Exceed data allowances' - }, - DBERROR:{ - resultCode : '50001', - resultDescription : 'Database error' - }, - SUCCESSWITHCON:{ - resultCode : '20001', - resultDescription : 'Success with condition' - } - } - }, - RESPONSERESULT:{ - SUCCESS:{ - resultCode : '20000', - resultDescription : 'Success' - }, - MISSING_INVALID:{ - resultCode : '40300', - resultDescription : 'Missing or invalid parameter' - }, - DATA_NOT_FOUND:{ - resultCode : '40401', - resultDescription : 'Data Not Found' - }, - DATA_EXIST:{ - resultCode : '40301', - resultDescription : 'Data Exist' - }, - ERROR:{ - resultCode : '50000', - resultDescription : 'System error' - }, - DENIED:{ - resultCode : '40100', - resultDescription : 'Access Denied' - }, - UNKNOW:{ - resultCode : '50060', - resultDescription : 'Unknown URL' - } - } -}); \ No newline at end of file diff --git a/utils/core.js b/utils/core.js new file mode 100644 index 0000000..77f0b11 --- /dev/null +++ b/utils/core.js @@ -0,0 +1,18 @@ +var log = require('./cores/log'); +var stats = require('./cores/stats'); +var constants = require('./cores/constants') +var config = require('./cores/config') + +var init = function(app) +{ + require('./cores/commonlog').init(config.get("configlog"),app); +} + + +module.exports = { + log, + stats, + constants, + config, + init +} \ No newline at end of file diff --git a/utils/cores/commonlog.js b/utils/cores/commonlog.js new file mode 100644 index 0000000..080229e --- /dev/null +++ b/utils/cores/commonlog.js @@ -0,0 +1,673 @@ +const fs = require('fs'); +const os = require("os"); +const path = require("path"); +let rfs = require('rotating-file-stream'); +const mkdirp = require('mkdirp'); +const onHeaders = require('on-headers'); + +const dateFMT = 'yyyymmdd HH:MM:ss.l'; +const dateFMT_SQL = 'yyyy-mm-dd HH:MM:ss.l'; +const fileFMT = 'yyyymmddHHMMss'; +const dateFormat = require('dateformat'); +const sqlite3 = require('sqlite3').verbose(); +process.env.pm_id = process.env.pm_id | '0'; +let db; +//const db = new sqlite3.Database( path.resolve(__dirname, `statDB/stat_${process.env.pm_id}.db`) ); +const cron = require('node-cron'); +// import cron from 'node-cron'; + +let conf = {}; +conf.projectName = 'PROJECT_NAME'; + +conf.log = {}; +conf.log.time = null; //min +conf.log.size = null; //maxsize per file, k +conf.log.path = './appLogPath/'; +conf.log.level = 'debug'; //debug,info,warn,error +conf.log.console = false; +conf.log.file = true; + +conf.summary = {}; +conf.summary.time = 15; +conf.summary.size = null; +conf.summary.path = './summaryPath/'; +conf.summary.console = false; +conf.summary.file = true; + +conf.detail = {}; +conf.detail.time = 15; +conf.detail.size = null; +conf.detail.path = './detailPath/'; +conf.detail.console = false; +conf.detail.file = true; + +conf.stat = {}; +conf.stat.time = 15; +conf.stat.size = 15; +conf.stat.path = './statPath/'; +conf.stat.mode = 0; //0 == file, 1== :memory: +conf.stat.pathDB = undefined; //optional, folder path DB +conf.stat.statInterval = 15; +conf.stat.console = false; +conf.stat.file = true; +// conf.stat.process = [{ +// name:'stat_name_1', +// threshold: 10 +// },{ +// name:'stat_name_2', +// threshold: 10 +// }]; +// conf.alarm = {}; +// conf.alarm.time = 15; +// conf.alarm.size = 15; +// conf.alarm.path = './alarmPath/'; +// conf.alarm.console = false; +// conf.alarm.file = true; + +let log = { + initLog: false +} + + + +function getLogFileName(date, index) { + return os.hostname() + '_' + + conf.projectName + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') + + '.log'; +} +function getStatFileName(date, index) { + return os.hostname() + '_' + + conf.projectName + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') + + '.' + process.env.pm_id + + '.stat'; +} +function getSummaryFileName(date, index) { + return os.hostname() + '_' + + conf.projectName + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') + + '.summary'; +} +function getDetailFileName(date, index) { + return os.hostname() + '_' + + conf.projectName + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') + + '.detail'; +} +function getConf(type) { + if (type === 'app') return conf['log']; + else if (type === 'stt') return conf['stat']; + else if (type === 'smr') return conf['summary']; + else if (type === 'dtl') return conf['detail']; +} +function generator(type) { + return (time, index) => { + if (type === 'app') return getLogFileName(time, index); + else if (type === 'stt') return getStatFileName(time, index); + else if (type === 'smr') return getSummaryFileName(time, index); + else if (type === 'dtl') return getDetailFileName(time, index); + } +} +function createOpts(conf) { + let o = { + path: conf.path + }; + if (conf.size) o.size = conf.size + 'K'; + if (conf.time) o.interval = conf.time + 'm'; + return o; +} +function createStream(type) { + let conf = getConf(type); + let stream = rfs(generator(type), createOpts(conf)); + return stream; +} + +var streamTask = { + app: [], + stt: [], + smr: [], + dtl: [] +}; + +function toStr(txt) { + // console.log(txt + 'txt instanceof Object ' + (txt instanceof Object)); + // console.log(txt + 'txt instanceof Array ' + (txt instanceof Array)); + + if ( txt instanceof Error ) { + return txt.message + ', ' + txt.stack; + } else if ( txt instanceof Object ) { + return JSON.stringify(txt); + } else { + return txt; + } +} + + +function printTxtOrError( _txt ){ + if (_txt instanceof Error) { + return _txt; + }else{ + return _txt; + } +} + +function processApplog(lvlAppLog, ..._txt) { + let session; + let rtxt = ''; + if (_txt instanceof Array) { + if (_txt.length > 1) { + //index0 == session, index1 == text + session = _txt[0]; + rtxt = toStr(_txt[1]) + for (let i = 2; i < _txt.length; i++) { + rtxt += ' ' + toStr(_txt[i]); + } + } else { + session = ''; + rtxt = _txt[0]; + } + } else { + + session = ''; + rtxt = toStr(_txt); + } + return `${getDateTimeLogFormat(new Date())}|${session}|${lvlAppLog}|${rtxt}`; +} + + +function write(type, txt) { + for (const stream of streamTask[type]) { + stream.write(txt + '\r\n'); + } +} + +function getDateTimeLogFormat(currentDates) { + var years = currentDates.getFullYear(); + var months = currentDates.getMonth() + 1; + var day = currentDates.getDate(); + var hours = currentDates.getHours(); + var minutes = currentDates.getMinutes(); + var second = currentDates.getSeconds(); + var millisecs = currentDates.getMilliseconds(); + var monthFormatted = months < 10 ? "0" + months : months; + var dayFormatted = day < 10 ? "0" + day : day; + var hourFormatted = hours < 10 ? "0" + hours : hours; + var minFormatted = minutes < 10 ? "0" + minutes : minutes; + var secFormatted = second < 10 ? "0" + second : second; + var milliFormatted = null; + + if (millisecs < 10) { + milliFormatted = "00" + millisecs; + } + else if (millisecs < 100) { + milliFormatted = "0" + millisecs; + } + else { + milliFormatted = millisecs; + } + var detail = '' + years + monthFormatted + dayFormatted + ' ' + hourFormatted + ':' + minFormatted + ':' + secFormatted + '.' + milliFormatted + '|' + os.hostname() + '|' + conf.projectName; + return detail; +} + + + + + +function initDB() { + if (conf.stat.mode === 0) { + db = new sqlite3.cached.Database(':memory:'); + + } else if (conf.stat.mode === 1) { + if (!conf.stat.pathDB) { + conf.stat.pathDB = path.resolve(__dirname, 'statDB'); + } + if (!fs.existsSync(conf.stat.pathDB)) { + fs.mkdirSync(conf.stat.pathDB); + } + db = new sqlite3.cached.Database(`${conf.stat.pathDB}/stat_${process.env.pm_id}.db`); + } + + db.serialize(function () { + db.run("CREATE TABLE IF NOT EXISTS stat (stat_time TEXT, stat_name TEXT);"); + }); +} + +function initLog() { + if (conf.log) { + if (conf.log.file) { + if (!fs.existsSync(conf.log.path)) { + //fs.mkdirSync(conf.log.path); + mkdirp.sync(conf.log.path); + } + streamTask['app'].push(createStream('app')); + } + if (conf.log.console) streamTask['app'].push(process.stdout); + } + if (conf.stat) { + if (conf.log.file){ + if (!fs.existsSync(conf.stat.path)) { + //fs.mkdirSync(conf.stat.path); + mkdirp.sync(conf.stat.path); + } + streamTask['stt'].push(createStream('stt')); + } + if (conf.log.console) streamTask['stt'].push(process.stdout); + } + if (conf.summary) { + if (conf.log.file){ + if (!fs.existsSync(conf.summary.path)) { + // fs.mkdirSync(conf.summary.path); + mkdirp.sync(conf.summary.path); + } + streamTask['smr'].push(createStream('smr')); + } + if (conf.log.console) streamTask['smr'].push(process.stdout); + } + if (conf.detail) { + if (conf.log.file){ + if (!fs.existsSync(conf.detail.path)) { + // fs.mkdirSync(conf.detail.path); + mkdirp.sync(conf.detail.path); + } + streamTask['dtl'].push(createStream('dtl')); + } + if (conf.log.console) streamTask['dtl'].push(process.stdout); + } +} + +function addStat(stat_name) { + db.serialize(function () { + //db.run("CREATE TABLE lorem (info TEXT, xx TEXT)"); + /*var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); + for (var i = 0; i < 10; i++) { + stmt.run("Ipsum " + i); + } + stmt.finalize(); + + db.run("INSERT INTO stat(stat_time, stat_name) VALUES (?)", ['xx',stat_name], function() { + console.log( 'x' ); + getStat(); + });*/ + + /*db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name], function() { + getStat(); + });*/ + + db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name]); + + /*db.run("INSERT INTO stat VALUES (DATETIME('now'), '"+stat_name+"')", function() { + console.log( 'x' ); + getStat(); + });*/ + + + }); + +} + + + +//var prevStatDateRange; +function fushStat() { + var end = new Date(); + end.setMilliseconds(0); + + //var start = new Date(end.getTime()); + //start.setMinutes(end.getMinutes() - conf.log.statTime); + + var en = dateFormat(end, dateFMT_SQL); + //var st = dateFormat(prevStatDateRange, dateFMT_SQL); + + //prevStatDateRange = end; //assign for next loop + //console.log(st + '|' + en); + //var path = getStatFileName(end); + + db.serialize(function () { + //var stream = fs.createWriteStream(path, { 'flags': 'a' }); + write('stt', en); + + db.each("SELECT stat_name, count(stat_name) as count FROM stat WHERE stat_time < ? group by stat_name order by stat_time", [en], function (err, row) { + if (err) { + console.log(err); + return; + } + //console.log(path+' '+ row.stat_name + ' ' + row.count ); + write('stt', row.stat_name + ' ' + row.count); + }); + + db.run("DELETE FROM stat WHERE stat_time < ?", [en]); + + /*db.each("SELECT stat_name, count(stat_name) as count FROM stat WHERE stat_time BETWEEN ? AND ? group by stat_name order by stat_name", [st, en], function (err, row) { + if (err) { + console.log(err); + return; + } + //console.log(path+' '+ row.stat_name + ' ' + row.count ); + write(end, 'stt', row.stat_name + ' ' + row.count, false); + }); + + db.run("DELETE FROM stat WHERE stat_time BETWEEN ? AND ?", [st, en]);*/ + + }); +} + +function getIntervalTime(type) { + if (type === 'app') { + return conf.log.time; + } else if (type === 'stt') { + return conf.stat.time; + } else if (type === 'smr') { + return conf.summary.time; + } else if (type === 'dtl') { + return conf.detail.time; + } + return null; +} + + + +log.stat = function (stat_name) { + addStat(stat_name); +} + +log.debug = function (..._log) { + if (conf.log.level > 0) return; + write('app', processApplog('debug', ..._log)); +} + +log.info = function (..._log) { + if (conf.log.level > 1) return; + write('app', processApplog('info', ..._log)); +} +log.warn = function (..._log) { + if (conf.log.level > 2) return; + write('app', processApplog('warn', ..._log)); +} +log.error = function (..._log) { + if (conf.log.level > 3) return; + write('app', processApplog('error', ..._log)); +} + +log.detail = function (detailLog) { + write('dtl', detailLog); +} + + +// log.detail = function (session, scenario, identity) { +// var startTimeDate = new Date(); +// var inputTime; +// var outputTime; + +// var detailLog = { +// Session: session, +// InitInvoke: '', //only equinox platform +// Scenario: scenario, +// Identity: identity, +// InputTimeStamp: null, +// Input: [], +// OutputTimeStamp: null, +// Output: [], +// ProcessingTime: null, +// addInputRequest: function (node, cmd, rawData, data) { +// this.addInput(node, cmd, 'REQ', rawData, data); +// }, +// addInputResponse: function (node, cmd, rawData, data, resTime) { +// this.addInput(node, cmd, 'RES', rawData, data, resTime); +// }, +// addInputResponseTimeout: function (node, cmd) { +// this.addInput(node, cmd, 'RES_TIMEOUT'); +// }, +// addInputResponseError: function (node, cmd) { +// this.addInput(node, cmd, 'RES_ERROR'); +// }, +// addInput: function (node, cmd, type, rawData, data, resTime) { +// inputTime = new Date(); +// if (typeof resTime === 'number') { +// resTime = resTime.toLocaleString() + ' ms'; +// } +// var input = { +// Invoke: null, +// Event: node + '.' + cmd, +// Type: type, +// RawData: rawData, +// Data: data, +// ResTime: resTime +// }; +// this.Input.push(input); +// }, + +// addOutputRequest: function (node, cmd, rawData, data) { +// this.addOutput(node, cmd, 'REQ', rawData, data); +// }, +// addOutputResponse: function (node, cmd, rawData, data) { +// this.addOutput(node, cmd, 'RES', rawData, data); +// }, +// addOutputRequestRetry: function (node, cmd, rawData, data, total, maxCount) { +// this.addOutput(node, cmd, ('REQ_RETRY_' + total + '/' + maxCount), rawData, data); +// }, +// addOutput: function (node, cmd, type, rawData, data) { +// outputTime = new Date(); +// var input = { +// Invoke: null, +// Event: node + '.' + cmd, +// Type: type, +// RawData: rawData, +// Data: data +// }; + +// this.Output.push(input); +// }, + +// end: function () { +// let currentTime = new Date(); +// detailLog.ProcessingTime = new Date().getTime() - startTimeDate.getTime(); +// detailLog.InputTimeStamp = dateFormat(inputTime, dateFMT); +// detailLog.OutputTimeStamp = dateFormat(outputTime, dateFMT);; + +// write('dtl', JSON.stringify(detailLog)); +// startTimeDate = currentTime; +// detailLog._clr(); +// }, + +// _clr: function (){ +// detailLog.ProcessingTime = null; +// detailLog.InputTimeStamp = null; +// detailLog.OutputTimeStamp= null; +// detailLog.Input=[]; +// detailLog.Output=[]; +// } +// } + +// return detailLog; +// } + +log.summary = function (session, cmd, identity) { + var now = new Date(); + + var summaryLog = { + requestTime: now, + session: session, + initInvoke: '', //only equinox platform + cmd: cmd, + identity: identity, + /*resultCode: null, + resultDescription: null,*/ + blockDetail: [], + + addSuccessBlock: function (node, cmd, resultCode, resultDesc) { + //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); + addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); + }, + + addErrorBlock: function (node, cmd, resultCode, resultDesc) { + //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); + addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); + }, + + end: function (resultCode, resultDescription) { + var endTime = new Date(); + + var blockDetailTxt = '['; + for (var j = 0; j < this.blockDetail.length; j++) { + var i = this.blockDetail[j]; + var aa = i.node + '; ' + i.cmd + '(' + i.count + '); ['; + + for (var k = 0; k < i.result.length; k++) { + var bb = i.result[k].resultCode + '; ' + i.result[k].resultDesc + '(' + i.result[k].count + ')'; + + if (k === i.result.length - 1) { + aa += bb; + } else { + aa += (bb + ', '); + } + } + + if (j === this.blockDetail.length - 1) { + aa += ']'; + } else { + aa += '], '; + } + blockDetailTxt += aa; + } + blockDetailTxt += ']'; + + + var txt = dateFormat(this.requestTime, dateFMT) + '|'; + txt += this.session + '|'; + txt += this.initInvoke + '|' + txt += this.cmd + '|' + txt += this.identity + '|' + txt += resultCode + '|' + txt += resultDescription + '|' + txt += blockDetailTxt + '|' + txt += dateFormat(endTime, dateFMT) + '|' + txt += (endTime.getTime() - this.requestTime.getTime()); + + write('smr', txt); + } + }; + return summaryLog; +} + +function addBlock(store, node, cmd, resultCode, resultDesc) { + var found = null; + + for (var i = 0; i < store.length; i++) { + if (store[i].node === node && store[i].cmd === cmd) { + found = store[i]; + store[i].count++; + break; + } + } + + if (!found) { + var result = { + resultCode: resultCode, + resultDesc: resultDesc, + count: 1 + } + + var b = { + node: node, + cmd: cmd, + count: 1, + result: [result] + } + store.push(b); + + } else { + var foundResult = false; + for (var j = 0; j < found.result.length; j++) { + if (found.result[j].resultCode === resultCode && + found.result[j].resultDesc === resultDesc) { + + found.result[j].count++; + foundResult = true; + break; + } + } + + if (!foundResult) { + var result = { + resultCode: resultCode, + resultDesc: resultDesc, + count: 1 + } + found.result.push(result); + } + } +} + +log.init = function (_conf, express) { + log.initLog = true; + if (_conf) { + conf = _conf; + } + + + + if (conf.log) { + if (conf.log.level === 'debug') { + conf.log.level = 0; + } else if (conf.log.level === 'info') { + conf.log.level = 1; + } else if (conf.log.level === 'warn') { + conf.log.level = 2; + } else if (conf.log.level === 'error') { + conf.log.level = 3; + } else { + conf.log.level = 4; + } + + //enable only debug + if (express && conf.log.level === 0) { + express.use(function (req, res, next) { + req._reqTimeForLog = Date.now(); + let sid; + if(typeof log.sessionID === 'function'){ + sid = log.sessionID(req, res); + } + + let txtLogReq = 'INCOMING|__Method=' +req.method + + ' __URL=' + req.url + + ' __Headers=' + JSON.stringify(req.headers?req.headers:"{}" ) + //+ ' __Params=' + JSON.stringify(req.params ) + + ' __Body=' + JSON.stringify(req.body?req.body:"{}"); + + if(sid){ + log.debug(sid, txtLogReq); + }else{ + log.debug(txtLogReq); + } + + onHeaders(res, ()=>{ + let txtLogRes = 'OUTGOING|__Statuscode=' + res.statusCode + + ' __Headers=' + JSON.stringify(res._headers ) + + ' __Body=' + req.res.resBody + + ' __Restime=' + ( Date.now() - req._reqTimeForLog ); + + if(sid){ + log.debug(sid, txtLogRes); + }else{ + log.debug(txtLogRes); + } + }); + next(); + }); + } + } + + initLog(); + + if (conf.stat) { + initDB(); + cron.schedule('*/' + conf.stat.statInterval + ' * * * *', function () { + fushStat(); + }); + } + return log; +}; + +module.exports = log; +// module.exports = commonLog(); diff --git a/utils/cores/config.js b/utils/cores/config.js new file mode 100644 index 0000000..b7bcd22 --- /dev/null +++ b/utils/cores/config.js @@ -0,0 +1,28 @@ + +const nodeEnv = process.env.NODE_ENV || 'default'; + +// setup config +const NodeJsonConfig = require('node-json-config'); +const conf = new NodeJsonConfig('app.config.json'); + +let defaultConfObj = conf.get('default'); +// console.log('defaultConfObj'); +// console.log(defaultConfObj); + +let envConfObj = conf.get(nodeEnv); +// console.log('envConfObj'); +// console.log(envConfObj); + +let currentConfObj = Object.assign(defaultConfObj, envConfObj); + +// console.log('currentConfObj'); +// console.log(currentConfObj); + +conf.put(nodeEnv, currentConfObj); + +conf.getOld = conf.get; +conf.get = function(configName) { + return conf.getOld(nodeEnv + '.' + configName); +}; + +module.exports = conf; \ No newline at end of file diff --git a/utils/cores/constants.js b/utils/cores/constants.js new file mode 100644 index 0000000..39963bc --- /dev/null +++ b/utils/cores/constants.js @@ -0,0 +1,90 @@ +module.exports = Object.freeze({ + REQUEST: 'Request', + RESPONSE: 'Response', + SUCCESS: 'Success', + ERROR: 'Error', + FAIL: 'Fail', + URLKEYWORD : ["filter","offset","orderby","limit","fields"], + METHOD:{ + GET : 'GET', + POST : 'POST', + PUT : 'PUT', + DELETE : 'DELETE' + }, + RESPONSECONN:{ + STATS:{ + ETIMEDOUT:'ETIMEDOUT', + ECONNREFUSED : 'ECONNREFUSED', + ESOCKETTIMEDOUT : 'ESOCKETTIMEDOUT', + MISSING : 'Missing' + }, + MESSAGE:{ + ETIMEDOUT:{ + resultCode : 'null', + resultDescription : 'Time out' + }, + ECONNREFUSED:{ + resultCode : 'null', + resultDescription : 'Reject' + }, + ESOCKETTIMEDOUT:{ + resultCode : 'null', + resultDescription : 'Time out' + }, + EOTHERERROR:{ + resultCode : 'null', + resultDescription : 'Error' + }, + ERROR:{ + resultCode : '50000', + resultDescription : 'System error' + }, + SUCCESS:{ + resultCode : '20000', + resultDescription : 'Success' + }, + EXCEED:{ + resultCode : '40302', + resultDescription : 'Exceed data allowances' + }, + DBERROR:{ + resultCode : '50001', + resultDescription : 'Database error' + }, + SUCCESSWITHCON:{ + resultCode : '20001', + resultDescription : 'Success with condition' + } + } + }, + RESPONSERESULT:{ + SUCCESS:{ + resultCode : '20000', + resultDescription : 'Success' + }, + MISSING_INVALID:{ + resultCode : '40300', + resultDescription : 'Missing or invalid parameter' + }, + DATA_NOT_FOUND:{ + resultCode : '40401', + resultDescription : 'Data Not Found' + }, + DATA_EXIST:{ + resultCode : '40301', + resultDescription : 'Data Exist' + }, + ERROR:{ + resultCode : '50000', + resultDescription : 'System error' + }, + DENIED:{ + resultCode : '40100', + resultDescription : 'Access Denied' + }, + UNKNOW:{ + resultCode : '50060', + resultDescription : 'Unknown URL' + } + } +}); \ No newline at end of file diff --git a/utils/cores/helper.js b/utils/cores/helper.js new file mode 100644 index 0000000..f295869 --- /dev/null +++ b/utils/cores/helper.js @@ -0,0 +1,39 @@ +var constants = require('./constants'); + +findCmdfromMethod = function (method) +{ + var cmd = ""; + switch (method) { + case constants.METHOD.GET: + cmd = 'QUERY'; + break; + case constants.METHOD.POST: + cmd = "INSERT" + break; + case constants.METHOD.PUT: + cmd = 'UPDATE'; + break; + case constants.METHOD.DELETE: + cmd = 'DELETE'; + break; + } + return cmd; +} + +undefinedToNull = function(data){ + if(!data){ + data = 'null'; + } else if(data.includes('undefined')){ + data = data.replace(/undefined/g,'null'); + } else { + data = data; + } + + return data; +} + + +module.exports = { + findCmdfromMethod, + undefinedToNull +}; \ No newline at end of file diff --git a/utils/cores/log.js b/utils/cores/log.js new file mode 100644 index 0000000..44f01ee --- /dev/null +++ b/utils/cores/log.js @@ -0,0 +1,265 @@ +var cfg = require('./config'); +var conf = cfg.get("configlog"); +conf.projectName = cfg.get("appName"); +var logg = require('./commonlog'); +var constants = require('./constants'); +var helper = require('./helper'); + +var logger = []; + +const mapLog = {}; + +logger.startlog = function (req,cmdData,session,identity) +{ + + + var start = { + session : helper.undefinedToNull(session), + cmd : cmdData, + identity : helper.undefinedToNull(identity) + }; + + mapLog[req.id].start = start; +} + +logger.stat = function (msg){ + logg.stat(msg); +} + +logger.log = function (msg,type) +{ + + if(!type) + type = 'debug'; + + type = type.toLowerCase(); + + switch(type) { + case 'debug': + logg.debug(msg); + break; + case 'info': + logg.info(msg); + break; + case 'warn': + logg.warn(msg); + break; + case 'error': + logg.error(msg); + break; + default: + logg.debug(msg); + } + + console.log(msg); +} + +logger.addSuccessSummary = function (req,nodeData,cmdData,result) +{ + summary = { + node : helper.undefinedToNull(nodeData), + cmd : cmdData, + resultCode : result.resultCode, + resultDescription : result.resultDescription, + type : constants.SUCCESS + }; + mapLog[req.id].summaryList.push(summary); +} + +logger.addErrorSummary = function (req,nodeData,cmdData,result) +{ + summary = { + node : helper.undefinedToNull(nodeData), + cmd : cmdData, + resultCode : result.resultCode, + resultDescription : result.resultDescription, + type : constants.ERROR + }; + + mapLog[req.id].summaryList.push(summary); +} + +logger.logSummary = function (req,response) +{ + + var start = mapLog[req.id].start; + var summaryList = mapLog[req.id].summaryList; + + var log = logg.summary(start.session,start.cmd,start.identity); + var row; + for (let index = 0; index < summaryList.length; index++) { + row = summaryList[index]; + if(row.type === constants.SUCCESS){ + log.addSuccessBlock(row.node,row.cmd,row.resultCode,row.resultDescription); + } else { + log.addErrorBlock(row.node,row.cmd,helper.undefinedToNull(row.resultCode),helper.undefinedToNull(row.resultDescription)); + } + } + + if(response){ + log.end(response.resultCode,response.resultDescription) + } else { + log.end() + } +} + +logger.startEC = function(req) +{ + var objectLog = { + summaryList : [], + start : {}, + detail : {}, + startTime : new Date() + } + + + mapLog[req.id] = objectLog; + + var newLine = "\r\n"; + var messageRes = ''; + var startTime = mapLog[req.id].startTime; + + + if(req.body) + { + if(typeof req.body === 'string') + body = req.bod; + else + body = JSON.stringify(req.body); + } + + var header = ""; + if(req.headers) + { + if(typeof req.headers === 'string') + header = req.headers; + else + header = JSON.stringify(req.headers); + } + + messageRes += newLine+newLine+"#====> Incoming Message <====# "; + messageRes += newLine+"Session : "+req.id; + messageRes += newLine+"Url : "+req.url; + messageRes += newLine+"Method : "+req.method; + messageRes += newLine+"Header : "+header; + messageRes += newLine+"Body : "+body; + messageRes += newLine+"Time in : "+startTime.toISOString(); + messageRes += newLine + + // logg.debug(messageRes); +} + +logger.endEC = function(req,res) +{ + + var newLine = "\r\n"; + var messageRes = ''; + var startTime = mapLog[req.id].startTime; + var endTime = new Date(); + + var body = ""; + if(res.resBody) + { + if(typeof res.resBody === 'string') + body = res.resBody; + else + body = JSON.stringify(res.resBody); + } + + var header = ""; + if(res.req.headers) + { + if(typeof res.req.headers === 'string') + header = res.req.headers; + else + header = JSON.stringify(res.req.headers); + } + + messageRes += newLine+newLine+"#====> Outgoing Message <====# "; + messageRes += newLine+"Session : "+req.id; + messageRes += newLine+"Header : "+header; + messageRes += newLine+"Body : "+body; + messageRes += newLine+"Time out : "+endTime.toISOString(); + messageRes += newLine+"Use times : "+Math.abs(endTime - startTime)+" ms"; + + + + + // logg.debug(messageRes); + messageRes += newLine +} + +//detail +logger.detailRequestFE = function(req) +{ + // console.log(req); + var protocal = "HTTP"; + var method = req.method; + var url = req.headers.host+req.originalUrl; + var body = JSON.stringify(req.body); + // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); + logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); + +} + +logger.detailResponseFE = function(req) +{ + var protocal = "HTTP"; + var method = req.method; + var url = req.headers.host+req.originalUrl; + var body = JSON.stringify(req.body); + // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); + logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); +} + +//mongo +logger.detailSqlQuery = function(req,method,collection,data) +{ + // logger4jDetail.info(logPrefix(req)+"BE Send Mongo Collection: \""+collection+"\" Query: "+JSON.stringify(data)); + logg.detail(logPrefix(req)+"BE Send Mongo Collection: "+collection+" Method:"+helper.findCmdfromMethod(method)+" Query: "+JSON.stringify(data)); +} + +logger.detailSqlResult = function(req,collection,data) +{ + // logger4jDetail.info(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); + logg.detail(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); + +} + +logger.detailRequestBE = function(req,data) +{ + var protocal = data.protocal; + var method = data.method; + var url = data.url; + var header = JSON.stringify(data.header); + var body = JSON.stringify(data.body); + // logger4jDetail.info(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); + logg.detail(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); +} + +logger.detailResponseBE = function(req,data) +{ + var protocal = data.protocal; + var method = data.method; + var url = data.url; + var header = JSON.stringify(data.header); + var body = JSON.stringify(data.body); + // logger4jDetail.info(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); + logg.detail(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); +} + +//private +function logPrefix(req) +{ + + var userName = "userName"; + var sessionID = "sessionID"; + var session = "session"; + + return ":: ## "+userName+" - "+sessionID+" - "+session+ " ## "; + +} + + + +module.exports = logger; diff --git a/utils/cores/stats.js b/utils/cores/stats.js new file mode 100644 index 0000000..a023f3a --- /dev/null +++ b/utils/cores/stats.js @@ -0,0 +1,51 @@ +var config = require('./config'); +var log = require('./log.js'); +var helper = require('./helper'); + + +var nodeName = config.get("appName"); +var stat = []; + + +//client and Node +stat.receiveRequest = function (method,cmd){ + writeStats(nodeName+" Receive "+method+" "+cmd+" Request"); +}; + +stat.receiveBadRequest = function (method,cmd){ + writeStats(nodeName+" Receive Bad "+method+" "+cmd+" Request"); +}; + +stat.sendResponse = function (method,cmd,result){ + writeStats(nodeName+" Send "+method+" "+cmd+" Response "+(result?result:"Success")); +}; + +//node and mongo +stat.sendQuery = function (method,collection){ + writeStats(nodeName+" Send "+helper.findCmdfromMethod(method)+" "+collection+" Request"); +}; + +stat.receiveBadQuery = function (method,collection){ + writeStats(nodeName+" Receive Bad "+helper.findCmdfromMethod(method)+" "+collection+" Response"); +}; + +stat.receiveQuery = function (method,collection){ + writeStats(nodeName+" Receive "+helper.findCmdfromMethod(method)+" "+collection+" Response"); +}; + +//unknow +stat.receiveUnknow = function(){ + writeStats(nodeName+" Receive Unknown Error Request"); +}; + +stat.sendUnknow = function(){ + writeStats(nodeName+" Send Unknown Error Response"); +}; + +function writeStats(string) { + log.log(string); + log.stat(string); +} + + +module.exports = stat; \ No newline at end of file diff --git a/utils/log.js b/utils/log.js deleted file mode 100644 index 5401f1a..0000000 --- a/utils/log.js +++ /dev/null @@ -1,273 +0,0 @@ -var cfg = require('./config'); -var utils = require('./utils'); -var conf = cfg.get("configlog"); -conf.projectName = cfg.get("appName"); -var logg = require('./commonlog'); -var constants = require('./constants'); -var utils = require('./utils'); - - -// var log4js = require('log4js'); -// var appenders = {}; -// appenders[conf.projectName] = { type: 'file', filename: conf.detail.path+"/"+os.hostname() + '_'+conf.projectName+'.detail' }; -// log4js.configure({ -// appenders: appenders , -// categories: { default: { appenders: [conf.projectName], level: 'info' } } -// }); - -// var logger4jDetail = log4js.getLogger(conf.projectName); -var logger = []; - -const mapLog = {}; - -logger.startlog = function (req,cmdData,session,identity) -{ - - - var start = { - session : utils.undefinedToNull(session), - cmd : cmdData, - identity : utils.undefinedToNull(identity) - }; - - mapLog[req.id].start = start; -} - -logger.stat = function (msg){ - logg.stat(msg); -} - -logger.log = function (msg,type) -{ - - if(!type) - type = 'debug'; - - type = type.toLowerCase(); - - switch(type) { - case 'debug': - logg.debug(msg); - break; - case 'info': - logg.info(msg); - break; - case 'warn': - logg.warn(msg); - break; - case 'error': - logg.error(msg); - break; - default: - logg.debug(msg); - } - - console.log(msg); -} - -logger.addSuccessSummary = function (req,nodeData,cmdData,result) -{ - summary = { - node : utils.undefinedToNull(nodeData), - cmd : cmdData, - resultCode : result.resultCode, - resultDescription : result.resultDescription, - type : constants.SUCCESS - }; - mapLog[req.id].summaryList.push(summary); -} - -logger.addErrorSummary = function (req,nodeData,cmdData,result) -{ - summary = { - node : utils.undefinedToNull(nodeData), - cmd : cmdData, - resultCode : result.resultCode, - resultDescription : result.resultDescription, - type : constants.ERROR - }; - - mapLog[req.id].summaryList.push(summary); -} - -logger.logSummary = function (req,response) -{ - - var start = mapLog[req.id].start; - var summaryList = mapLog[req.id].summaryList; - - var log = logg.summary(start.session,start.cmd,start.identity); - var row; - for (let index = 0; index < summaryList.length; index++) { - row = summaryList[index]; - if(row.type === constants.SUCCESS){ - log.addSuccessBlock(row.node,row.cmd,row.resultCode,row.resultDescription); - } else { - log.addErrorBlock(row.node,row.cmd,utils.undefinedToNull(row.resultCode),utils.undefinedToNull(row.resultDescription)); - } - } - - if(response){ - log.end(response.resultCode,response.resultDescription) - } else { - log.end() - } -} - -logger.startEC = function(req) -{ - var objectLog = { - summaryList : [], - start : {}, - detail : {}, - startTime : new Date() - } - - - mapLog[req.id] = objectLog; - - var newLine = "\r\n"; - var messageRes = ''; - var startTime = mapLog[req.id].startTime; - - - if(req.body) - { - if(typeof req.body === 'string') - body = req.bod; - else - body = JSON.stringify(req.body); - } - - var header = ""; - if(req.headers) - { - if(typeof req.headers === 'string') - header = req.headers; - else - header = JSON.stringify(req.headers); - } - - messageRes += newLine+newLine+"#====> Incoming Message <====# "; - messageRes += newLine+"Session : "+req.id; - messageRes += newLine+"Url : "+req.url; - messageRes += newLine+"Method : "+req.method; - messageRes += newLine+"Header : "+header; - messageRes += newLine+"Body : "+body; - messageRes += newLine+"Time in : "+startTime.toISOString(); - messageRes += newLine - - // logg.debug(messageRes); -} - -logger.endEC = function(req,res) -{ - - var newLine = "\r\n"; - var messageRes = ''; - var startTime = mapLog[req.id].startTime; - var endTime = new Date(); - - var body = ""; - if(res.resBody) - { - if(typeof res.resBody === 'string') - body = res.resBody; - else - body = JSON.stringify(res.resBody); - } - - var header = ""; - if(res.req.headers) - { - if(typeof res.req.headers === 'string') - header = res.req.headers; - else - header = JSON.stringify(res.req.headers); - } - - messageRes += newLine+newLine+"#====> Outgoing Message <====# "; - messageRes += newLine+"Session : "+req.id; - messageRes += newLine+"Header : "+header; - messageRes += newLine+"Body : "+body; - messageRes += newLine+"Time out : "+endTime.toISOString(); - messageRes += newLine+"Use times : "+Math.abs(endTime - startTime)+" ms"; - - - - - // logg.debug(messageRes); - messageRes += newLine -} - -//detail -logger.detailRequestFE = function(req) -{ - // console.log(req); - var protocal = "HTTP"; - var method = req.method; - var url = req.headers.host+req.originalUrl; - var body = JSON.stringify(req.body); - // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); - logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); - -} - -logger.detailResponseFE = function(req) -{ - var protocal = "HTTP"; - var method = req.method; - var url = req.headers.host+req.originalUrl; - var body = JSON.stringify(req.body); - // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); - logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); -} - -//mongo -logger.detailSqlQuery = function(req,method,collection,data) -{ - // logger4jDetail.info(logPrefix(req)+"BE Send Mongo Collection: \""+collection+"\" Query: "+JSON.stringify(data)); - logg.detail(logPrefix(req)+"BE Send Mongo Collection: "+collection+" Method:"+utils.findCmdfromMethod(method)+" Query: "+JSON.stringify(data)); -} - -logger.detailSqlResult = function(req,collection,data) -{ - // logger4jDetail.info(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); - logg.detail(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); - -} - -logger.detailRequestBE = function(req,data) -{ - var protocal = data.protocal; - var method = data.method; - var url = data.url; - var header = JSON.stringify(data.header); - var body = JSON.stringify(data.body); - // logger4jDetail.info(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); - logg.detail(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); -} - -logger.detailResponseBE = function(req,data) -{ - var protocal = data.protocal; - var method = data.method; - var url = data.url; - var header = JSON.stringify(data.header); - var body = JSON.stringify(data.body); - // logger4jDetail.info(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); - logg.detail(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); -} - -function logPrefix(req) -{ - - var userName = "userName"; - var sessionID = "sessionID"; - var session = "session"; - - return ":: ## "+userName+" - "+sessionID+" - "+session+ " ## "; - -} - -module.exports = logger; diff --git a/utils/mongoDB.js b/utils/mongoDB.js index 0e8bd55..2af02ae 100644 --- a/utils/mongoDB.js +++ b/utils/mongoDB.js @@ -1,8 +1,12 @@ -const log = require('./log'); -const stat = require('./stats'); -var constants = require('./constants'); var MongoClient = require('mongodb').MongoClient; -var config = require('./config'); + +const core = require('./core'); +const log = core.log; +const stat = core.stats; +const constants = core.constants; +const config = core.config; + + var connection = {}; connection.reqMongo = async function (req,method, query, collection) { diff --git a/utils/stats.js b/utils/stats.js deleted file mode 100644 index 63334c7..0000000 --- a/utils/stats.js +++ /dev/null @@ -1,51 +0,0 @@ -var config = require('./config'); -var log = require('./log.js'); -var utils = require('./utils'); - - -var nodeName = config.get("appName"); -var stat = []; - - -//client and Node -stat.receiveRequest = function (method,cmd){ - writeStats(nodeName+" Receive "+method+" "+cmd+" Request"); -}; - -stat.receiveBadRequest = function (method,cmd){ - writeStats(nodeName+" Receive Bad "+method+" "+cmd+" Request"); -}; - -stat.sendResponse = function (method,cmd,result){ - writeStats(nodeName+" Send "+method+" "+cmd+" Response "+(result?result:"Success")); -}; - -//node and mongo -stat.sendQuery = function (method,collection){ - writeStats(nodeName+" Send "+utils.findCmdfromMethod(method)+" "+collection+" Request"); -}; - -stat.receiveBadQuery = function (method,collection){ - writeStats(nodeName+" Receive Bad "+utils.findCmdfromMethod(method)+" "+collection+" Response"); -}; - -stat.receiveQuery = function (method,collection){ - writeStats(nodeName+" Receive "+utils.findCmdfromMethod(method)+" "+collection+" Response"); -}; - -//unknow -stat.receiveUnknow = function(){ - writeStats(nodeName+" Receive Unknown Error Request"); -}; - -stat.sendUnknow = function(){ - writeStats(nodeName+" Send Unknown Error Response"); -}; - -function writeStats(string) { - log.log(string); - log.stat(string); -} - - -module.exports = stat; \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index e20255c..6fafcf4 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -1,6 +1,6 @@ var parseFilter = require('ldapjs').parseFilter; -var constants = require('./constants'); +var constants = require('./cores/constants'); // //unit test // var fileTempUnitTest = './unitTest/testCase.txt'; -- libgit2 0.21.2