Commit 6fc87409f1b9e5cc4495f6411f9762023e6e1dec
1 parent
180aaa4d
Exists in
master
update structure
Showing
16 changed files
with
1202 additions
and
1135 deletions
Show diff stats
app.js
| 1 | 1 | require('app-module-path').addPath(__dirname); |
| 2 | 2 | const express = require('express'); |
| 3 | 3 | const app = express(); |
| 4 | -const conf = require('utils/config'); | |
| 5 | 4 | const utils = require('utils/utils'); |
| 6 | -var log = require('./utils/log'); | |
| 7 | -var constants = require('./utils/constants'); | |
| 8 | - | |
| 9 | -var testObj = {one:1,two:2,three:3}; | |
| 10 | -var testArray = [5,6]; | |
| 11 | -var testObj2 = {test:{ | |
| 12 | - lv2 : 10 | |
| 13 | -}}; | |
| 14 | -console.log(utils.testUnit(testObj,4,testArray,testObj2)); | |
| 5 | +// const conf = require('utils/config'); | |
| 6 | +// var log = require('./utils/log'); | |
| 7 | +// var constants = require('./utils/constants'); | |
| 8 | + | |
| 9 | +var core = require('./utils/core'); | |
| 10 | +// var stats = core.stats; | |
| 11 | +var log = core.log; | |
| 12 | +var constants = core.constants; | |
| 13 | +var conf = core.config; | |
| 14 | +core.init(app); | |
| 15 | +//init | |
| 16 | +// app.use(function (req, res, next) { | |
| 17 | +// core.init(app); | |
| 18 | +// next(); | |
| 19 | +// }); | |
| 20 | + | |
| 15 | 21 | // console.log(conf.get('redis.host')); |
| 16 | 22 | |
| 17 | 23 | // setup generate request-id middleware |
| ... | ... | @@ -124,7 +130,8 @@ app.use(bodyParser.json()); |
| 124 | 130 | // app.use(passport.session()); |
| 125 | 131 | |
| 126 | 132 | |
| 127 | -var logg = require('./utils/commonlog').init(conf.get("configlog"),app); | |
| 133 | + | |
| 134 | +// var logg = require('./utils/cores/commonlog').init(conf.get("configlog"),app); | |
| 128 | 135 | app.use(function (req, res, next) { |
| 129 | 136 | // add generated request-id to session |
| 130 | 137 | //req.session.reqId = req.id; | ... | ... |
controllers/preference/index.js
| 1 | 1 | let express = require('express'); |
| 2 | 2 | let router = express.Router(); |
| 3 | + | |
| 4 | +//core | |
| 5 | +var core = require('../../utils/core'); | |
| 6 | +var stats = core.stats; | |
| 7 | +var log = core.log; | |
| 8 | +var constants = core.constants; | |
| 9 | + | |
| 10 | +//utils | |
| 11 | +var utils = require("../../utils/utils") | |
| 3 | 12 | let connection = require("../../utils/mongoDB") |
| 4 | -var utils = require('../../utils/utils'); | |
| 5 | -var stats = require('../../utils/stats'); | |
| 6 | -var log = require('../../utils/log'); | |
| 7 | -var constants = require('../../utils/constants'); | |
| 13 | + | |
| 14 | + | |
| 8 | 15 | |
| 9 | 16 | |
| 10 | 17 | var allow = ['general','holiday','shift','service','job','specialist','room']; | ... | ... |
utils/commonlog.js
| ... | ... | @@ -1,673 +0,0 @@ |
| 1 | -const fs = require('fs'); | |
| 2 | -const os = require("os"); | |
| 3 | -const path = require("path"); | |
| 4 | -let rfs = require('rotating-file-stream'); | |
| 5 | -const mkdirp = require('mkdirp'); | |
| 6 | -const onHeaders = require('on-headers'); | |
| 7 | - | |
| 8 | -const dateFMT = 'yyyymmdd HH:MM:ss.l'; | |
| 9 | -const dateFMT_SQL = 'yyyy-mm-dd HH:MM:ss.l'; | |
| 10 | -const fileFMT = 'yyyymmddHHMMss'; | |
| 11 | -const dateFormat = require('dateformat'); | |
| 12 | -const sqlite3 = require('sqlite3').verbose(); | |
| 13 | -process.env.pm_id = process.env.pm_id | '0'; | |
| 14 | -let db; | |
| 15 | -//const db = new sqlite3.Database( path.resolve(__dirname, `statDB/stat_${process.env.pm_id}.db`) ); | |
| 16 | -const cron = require('node-cron'); | |
| 17 | -// import cron from 'node-cron'; | |
| 18 | - | |
| 19 | -let conf = {}; | |
| 20 | -conf.projectName = 'PROJECT_NAME'; | |
| 21 | - | |
| 22 | -conf.log = {}; | |
| 23 | -conf.log.time = null; //min | |
| 24 | -conf.log.size = null; //maxsize per file, k | |
| 25 | -conf.log.path = './appLogPath/'; | |
| 26 | -conf.log.level = 'debug'; //debug,info,warn,error | |
| 27 | -conf.log.console = false; | |
| 28 | -conf.log.file = true; | |
| 29 | - | |
| 30 | -conf.summary = {}; | |
| 31 | -conf.summary.time = 15; | |
| 32 | -conf.summary.size = null; | |
| 33 | -conf.summary.path = './summaryPath/'; | |
| 34 | -conf.summary.console = false; | |
| 35 | -conf.summary.file = true; | |
| 36 | - | |
| 37 | -conf.detail = {}; | |
| 38 | -conf.detail.time = 15; | |
| 39 | -conf.detail.size = null; | |
| 40 | -conf.detail.path = './detailPath/'; | |
| 41 | -conf.detail.console = false; | |
| 42 | -conf.detail.file = true; | |
| 43 | - | |
| 44 | -conf.stat = {}; | |
| 45 | -conf.stat.time = 15; | |
| 46 | -conf.stat.size = 15; | |
| 47 | -conf.stat.path = './statPath/'; | |
| 48 | -conf.stat.mode = 0; //0 == file, 1== :memory: | |
| 49 | -conf.stat.pathDB = undefined; //optional, folder path DB | |
| 50 | -conf.stat.statInterval = 15; | |
| 51 | -conf.stat.console = false; | |
| 52 | -conf.stat.file = true; | |
| 53 | -// conf.stat.process = [{ | |
| 54 | -// name:'stat_name_1', | |
| 55 | -// threshold: 10 | |
| 56 | -// },{ | |
| 57 | -// name:'stat_name_2', | |
| 58 | -// threshold: 10 | |
| 59 | -// }]; | |
| 60 | -// conf.alarm = {}; | |
| 61 | -// conf.alarm.time = 15; | |
| 62 | -// conf.alarm.size = 15; | |
| 63 | -// conf.alarm.path = './alarmPath/'; | |
| 64 | -// conf.alarm.console = false; | |
| 65 | -// conf.alarm.file = true; | |
| 66 | - | |
| 67 | -let log = { | |
| 68 | - initLog: false | |
| 69 | -} | |
| 70 | - | |
| 71 | - | |
| 72 | - | |
| 73 | -function getLogFileName(date, index) { | |
| 74 | - return os.hostname() + '_' | |
| 75 | - + conf.projectName | |
| 76 | - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 77 | - + '.log'; | |
| 78 | -} | |
| 79 | -function getStatFileName(date, index) { | |
| 80 | - return os.hostname() + '_' | |
| 81 | - + conf.projectName | |
| 82 | - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 83 | - + '.' + process.env.pm_id | |
| 84 | - + '.stat'; | |
| 85 | -} | |
| 86 | -function getSummaryFileName(date, index) { | |
| 87 | - return os.hostname() + '_' | |
| 88 | - + conf.projectName | |
| 89 | - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 90 | - + '.summary'; | |
| 91 | -} | |
| 92 | -function getDetailFileName(date, index) { | |
| 93 | - return os.hostname() + '_' | |
| 94 | - + conf.projectName | |
| 95 | - + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 96 | - + '.detail'; | |
| 97 | -} | |
| 98 | -function getConf(type) { | |
| 99 | - if (type === 'app') return conf['log']; | |
| 100 | - else if (type === 'stt') return conf['stat']; | |
| 101 | - else if (type === 'smr') return conf['summary']; | |
| 102 | - else if (type === 'dtl') return conf['detail']; | |
| 103 | -} | |
| 104 | -function generator(type) { | |
| 105 | - return (time, index) => { | |
| 106 | - if (type === 'app') return getLogFileName(time, index); | |
| 107 | - else if (type === 'stt') return getStatFileName(time, index); | |
| 108 | - else if (type === 'smr') return getSummaryFileName(time, index); | |
| 109 | - else if (type === 'dtl') return getDetailFileName(time, index); | |
| 110 | - } | |
| 111 | -} | |
| 112 | -function createOpts(conf) { | |
| 113 | - let o = { | |
| 114 | - path: conf.path | |
| 115 | - }; | |
| 116 | - if (conf.size) o.size = conf.size + 'K'; | |
| 117 | - if (conf.time) o.interval = conf.time + 'm'; | |
| 118 | - return o; | |
| 119 | -} | |
| 120 | -function createStream(type) { | |
| 121 | - let conf = getConf(type); | |
| 122 | - let stream = rfs(generator(type), createOpts(conf)); | |
| 123 | - return stream; | |
| 124 | -} | |
| 125 | - | |
| 126 | -var streamTask = { | |
| 127 | - app: [], | |
| 128 | - stt: [], | |
| 129 | - smr: [], | |
| 130 | - dtl: [] | |
| 131 | -}; | |
| 132 | - | |
| 133 | -function toStr(txt) { | |
| 134 | - // console.log(txt + 'txt instanceof Object ' + (txt instanceof Object)); | |
| 135 | - // console.log(txt + 'txt instanceof Array ' + (txt instanceof Array)); | |
| 136 | - | |
| 137 | - if ( txt instanceof Error ) { | |
| 138 | - return txt.message + ', ' + txt.stack; | |
| 139 | - } else if ( txt instanceof Object ) { | |
| 140 | - return JSON.stringify(txt); | |
| 141 | - } else { | |
| 142 | - return txt; | |
| 143 | - } | |
| 144 | -} | |
| 145 | - | |
| 146 | - | |
| 147 | -function printTxtOrError( _txt ){ | |
| 148 | - if (_txt instanceof Error) { | |
| 149 | - return _txt; | |
| 150 | - }else{ | |
| 151 | - return _txt; | |
| 152 | - } | |
| 153 | -} | |
| 154 | - | |
| 155 | -function processApplog(lvlAppLog, ..._txt) { | |
| 156 | - let session; | |
| 157 | - let rtxt = ''; | |
| 158 | - if (_txt instanceof Array) { | |
| 159 | - if (_txt.length > 1) { | |
| 160 | - //index0 == session, index1 == text | |
| 161 | - session = _txt[0]; | |
| 162 | - rtxt = toStr(_txt[1]) | |
| 163 | - for (let i = 2; i < _txt.length; i++) { | |
| 164 | - rtxt += ' ' + toStr(_txt[i]); | |
| 165 | - } | |
| 166 | - } else { | |
| 167 | - session = ''; | |
| 168 | - rtxt = _txt[0]; | |
| 169 | - } | |
| 170 | - } else { | |
| 171 | - | |
| 172 | - session = ''; | |
| 173 | - rtxt = toStr(_txt); | |
| 174 | - } | |
| 175 | - return `${getDateTimeLogFormat(new Date())}|${session}|${lvlAppLog}|${rtxt}`; | |
| 176 | -} | |
| 177 | - | |
| 178 | - | |
| 179 | -function write(type, txt) { | |
| 180 | - for (const stream of streamTask[type]) { | |
| 181 | - stream.write(txt + '\r\n'); | |
| 182 | - } | |
| 183 | -} | |
| 184 | - | |
| 185 | -function getDateTimeLogFormat(currentDates) { | |
| 186 | - var years = currentDates.getFullYear(); | |
| 187 | - var months = currentDates.getMonth() + 1; | |
| 188 | - var day = currentDates.getDate(); | |
| 189 | - var hours = currentDates.getHours(); | |
| 190 | - var minutes = currentDates.getMinutes(); | |
| 191 | - var second = currentDates.getSeconds(); | |
| 192 | - var millisecs = currentDates.getMilliseconds(); | |
| 193 | - var monthFormatted = months < 10 ? "0" + months : months; | |
| 194 | - var dayFormatted = day < 10 ? "0" + day : day; | |
| 195 | - var hourFormatted = hours < 10 ? "0" + hours : hours; | |
| 196 | - var minFormatted = minutes < 10 ? "0" + minutes : minutes; | |
| 197 | - var secFormatted = second < 10 ? "0" + second : second; | |
| 198 | - var milliFormatted = null; | |
| 199 | - | |
| 200 | - if (millisecs < 10) { | |
| 201 | - milliFormatted = "00" + millisecs; | |
| 202 | - } | |
| 203 | - else if (millisecs < 100) { | |
| 204 | - milliFormatted = "0" + millisecs; | |
| 205 | - } | |
| 206 | - else { | |
| 207 | - milliFormatted = millisecs; | |
| 208 | - } | |
| 209 | - var detail = '' + years + monthFormatted + dayFormatted + ' ' + hourFormatted + ':' + minFormatted + ':' + secFormatted + '.' + milliFormatted + '|' + os.hostname() + '|' + conf.projectName; | |
| 210 | - return detail; | |
| 211 | -} | |
| 212 | - | |
| 213 | - | |
| 214 | - | |
| 215 | - | |
| 216 | - | |
| 217 | -function initDB() { | |
| 218 | - if (conf.stat.mode === 0) { | |
| 219 | - db = new sqlite3.cached.Database(':memory:'); | |
| 220 | - | |
| 221 | - } else if (conf.stat.mode === 1) { | |
| 222 | - if (!conf.stat.pathDB) { | |
| 223 | - conf.stat.pathDB = path.resolve(__dirname, 'statDB'); | |
| 224 | - } | |
| 225 | - if (!fs.existsSync(conf.stat.pathDB)) { | |
| 226 | - fs.mkdirSync(conf.stat.pathDB); | |
| 227 | - } | |
| 228 | - db = new sqlite3.cached.Database(`${conf.stat.pathDB}/stat_${process.env.pm_id}.db`); | |
| 229 | - } | |
| 230 | - | |
| 231 | - db.serialize(function () { | |
| 232 | - db.run("CREATE TABLE IF NOT EXISTS stat (stat_time TEXT, stat_name TEXT);"); | |
| 233 | - }); | |
| 234 | -} | |
| 235 | - | |
| 236 | -function initLog() { | |
| 237 | - if (conf.log) { | |
| 238 | - if (conf.log.file) { | |
| 239 | - if (!fs.existsSync(conf.log.path)) { | |
| 240 | - //fs.mkdirSync(conf.log.path); | |
| 241 | - mkdirp.sync(conf.log.path); | |
| 242 | - } | |
| 243 | - streamTask['app'].push(createStream('app')); | |
| 244 | - } | |
| 245 | - if (conf.log.console) streamTask['app'].push(process.stdout); | |
| 246 | - } | |
| 247 | - if (conf.stat) { | |
| 248 | - if (conf.log.file){ | |
| 249 | - if (!fs.existsSync(conf.stat.path)) { | |
| 250 | - //fs.mkdirSync(conf.stat.path); | |
| 251 | - mkdirp.sync(conf.stat.path); | |
| 252 | - } | |
| 253 | - streamTask['stt'].push(createStream('stt')); | |
| 254 | - } | |
| 255 | - if (conf.log.console) streamTask['stt'].push(process.stdout); | |
| 256 | - } | |
| 257 | - if (conf.summary) { | |
| 258 | - if (conf.log.file){ | |
| 259 | - if (!fs.existsSync(conf.summary.path)) { | |
| 260 | - // fs.mkdirSync(conf.summary.path); | |
| 261 | - mkdirp.sync(conf.summary.path); | |
| 262 | - } | |
| 263 | - streamTask['smr'].push(createStream('smr')); | |
| 264 | - } | |
| 265 | - if (conf.log.console) streamTask['smr'].push(process.stdout); | |
| 266 | - } | |
| 267 | - if (conf.detail) { | |
| 268 | - if (conf.log.file){ | |
| 269 | - if (!fs.existsSync(conf.detail.path)) { | |
| 270 | - // fs.mkdirSync(conf.detail.path); | |
| 271 | - mkdirp.sync(conf.detail.path); | |
| 272 | - } | |
| 273 | - streamTask['dtl'].push(createStream('dtl')); | |
| 274 | - } | |
| 275 | - if (conf.log.console) streamTask['dtl'].push(process.stdout); | |
| 276 | - } | |
| 277 | -} | |
| 278 | - | |
| 279 | -function addStat(stat_name) { | |
| 280 | - db.serialize(function () { | |
| 281 | - //db.run("CREATE TABLE lorem (info TEXT, xx TEXT)"); | |
| 282 | - /*var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); | |
| 283 | - for (var i = 0; i < 10; i++) { | |
| 284 | - stmt.run("Ipsum " + i); | |
| 285 | - } | |
| 286 | - stmt.finalize(); | |
| 287 | - | |
| 288 | - db.run("INSERT INTO stat(stat_time, stat_name) VALUES (?)", ['xx',stat_name], function() { | |
| 289 | - console.log( 'x' ); | |
| 290 | - getStat(); | |
| 291 | - });*/ | |
| 292 | - | |
| 293 | - /*db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name], function() { | |
| 294 | - getStat(); | |
| 295 | - });*/ | |
| 296 | - | |
| 297 | - db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name]); | |
| 298 | - | |
| 299 | - /*db.run("INSERT INTO stat VALUES (DATETIME('now'), '"+stat_name+"')", function() { | |
| 300 | - console.log( 'x' ); | |
| 301 | - getStat(); | |
| 302 | - });*/ | |
| 303 | - | |
| 304 | - | |
| 305 | - }); | |
| 306 | - | |
| 307 | -} | |
| 308 | - | |
| 309 | - | |
| 310 | - | |
| 311 | -//var prevStatDateRange; | |
| 312 | -function fushStat() { | |
| 313 | - var end = new Date(); | |
| 314 | - end.setMilliseconds(0); | |
| 315 | - | |
| 316 | - //var start = new Date(end.getTime()); | |
| 317 | - //start.setMinutes(end.getMinutes() - conf.log.statTime); | |
| 318 | - | |
| 319 | - var en = dateFormat(end, dateFMT_SQL); | |
| 320 | - //var st = dateFormat(prevStatDateRange, dateFMT_SQL); | |
| 321 | - | |
| 322 | - //prevStatDateRange = end; //assign for next loop | |
| 323 | - //console.log(st + '|' + en); | |
| 324 | - //var path = getStatFileName(end); | |
| 325 | - | |
| 326 | - db.serialize(function () { | |
| 327 | - //var stream = fs.createWriteStream(path, { 'flags': 'a' }); | |
| 328 | - write('stt', en); | |
| 329 | - | |
| 330 | - 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) { | |
| 331 | - if (err) { | |
| 332 | - console.log(err); | |
| 333 | - return; | |
| 334 | - } | |
| 335 | - //console.log(path+' '+ row.stat_name + ' ' + row.count ); | |
| 336 | - write('stt', row.stat_name + ' ' + row.count); | |
| 337 | - }); | |
| 338 | - | |
| 339 | - db.run("DELETE FROM stat WHERE stat_time < ?", [en]); | |
| 340 | - | |
| 341 | - /*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) { | |
| 342 | - if (err) { | |
| 343 | - console.log(err); | |
| 344 | - return; | |
| 345 | - } | |
| 346 | - //console.log(path+' '+ row.stat_name + ' ' + row.count ); | |
| 347 | - write(end, 'stt', row.stat_name + ' ' + row.count, false); | |
| 348 | - }); | |
| 349 | - | |
| 350 | - db.run("DELETE FROM stat WHERE stat_time BETWEEN ? AND ?", [st, en]);*/ | |
| 351 | - | |
| 352 | - }); | |
| 353 | -} | |
| 354 | - | |
| 355 | -function getIntervalTime(type) { | |
| 356 | - if (type === 'app') { | |
| 357 | - return conf.log.time; | |
| 358 | - } else if (type === 'stt') { | |
| 359 | - return conf.stat.time; | |
| 360 | - } else if (type === 'smr') { | |
| 361 | - return conf.summary.time; | |
| 362 | - } else if (type === 'dtl') { | |
| 363 | - return conf.detail.time; | |
| 364 | - } | |
| 365 | - return null; | |
| 366 | -} | |
| 367 | - | |
| 368 | - | |
| 369 | - | |
| 370 | -log.stat = function (stat_name) { | |
| 371 | - addStat(stat_name); | |
| 372 | -} | |
| 373 | - | |
| 374 | -log.debug = function (..._log) { | |
| 375 | - if (conf.log.level > 0) return; | |
| 376 | - write('app', processApplog('debug', ..._log)); | |
| 377 | -} | |
| 378 | - | |
| 379 | -log.info = function (..._log) { | |
| 380 | - if (conf.log.level > 1) return; | |
| 381 | - write('app', processApplog('info', ..._log)); | |
| 382 | -} | |
| 383 | -log.warn = function (..._log) { | |
| 384 | - if (conf.log.level > 2) return; | |
| 385 | - write('app', processApplog('warn', ..._log)); | |
| 386 | -} | |
| 387 | -log.error = function (..._log) { | |
| 388 | - if (conf.log.level > 3) return; | |
| 389 | - write('app', processApplog('error', ..._log)); | |
| 390 | -} | |
| 391 | - | |
| 392 | -log.detail = function (detailLog) { | |
| 393 | - write('dtl', detailLog); | |
| 394 | -} | |
| 395 | - | |
| 396 | - | |
| 397 | -// log.detail = function (session, scenario, identity) { | |
| 398 | -// var startTimeDate = new Date(); | |
| 399 | -// var inputTime; | |
| 400 | -// var outputTime; | |
| 401 | - | |
| 402 | -// var detailLog = { | |
| 403 | -// Session: session, | |
| 404 | -// InitInvoke: '', //only equinox platform | |
| 405 | -// Scenario: scenario, | |
| 406 | -// Identity: identity, | |
| 407 | -// InputTimeStamp: null, | |
| 408 | -// Input: [], | |
| 409 | -// OutputTimeStamp: null, | |
| 410 | -// Output: [], | |
| 411 | -// ProcessingTime: null, | |
| 412 | -// addInputRequest: function (node, cmd, rawData, data) { | |
| 413 | -// this.addInput(node, cmd, 'REQ', rawData, data); | |
| 414 | -// }, | |
| 415 | -// addInputResponse: function (node, cmd, rawData, data, resTime) { | |
| 416 | -// this.addInput(node, cmd, 'RES', rawData, data, resTime); | |
| 417 | -// }, | |
| 418 | -// addInputResponseTimeout: function (node, cmd) { | |
| 419 | -// this.addInput(node, cmd, 'RES_TIMEOUT'); | |
| 420 | -// }, | |
| 421 | -// addInputResponseError: function (node, cmd) { | |
| 422 | -// this.addInput(node, cmd, 'RES_ERROR'); | |
| 423 | -// }, | |
| 424 | -// addInput: function (node, cmd, type, rawData, data, resTime) { | |
| 425 | -// inputTime = new Date(); | |
| 426 | -// if (typeof resTime === 'number') { | |
| 427 | -// resTime = resTime.toLocaleString() + ' ms'; | |
| 428 | -// } | |
| 429 | -// var input = { | |
| 430 | -// Invoke: null, | |
| 431 | -// Event: node + '.' + cmd, | |
| 432 | -// Type: type, | |
| 433 | -// RawData: rawData, | |
| 434 | -// Data: data, | |
| 435 | -// ResTime: resTime | |
| 436 | -// }; | |
| 437 | -// this.Input.push(input); | |
| 438 | -// }, | |
| 439 | - | |
| 440 | -// addOutputRequest: function (node, cmd, rawData, data) { | |
| 441 | -// this.addOutput(node, cmd, 'REQ', rawData, data); | |
| 442 | -// }, | |
| 443 | -// addOutputResponse: function (node, cmd, rawData, data) { | |
| 444 | -// this.addOutput(node, cmd, 'RES', rawData, data); | |
| 445 | -// }, | |
| 446 | -// addOutputRequestRetry: function (node, cmd, rawData, data, total, maxCount) { | |
| 447 | -// this.addOutput(node, cmd, ('REQ_RETRY_' + total + '/' + maxCount), rawData, data); | |
| 448 | -// }, | |
| 449 | -// addOutput: function (node, cmd, type, rawData, data) { | |
| 450 | -// outputTime = new Date(); | |
| 451 | -// var input = { | |
| 452 | -// Invoke: null, | |
| 453 | -// Event: node + '.' + cmd, | |
| 454 | -// Type: type, | |
| 455 | -// RawData: rawData, | |
| 456 | -// Data: data | |
| 457 | -// }; | |
| 458 | - | |
| 459 | -// this.Output.push(input); | |
| 460 | -// }, | |
| 461 | - | |
| 462 | -// end: function () { | |
| 463 | -// let currentTime = new Date(); | |
| 464 | -// detailLog.ProcessingTime = new Date().getTime() - startTimeDate.getTime(); | |
| 465 | -// detailLog.InputTimeStamp = dateFormat(inputTime, dateFMT); | |
| 466 | -// detailLog.OutputTimeStamp = dateFormat(outputTime, dateFMT);; | |
| 467 | - | |
| 468 | -// write('dtl', JSON.stringify(detailLog)); | |
| 469 | -// startTimeDate = currentTime; | |
| 470 | -// detailLog._clr(); | |
| 471 | -// }, | |
| 472 | - | |
| 473 | -// _clr: function (){ | |
| 474 | -// detailLog.ProcessingTime = null; | |
| 475 | -// detailLog.InputTimeStamp = null; | |
| 476 | -// detailLog.OutputTimeStamp= null; | |
| 477 | -// detailLog.Input=[]; | |
| 478 | -// detailLog.Output=[]; | |
| 479 | -// } | |
| 480 | -// } | |
| 481 | - | |
| 482 | -// return detailLog; | |
| 483 | -// } | |
| 484 | - | |
| 485 | -log.summary = function (session, cmd, identity) { | |
| 486 | - var now = new Date(); | |
| 487 | - | |
| 488 | - var summaryLog = { | |
| 489 | - requestTime: now, | |
| 490 | - session: session, | |
| 491 | - initInvoke: '', //only equinox platform | |
| 492 | - cmd: cmd, | |
| 493 | - identity: identity, | |
| 494 | - /*resultCode: null, | |
| 495 | - resultDescription: null,*/ | |
| 496 | - blockDetail: [], | |
| 497 | - | |
| 498 | - addSuccessBlock: function (node, cmd, resultCode, resultDesc) { | |
| 499 | - //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); | |
| 500 | - addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); | |
| 501 | - }, | |
| 502 | - | |
| 503 | - addErrorBlock: function (node, cmd, resultCode, resultDesc) { | |
| 504 | - //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); | |
| 505 | - addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); | |
| 506 | - }, | |
| 507 | - | |
| 508 | - end: function (resultCode, resultDescription) { | |
| 509 | - var endTime = new Date(); | |
| 510 | - | |
| 511 | - var blockDetailTxt = '['; | |
| 512 | - for (var j = 0; j < this.blockDetail.length; j++) { | |
| 513 | - var i = this.blockDetail[j]; | |
| 514 | - var aa = i.node + '; ' + i.cmd + '(' + i.count + '); ['; | |
| 515 | - | |
| 516 | - for (var k = 0; k < i.result.length; k++) { | |
| 517 | - var bb = i.result[k].resultCode + '; ' + i.result[k].resultDesc + '(' + i.result[k].count + ')'; | |
| 518 | - | |
| 519 | - if (k === i.result.length - 1) { | |
| 520 | - aa += bb; | |
| 521 | - } else { | |
| 522 | - aa += (bb + ', '); | |
| 523 | - } | |
| 524 | - } | |
| 525 | - | |
| 526 | - if (j === this.blockDetail.length - 1) { | |
| 527 | - aa += ']'; | |
| 528 | - } else { | |
| 529 | - aa += '], '; | |
| 530 | - } | |
| 531 | - blockDetailTxt += aa; | |
| 532 | - } | |
| 533 | - blockDetailTxt += ']'; | |
| 534 | - | |
| 535 | - | |
| 536 | - var txt = dateFormat(this.requestTime, dateFMT) + '|'; | |
| 537 | - txt += this.session + '|'; | |
| 538 | - txt += this.initInvoke + '|' | |
| 539 | - txt += this.cmd + '|' | |
| 540 | - txt += this.identity + '|' | |
| 541 | - txt += resultCode + '|' | |
| 542 | - txt += resultDescription + '|' | |
| 543 | - txt += blockDetailTxt + '|' | |
| 544 | - txt += dateFormat(endTime, dateFMT) + '|' | |
| 545 | - txt += (endTime.getTime() - this.requestTime.getTime()); | |
| 546 | - | |
| 547 | - write('smr', txt); | |
| 548 | - } | |
| 549 | - }; | |
| 550 | - return summaryLog; | |
| 551 | -} | |
| 552 | - | |
| 553 | -function addBlock(store, node, cmd, resultCode, resultDesc) { | |
| 554 | - var found = null; | |
| 555 | - | |
| 556 | - for (var i = 0; i < store.length; i++) { | |
| 557 | - if (store[i].node === node && store[i].cmd === cmd) { | |
| 558 | - found = store[i]; | |
| 559 | - store[i].count++; | |
| 560 | - break; | |
| 561 | - } | |
| 562 | - } | |
| 563 | - | |
| 564 | - if (!found) { | |
| 565 | - var result = { | |
| 566 | - resultCode: resultCode, | |
| 567 | - resultDesc: resultDesc, | |
| 568 | - count: 1 | |
| 569 | - } | |
| 570 | - | |
| 571 | - var b = { | |
| 572 | - node: node, | |
| 573 | - cmd: cmd, | |
| 574 | - count: 1, | |
| 575 | - result: [result] | |
| 576 | - } | |
| 577 | - store.push(b); | |
| 578 | - | |
| 579 | - } else { | |
| 580 | - var foundResult = false; | |
| 581 | - for (var j = 0; j < found.result.length; j++) { | |
| 582 | - if (found.result[j].resultCode === resultCode && | |
| 583 | - found.result[j].resultDesc === resultDesc) { | |
| 584 | - | |
| 585 | - found.result[j].count++; | |
| 586 | - foundResult = true; | |
| 587 | - break; | |
| 588 | - } | |
| 589 | - } | |
| 590 | - | |
| 591 | - if (!foundResult) { | |
| 592 | - var result = { | |
| 593 | - resultCode: resultCode, | |
| 594 | - resultDesc: resultDesc, | |
| 595 | - count: 1 | |
| 596 | - } | |
| 597 | - found.result.push(result); | |
| 598 | - } | |
| 599 | - } | |
| 600 | -} | |
| 601 | - | |
| 602 | -log.init = function (_conf, express) { | |
| 603 | - log.initLog = true; | |
| 604 | - if (_conf) { | |
| 605 | - conf = _conf; | |
| 606 | - } | |
| 607 | - | |
| 608 | - | |
| 609 | - | |
| 610 | - if (conf.log) { | |
| 611 | - if (conf.log.level === 'debug') { | |
| 612 | - conf.log.level = 0; | |
| 613 | - } else if (conf.log.level === 'info') { | |
| 614 | - conf.log.level = 1; | |
| 615 | - } else if (conf.log.level === 'warn') { | |
| 616 | - conf.log.level = 2; | |
| 617 | - } else if (conf.log.level === 'error') { | |
| 618 | - conf.log.level = 3; | |
| 619 | - } else { | |
| 620 | - conf.log.level = 4; | |
| 621 | - } | |
| 622 | - | |
| 623 | - //enable only debug | |
| 624 | - if (express && conf.log.level === 0) { | |
| 625 | - express.use(function (req, res, next) { | |
| 626 | - req._reqTimeForLog = Date.now(); | |
| 627 | - let sid; | |
| 628 | - if(typeof log.sessionID === 'function'){ | |
| 629 | - sid = log.sessionID(req, res); | |
| 630 | - } | |
| 631 | - | |
| 632 | - let txtLogReq = 'INCOMING|__Method=' +req.method | |
| 633 | - + ' __URL=' + req.url | |
| 634 | - + ' __Headers=' + JSON.stringify(req.headers ) | |
| 635 | - //+ ' __Params=' + JSON.stringify(req.params ) | |
| 636 | - + ' __Body=' + JSON.stringify(req.body ); | |
| 637 | - | |
| 638 | - if(sid){ | |
| 639 | - log.debug(sid, txtLogReq); | |
| 640 | - }else{ | |
| 641 | - log.debug(txtLogReq); | |
| 642 | - } | |
| 643 | - | |
| 644 | - onHeaders(res, ()=>{ | |
| 645 | - let txtLogRes = 'OUTGOING|__Statuscode=' + res.statusCode | |
| 646 | - + ' __Headers=' + JSON.stringify(res._headers ) | |
| 647 | - + ' __Body=' + req.res.resBody | |
| 648 | - + ' __Restime=' + ( Date.now() - req._reqTimeForLog ); | |
| 649 | - | |
| 650 | - if(sid){ | |
| 651 | - log.debug(sid, txtLogRes); | |
| 652 | - }else{ | |
| 653 | - log.debug(txtLogRes); | |
| 654 | - } | |
| 655 | - }); | |
| 656 | - next(); | |
| 657 | - }); | |
| 658 | - } | |
| 659 | - } | |
| 660 | - | |
| 661 | - initLog(); | |
| 662 | - | |
| 663 | - if (conf.stat) { | |
| 664 | - initDB(); | |
| 665 | - cron.schedule('*/' + conf.stat.statInterval + ' * * * *', function () { | |
| 666 | - fushStat(); | |
| 667 | - }); | |
| 668 | - } | |
| 669 | - return log; | |
| 670 | -}; | |
| 671 | - | |
| 672 | -module.exports = log; | |
| 673 | -// module.exports = commonLog(); |
utils/config.js
| ... | ... | @@ -1,28 +0,0 @@ |
| 1 | - | |
| 2 | -const nodeEnv = process.env.NODE_ENV || 'default'; | |
| 3 | - | |
| 4 | -// setup config | |
| 5 | -const NodeJsonConfig = require('node-json-config'); | |
| 6 | -const conf = new NodeJsonConfig('app.config.json'); | |
| 7 | - | |
| 8 | -let defaultConfObj = conf.get('default'); | |
| 9 | -// console.log('defaultConfObj'); | |
| 10 | -// console.log(defaultConfObj); | |
| 11 | - | |
| 12 | -let envConfObj = conf.get(nodeEnv); | |
| 13 | -// console.log('envConfObj'); | |
| 14 | -// console.log(envConfObj); | |
| 15 | - | |
| 16 | -let currentConfObj = Object.assign(defaultConfObj, envConfObj); | |
| 17 | - | |
| 18 | -// console.log('currentConfObj'); | |
| 19 | -// console.log(currentConfObj); | |
| 20 | - | |
| 21 | -conf.put(nodeEnv, currentConfObj); | |
| 22 | - | |
| 23 | -conf.getOld = conf.get; | |
| 24 | -conf.get = function(configName) { | |
| 25 | - return conf.getOld(nodeEnv + '.' + configName); | |
| 26 | -}; | |
| 27 | - | |
| 28 | -module.exports = conf; | |
| 29 | 0 | \ No newline at end of file |
utils/constants.js
| ... | ... | @@ -1,90 +0,0 @@ |
| 1 | -module.exports = Object.freeze({ | |
| 2 | - REQUEST: 'Request', | |
| 3 | - RESPONSE: 'Response', | |
| 4 | - SUCCESS: 'Success', | |
| 5 | - ERROR: 'Error', | |
| 6 | - FAIL: 'Fail', | |
| 7 | - URLKEYWORD : ["filter","offset","orderby","limit","fields"], | |
| 8 | - METHOD:{ | |
| 9 | - GET : 'GET', | |
| 10 | - POST : 'POST', | |
| 11 | - PUT : 'PUT', | |
| 12 | - DELETE : 'DELETE' | |
| 13 | - }, | |
| 14 | - RESPONSECONN:{ | |
| 15 | - STATS:{ | |
| 16 | - ETIMEDOUT:'ETIMEDOUT', | |
| 17 | - ECONNREFUSED : 'ECONNREFUSED', | |
| 18 | - ESOCKETTIMEDOUT : 'ESOCKETTIMEDOUT', | |
| 19 | - MISSING : 'Missing' | |
| 20 | - }, | |
| 21 | - MESSAGE:{ | |
| 22 | - ETIMEDOUT:{ | |
| 23 | - resultCode : 'null', | |
| 24 | - resultDescription : 'Time out' | |
| 25 | - }, | |
| 26 | - ECONNREFUSED:{ | |
| 27 | - resultCode : 'null', | |
| 28 | - resultDescription : 'Reject' | |
| 29 | - }, | |
| 30 | - ESOCKETTIMEDOUT:{ | |
| 31 | - resultCode : 'null', | |
| 32 | - resultDescription : 'Time out' | |
| 33 | - }, | |
| 34 | - EOTHERERROR:{ | |
| 35 | - resultCode : 'null', | |
| 36 | - resultDescription : 'Error' | |
| 37 | - }, | |
| 38 | - ERROR:{ | |
| 39 | - resultCode : '50000', | |
| 40 | - resultDescription : 'System error' | |
| 41 | - }, | |
| 42 | - SUCCESS:{ | |
| 43 | - resultCode : '20000', | |
| 44 | - resultDescription : 'Success' | |
| 45 | - }, | |
| 46 | - EXCEED:{ | |
| 47 | - resultCode : '40302', | |
| 48 | - resultDescription : 'Exceed data allowances' | |
| 49 | - }, | |
| 50 | - DBERROR:{ | |
| 51 | - resultCode : '50001', | |
| 52 | - resultDescription : 'Database error' | |
| 53 | - }, | |
| 54 | - SUCCESSWITHCON:{ | |
| 55 | - resultCode : '20001', | |
| 56 | - resultDescription : 'Success with condition' | |
| 57 | - } | |
| 58 | - } | |
| 59 | - }, | |
| 60 | - RESPONSERESULT:{ | |
| 61 | - SUCCESS:{ | |
| 62 | - resultCode : '20000', | |
| 63 | - resultDescription : 'Success' | |
| 64 | - }, | |
| 65 | - MISSING_INVALID:{ | |
| 66 | - resultCode : '40300', | |
| 67 | - resultDescription : 'Missing or invalid parameter' | |
| 68 | - }, | |
| 69 | - DATA_NOT_FOUND:{ | |
| 70 | - resultCode : '40401', | |
| 71 | - resultDescription : 'Data Not Found' | |
| 72 | - }, | |
| 73 | - DATA_EXIST:{ | |
| 74 | - resultCode : '40301', | |
| 75 | - resultDescription : 'Data Exist' | |
| 76 | - }, | |
| 77 | - ERROR:{ | |
| 78 | - resultCode : '50000', | |
| 79 | - resultDescription : 'System error' | |
| 80 | - }, | |
| 81 | - DENIED:{ | |
| 82 | - resultCode : '40100', | |
| 83 | - resultDescription : 'Access Denied' | |
| 84 | - }, | |
| 85 | - UNKNOW:{ | |
| 86 | - resultCode : '50060', | |
| 87 | - resultDescription : 'Unknown URL' | |
| 88 | - } | |
| 89 | - } | |
| 90 | -}); | |
| 91 | 0 | \ No newline at end of file |
| ... | ... | @@ -0,0 +1,18 @@ |
| 1 | +var log = require('./cores/log'); | |
| 2 | +var stats = require('./cores/stats'); | |
| 3 | +var constants = require('./cores/constants') | |
| 4 | +var config = require('./cores/config') | |
| 5 | + | |
| 6 | +var init = function(app) | |
| 7 | +{ | |
| 8 | + require('./cores/commonlog').init(config.get("configlog"),app); | |
| 9 | +} | |
| 10 | + | |
| 11 | + | |
| 12 | +module.exports = { | |
| 13 | + log, | |
| 14 | + stats, | |
| 15 | + constants, | |
| 16 | + config, | |
| 17 | + init | |
| 18 | +} | |
| 0 | 19 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,673 @@ |
| 1 | +const fs = require('fs'); | |
| 2 | +const os = require("os"); | |
| 3 | +const path = require("path"); | |
| 4 | +let rfs = require('rotating-file-stream'); | |
| 5 | +const mkdirp = require('mkdirp'); | |
| 6 | +const onHeaders = require('on-headers'); | |
| 7 | + | |
| 8 | +const dateFMT = 'yyyymmdd HH:MM:ss.l'; | |
| 9 | +const dateFMT_SQL = 'yyyy-mm-dd HH:MM:ss.l'; | |
| 10 | +const fileFMT = 'yyyymmddHHMMss'; | |
| 11 | +const dateFormat = require('dateformat'); | |
| 12 | +const sqlite3 = require('sqlite3').verbose(); | |
| 13 | +process.env.pm_id = process.env.pm_id | '0'; | |
| 14 | +let db; | |
| 15 | +//const db = new sqlite3.Database( path.resolve(__dirname, `statDB/stat_${process.env.pm_id}.db`) ); | |
| 16 | +const cron = require('node-cron'); | |
| 17 | +// import cron from 'node-cron'; | |
| 18 | + | |
| 19 | +let conf = {}; | |
| 20 | +conf.projectName = 'PROJECT_NAME'; | |
| 21 | + | |
| 22 | +conf.log = {}; | |
| 23 | +conf.log.time = null; //min | |
| 24 | +conf.log.size = null; //maxsize per file, k | |
| 25 | +conf.log.path = './appLogPath/'; | |
| 26 | +conf.log.level = 'debug'; //debug,info,warn,error | |
| 27 | +conf.log.console = false; | |
| 28 | +conf.log.file = true; | |
| 29 | + | |
| 30 | +conf.summary = {}; | |
| 31 | +conf.summary.time = 15; | |
| 32 | +conf.summary.size = null; | |
| 33 | +conf.summary.path = './summaryPath/'; | |
| 34 | +conf.summary.console = false; | |
| 35 | +conf.summary.file = true; | |
| 36 | + | |
| 37 | +conf.detail = {}; | |
| 38 | +conf.detail.time = 15; | |
| 39 | +conf.detail.size = null; | |
| 40 | +conf.detail.path = './detailPath/'; | |
| 41 | +conf.detail.console = false; | |
| 42 | +conf.detail.file = true; | |
| 43 | + | |
| 44 | +conf.stat = {}; | |
| 45 | +conf.stat.time = 15; | |
| 46 | +conf.stat.size = 15; | |
| 47 | +conf.stat.path = './statPath/'; | |
| 48 | +conf.stat.mode = 0; //0 == file, 1== :memory: | |
| 49 | +conf.stat.pathDB = undefined; //optional, folder path DB | |
| 50 | +conf.stat.statInterval = 15; | |
| 51 | +conf.stat.console = false; | |
| 52 | +conf.stat.file = true; | |
| 53 | +// conf.stat.process = [{ | |
| 54 | +// name:'stat_name_1', | |
| 55 | +// threshold: 10 | |
| 56 | +// },{ | |
| 57 | +// name:'stat_name_2', | |
| 58 | +// threshold: 10 | |
| 59 | +// }]; | |
| 60 | +// conf.alarm = {}; | |
| 61 | +// conf.alarm.time = 15; | |
| 62 | +// conf.alarm.size = 15; | |
| 63 | +// conf.alarm.path = './alarmPath/'; | |
| 64 | +// conf.alarm.console = false; | |
| 65 | +// conf.alarm.file = true; | |
| 66 | + | |
| 67 | +let log = { | |
| 68 | + initLog: false | |
| 69 | +} | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | +function getLogFileName(date, index) { | |
| 74 | + return os.hostname() + '_' | |
| 75 | + + conf.projectName | |
| 76 | + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 77 | + + '.log'; | |
| 78 | +} | |
| 79 | +function getStatFileName(date, index) { | |
| 80 | + return os.hostname() + '_' | |
| 81 | + + conf.projectName | |
| 82 | + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 83 | + + '.' + process.env.pm_id | |
| 84 | + + '.stat'; | |
| 85 | +} | |
| 86 | +function getSummaryFileName(date, index) { | |
| 87 | + return os.hostname() + '_' | |
| 88 | + + conf.projectName | |
| 89 | + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 90 | + + '.summary'; | |
| 91 | +} | |
| 92 | +function getDetailFileName(date, index) { | |
| 93 | + return os.hostname() + '_' | |
| 94 | + + conf.projectName | |
| 95 | + + (date ? ('_' + dateFormat(date, fileFMT) + '.' + index) : '') | |
| 96 | + + '.detail'; | |
| 97 | +} | |
| 98 | +function getConf(type) { | |
| 99 | + if (type === 'app') return conf['log']; | |
| 100 | + else if (type === 'stt') return conf['stat']; | |
| 101 | + else if (type === 'smr') return conf['summary']; | |
| 102 | + else if (type === 'dtl') return conf['detail']; | |
| 103 | +} | |
| 104 | +function generator(type) { | |
| 105 | + return (time, index) => { | |
| 106 | + if (type === 'app') return getLogFileName(time, index); | |
| 107 | + else if (type === 'stt') return getStatFileName(time, index); | |
| 108 | + else if (type === 'smr') return getSummaryFileName(time, index); | |
| 109 | + else if (type === 'dtl') return getDetailFileName(time, index); | |
| 110 | + } | |
| 111 | +} | |
| 112 | +function createOpts(conf) { | |
| 113 | + let o = { | |
| 114 | + path: conf.path | |
| 115 | + }; | |
| 116 | + if (conf.size) o.size = conf.size + 'K'; | |
| 117 | + if (conf.time) o.interval = conf.time + 'm'; | |
| 118 | + return o; | |
| 119 | +} | |
| 120 | +function createStream(type) { | |
| 121 | + let conf = getConf(type); | |
| 122 | + let stream = rfs(generator(type), createOpts(conf)); | |
| 123 | + return stream; | |
| 124 | +} | |
| 125 | + | |
| 126 | +var streamTask = { | |
| 127 | + app: [], | |
| 128 | + stt: [], | |
| 129 | + smr: [], | |
| 130 | + dtl: [] | |
| 131 | +}; | |
| 132 | + | |
| 133 | +function toStr(txt) { | |
| 134 | + // console.log(txt + 'txt instanceof Object ' + (txt instanceof Object)); | |
| 135 | + // console.log(txt + 'txt instanceof Array ' + (txt instanceof Array)); | |
| 136 | + | |
| 137 | + if ( txt instanceof Error ) { | |
| 138 | + return txt.message + ', ' + txt.stack; | |
| 139 | + } else if ( txt instanceof Object ) { | |
| 140 | + return JSON.stringify(txt); | |
| 141 | + } else { | |
| 142 | + return txt; | |
| 143 | + } | |
| 144 | +} | |
| 145 | + | |
| 146 | + | |
| 147 | +function printTxtOrError( _txt ){ | |
| 148 | + if (_txt instanceof Error) { | |
| 149 | + return _txt; | |
| 150 | + }else{ | |
| 151 | + return _txt; | |
| 152 | + } | |
| 153 | +} | |
| 154 | + | |
| 155 | +function processApplog(lvlAppLog, ..._txt) { | |
| 156 | + let session; | |
| 157 | + let rtxt = ''; | |
| 158 | + if (_txt instanceof Array) { | |
| 159 | + if (_txt.length > 1) { | |
| 160 | + //index0 == session, index1 == text | |
| 161 | + session = _txt[0]; | |
| 162 | + rtxt = toStr(_txt[1]) | |
| 163 | + for (let i = 2; i < _txt.length; i++) { | |
| 164 | + rtxt += ' ' + toStr(_txt[i]); | |
| 165 | + } | |
| 166 | + } else { | |
| 167 | + session = ''; | |
| 168 | + rtxt = _txt[0]; | |
| 169 | + } | |
| 170 | + } else { | |
| 171 | + | |
| 172 | + session = ''; | |
| 173 | + rtxt = toStr(_txt); | |
| 174 | + } | |
| 175 | + return `${getDateTimeLogFormat(new Date())}|${session}|${lvlAppLog}|${rtxt}`; | |
| 176 | +} | |
| 177 | + | |
| 178 | + | |
| 179 | +function write(type, txt) { | |
| 180 | + for (const stream of streamTask[type]) { | |
| 181 | + stream.write(txt + '\r\n'); | |
| 182 | + } | |
| 183 | +} | |
| 184 | + | |
| 185 | +function getDateTimeLogFormat(currentDates) { | |
| 186 | + var years = currentDates.getFullYear(); | |
| 187 | + var months = currentDates.getMonth() + 1; | |
| 188 | + var day = currentDates.getDate(); | |
| 189 | + var hours = currentDates.getHours(); | |
| 190 | + var minutes = currentDates.getMinutes(); | |
| 191 | + var second = currentDates.getSeconds(); | |
| 192 | + var millisecs = currentDates.getMilliseconds(); | |
| 193 | + var monthFormatted = months < 10 ? "0" + months : months; | |
| 194 | + var dayFormatted = day < 10 ? "0" + day : day; | |
| 195 | + var hourFormatted = hours < 10 ? "0" + hours : hours; | |
| 196 | + var minFormatted = minutes < 10 ? "0" + minutes : minutes; | |
| 197 | + var secFormatted = second < 10 ? "0" + second : second; | |
| 198 | + var milliFormatted = null; | |
| 199 | + | |
| 200 | + if (millisecs < 10) { | |
| 201 | + milliFormatted = "00" + millisecs; | |
| 202 | + } | |
| 203 | + else if (millisecs < 100) { | |
| 204 | + milliFormatted = "0" + millisecs; | |
| 205 | + } | |
| 206 | + else { | |
| 207 | + milliFormatted = millisecs; | |
| 208 | + } | |
| 209 | + var detail = '' + years + monthFormatted + dayFormatted + ' ' + hourFormatted + ':' + minFormatted + ':' + secFormatted + '.' + milliFormatted + '|' + os.hostname() + '|' + conf.projectName; | |
| 210 | + return detail; | |
| 211 | +} | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | +function initDB() { | |
| 218 | + if (conf.stat.mode === 0) { | |
| 219 | + db = new sqlite3.cached.Database(':memory:'); | |
| 220 | + | |
| 221 | + } else if (conf.stat.mode === 1) { | |
| 222 | + if (!conf.stat.pathDB) { | |
| 223 | + conf.stat.pathDB = path.resolve(__dirname, 'statDB'); | |
| 224 | + } | |
| 225 | + if (!fs.existsSync(conf.stat.pathDB)) { | |
| 226 | + fs.mkdirSync(conf.stat.pathDB); | |
| 227 | + } | |
| 228 | + db = new sqlite3.cached.Database(`${conf.stat.pathDB}/stat_${process.env.pm_id}.db`); | |
| 229 | + } | |
| 230 | + | |
| 231 | + db.serialize(function () { | |
| 232 | + db.run("CREATE TABLE IF NOT EXISTS stat (stat_time TEXT, stat_name TEXT);"); | |
| 233 | + }); | |
| 234 | +} | |
| 235 | + | |
| 236 | +function initLog() { | |
| 237 | + if (conf.log) { | |
| 238 | + if (conf.log.file) { | |
| 239 | + if (!fs.existsSync(conf.log.path)) { | |
| 240 | + //fs.mkdirSync(conf.log.path); | |
| 241 | + mkdirp.sync(conf.log.path); | |
| 242 | + } | |
| 243 | + streamTask['app'].push(createStream('app')); | |
| 244 | + } | |
| 245 | + if (conf.log.console) streamTask['app'].push(process.stdout); | |
| 246 | + } | |
| 247 | + if (conf.stat) { | |
| 248 | + if (conf.log.file){ | |
| 249 | + if (!fs.existsSync(conf.stat.path)) { | |
| 250 | + //fs.mkdirSync(conf.stat.path); | |
| 251 | + mkdirp.sync(conf.stat.path); | |
| 252 | + } | |
| 253 | + streamTask['stt'].push(createStream('stt')); | |
| 254 | + } | |
| 255 | + if (conf.log.console) streamTask['stt'].push(process.stdout); | |
| 256 | + } | |
| 257 | + if (conf.summary) { | |
| 258 | + if (conf.log.file){ | |
| 259 | + if (!fs.existsSync(conf.summary.path)) { | |
| 260 | + // fs.mkdirSync(conf.summary.path); | |
| 261 | + mkdirp.sync(conf.summary.path); | |
| 262 | + } | |
| 263 | + streamTask['smr'].push(createStream('smr')); | |
| 264 | + } | |
| 265 | + if (conf.log.console) streamTask['smr'].push(process.stdout); | |
| 266 | + } | |
| 267 | + if (conf.detail) { | |
| 268 | + if (conf.log.file){ | |
| 269 | + if (!fs.existsSync(conf.detail.path)) { | |
| 270 | + // fs.mkdirSync(conf.detail.path); | |
| 271 | + mkdirp.sync(conf.detail.path); | |
| 272 | + } | |
| 273 | + streamTask['dtl'].push(createStream('dtl')); | |
| 274 | + } | |
| 275 | + if (conf.log.console) streamTask['dtl'].push(process.stdout); | |
| 276 | + } | |
| 277 | +} | |
| 278 | + | |
| 279 | +function addStat(stat_name) { | |
| 280 | + db.serialize(function () { | |
| 281 | + //db.run("CREATE TABLE lorem (info TEXT, xx TEXT)"); | |
| 282 | + /*var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); | |
| 283 | + for (var i = 0; i < 10; i++) { | |
| 284 | + stmt.run("Ipsum " + i); | |
| 285 | + } | |
| 286 | + stmt.finalize(); | |
| 287 | + | |
| 288 | + db.run("INSERT INTO stat(stat_time, stat_name) VALUES (?)", ['xx',stat_name], function() { | |
| 289 | + console.log( 'x' ); | |
| 290 | + getStat(); | |
| 291 | + });*/ | |
| 292 | + | |
| 293 | + /*db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name], function() { | |
| 294 | + getStat(); | |
| 295 | + });*/ | |
| 296 | + | |
| 297 | + db.run("INSERT INTO stat VALUES (?,?)", [dateFormat(new Date(), dateFMT_SQL), stat_name]); | |
| 298 | + | |
| 299 | + /*db.run("INSERT INTO stat VALUES (DATETIME('now'), '"+stat_name+"')", function() { | |
| 300 | + console.log( 'x' ); | |
| 301 | + getStat(); | |
| 302 | + });*/ | |
| 303 | + | |
| 304 | + | |
| 305 | + }); | |
| 306 | + | |
| 307 | +} | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | +//var prevStatDateRange; | |
| 312 | +function fushStat() { | |
| 313 | + var end = new Date(); | |
| 314 | + end.setMilliseconds(0); | |
| 315 | + | |
| 316 | + //var start = new Date(end.getTime()); | |
| 317 | + //start.setMinutes(end.getMinutes() - conf.log.statTime); | |
| 318 | + | |
| 319 | + var en = dateFormat(end, dateFMT_SQL); | |
| 320 | + //var st = dateFormat(prevStatDateRange, dateFMT_SQL); | |
| 321 | + | |
| 322 | + //prevStatDateRange = end; //assign for next loop | |
| 323 | + //console.log(st + '|' + en); | |
| 324 | + //var path = getStatFileName(end); | |
| 325 | + | |
| 326 | + db.serialize(function () { | |
| 327 | + //var stream = fs.createWriteStream(path, { 'flags': 'a' }); | |
| 328 | + write('stt', en); | |
| 329 | + | |
| 330 | + 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) { | |
| 331 | + if (err) { | |
| 332 | + console.log(err); | |
| 333 | + return; | |
| 334 | + } | |
| 335 | + //console.log(path+' '+ row.stat_name + ' ' + row.count ); | |
| 336 | + write('stt', row.stat_name + ' ' + row.count); | |
| 337 | + }); | |
| 338 | + | |
| 339 | + db.run("DELETE FROM stat WHERE stat_time < ?", [en]); | |
| 340 | + | |
| 341 | + /*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) { | |
| 342 | + if (err) { | |
| 343 | + console.log(err); | |
| 344 | + return; | |
| 345 | + } | |
| 346 | + //console.log(path+' '+ row.stat_name + ' ' + row.count ); | |
| 347 | + write(end, 'stt', row.stat_name + ' ' + row.count, false); | |
| 348 | + }); | |
| 349 | + | |
| 350 | + db.run("DELETE FROM stat WHERE stat_time BETWEEN ? AND ?", [st, en]);*/ | |
| 351 | + | |
| 352 | + }); | |
| 353 | +} | |
| 354 | + | |
| 355 | +function getIntervalTime(type) { | |
| 356 | + if (type === 'app') { | |
| 357 | + return conf.log.time; | |
| 358 | + } else if (type === 'stt') { | |
| 359 | + return conf.stat.time; | |
| 360 | + } else if (type === 'smr') { | |
| 361 | + return conf.summary.time; | |
| 362 | + } else if (type === 'dtl') { | |
| 363 | + return conf.detail.time; | |
| 364 | + } | |
| 365 | + return null; | |
| 366 | +} | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | +log.stat = function (stat_name) { | |
| 371 | + addStat(stat_name); | |
| 372 | +} | |
| 373 | + | |
| 374 | +log.debug = function (..._log) { | |
| 375 | + if (conf.log.level > 0) return; | |
| 376 | + write('app', processApplog('debug', ..._log)); | |
| 377 | +} | |
| 378 | + | |
| 379 | +log.info = function (..._log) { | |
| 380 | + if (conf.log.level > 1) return; | |
| 381 | + write('app', processApplog('info', ..._log)); | |
| 382 | +} | |
| 383 | +log.warn = function (..._log) { | |
| 384 | + if (conf.log.level > 2) return; | |
| 385 | + write('app', processApplog('warn', ..._log)); | |
| 386 | +} | |
| 387 | +log.error = function (..._log) { | |
| 388 | + if (conf.log.level > 3) return; | |
| 389 | + write('app', processApplog('error', ..._log)); | |
| 390 | +} | |
| 391 | + | |
| 392 | +log.detail = function (detailLog) { | |
| 393 | + write('dtl', detailLog); | |
| 394 | +} | |
| 395 | + | |
| 396 | + | |
| 397 | +// log.detail = function (session, scenario, identity) { | |
| 398 | +// var startTimeDate = new Date(); | |
| 399 | +// var inputTime; | |
| 400 | +// var outputTime; | |
| 401 | + | |
| 402 | +// var detailLog = { | |
| 403 | +// Session: session, | |
| 404 | +// InitInvoke: '', //only equinox platform | |
| 405 | +// Scenario: scenario, | |
| 406 | +// Identity: identity, | |
| 407 | +// InputTimeStamp: null, | |
| 408 | +// Input: [], | |
| 409 | +// OutputTimeStamp: null, | |
| 410 | +// Output: [], | |
| 411 | +// ProcessingTime: null, | |
| 412 | +// addInputRequest: function (node, cmd, rawData, data) { | |
| 413 | +// this.addInput(node, cmd, 'REQ', rawData, data); | |
| 414 | +// }, | |
| 415 | +// addInputResponse: function (node, cmd, rawData, data, resTime) { | |
| 416 | +// this.addInput(node, cmd, 'RES', rawData, data, resTime); | |
| 417 | +// }, | |
| 418 | +// addInputResponseTimeout: function (node, cmd) { | |
| 419 | +// this.addInput(node, cmd, 'RES_TIMEOUT'); | |
| 420 | +// }, | |
| 421 | +// addInputResponseError: function (node, cmd) { | |
| 422 | +// this.addInput(node, cmd, 'RES_ERROR'); | |
| 423 | +// }, | |
| 424 | +// addInput: function (node, cmd, type, rawData, data, resTime) { | |
| 425 | +// inputTime = new Date(); | |
| 426 | +// if (typeof resTime === 'number') { | |
| 427 | +// resTime = resTime.toLocaleString() + ' ms'; | |
| 428 | +// } | |
| 429 | +// var input = { | |
| 430 | +// Invoke: null, | |
| 431 | +// Event: node + '.' + cmd, | |
| 432 | +// Type: type, | |
| 433 | +// RawData: rawData, | |
| 434 | +// Data: data, | |
| 435 | +// ResTime: resTime | |
| 436 | +// }; | |
| 437 | +// this.Input.push(input); | |
| 438 | +// }, | |
| 439 | + | |
| 440 | +// addOutputRequest: function (node, cmd, rawData, data) { | |
| 441 | +// this.addOutput(node, cmd, 'REQ', rawData, data); | |
| 442 | +// }, | |
| 443 | +// addOutputResponse: function (node, cmd, rawData, data) { | |
| 444 | +// this.addOutput(node, cmd, 'RES', rawData, data); | |
| 445 | +// }, | |
| 446 | +// addOutputRequestRetry: function (node, cmd, rawData, data, total, maxCount) { | |
| 447 | +// this.addOutput(node, cmd, ('REQ_RETRY_' + total + '/' + maxCount), rawData, data); | |
| 448 | +// }, | |
| 449 | +// addOutput: function (node, cmd, type, rawData, data) { | |
| 450 | +// outputTime = new Date(); | |
| 451 | +// var input = { | |
| 452 | +// Invoke: null, | |
| 453 | +// Event: node + '.' + cmd, | |
| 454 | +// Type: type, | |
| 455 | +// RawData: rawData, | |
| 456 | +// Data: data | |
| 457 | +// }; | |
| 458 | + | |
| 459 | +// this.Output.push(input); | |
| 460 | +// }, | |
| 461 | + | |
| 462 | +// end: function () { | |
| 463 | +// let currentTime = new Date(); | |
| 464 | +// detailLog.ProcessingTime = new Date().getTime() - startTimeDate.getTime(); | |
| 465 | +// detailLog.InputTimeStamp = dateFormat(inputTime, dateFMT); | |
| 466 | +// detailLog.OutputTimeStamp = dateFormat(outputTime, dateFMT);; | |
| 467 | + | |
| 468 | +// write('dtl', JSON.stringify(detailLog)); | |
| 469 | +// startTimeDate = currentTime; | |
| 470 | +// detailLog._clr(); | |
| 471 | +// }, | |
| 472 | + | |
| 473 | +// _clr: function (){ | |
| 474 | +// detailLog.ProcessingTime = null; | |
| 475 | +// detailLog.InputTimeStamp = null; | |
| 476 | +// detailLog.OutputTimeStamp= null; | |
| 477 | +// detailLog.Input=[]; | |
| 478 | +// detailLog.Output=[]; | |
| 479 | +// } | |
| 480 | +// } | |
| 481 | + | |
| 482 | +// return detailLog; | |
| 483 | +// } | |
| 484 | + | |
| 485 | +log.summary = function (session, cmd, identity) { | |
| 486 | + var now = new Date(); | |
| 487 | + | |
| 488 | + var summaryLog = { | |
| 489 | + requestTime: now, | |
| 490 | + session: session, | |
| 491 | + initInvoke: '', //only equinox platform | |
| 492 | + cmd: cmd, | |
| 493 | + identity: identity, | |
| 494 | + /*resultCode: null, | |
| 495 | + resultDescription: null,*/ | |
| 496 | + blockDetail: [], | |
| 497 | + | |
| 498 | + addSuccessBlock: function (node, cmd, resultCode, resultDesc) { | |
| 499 | + //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); | |
| 500 | + addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); | |
| 501 | + }, | |
| 502 | + | |
| 503 | + addErrorBlock: function (node, cmd, resultCode, resultDesc) { | |
| 504 | + //this.blockDetail.push('['+ node+'; '+cmd+'(1); ['+ resultCode+'; '+resultDesc+'(1)]]'); | |
| 505 | + addBlock(this.blockDetail, node, cmd, resultCode, resultDesc); | |
| 506 | + }, | |
| 507 | + | |
| 508 | + end: function (resultCode, resultDescription) { | |
| 509 | + var endTime = new Date(); | |
| 510 | + | |
| 511 | + var blockDetailTxt = '['; | |
| 512 | + for (var j = 0; j < this.blockDetail.length; j++) { | |
| 513 | + var i = this.blockDetail[j]; | |
| 514 | + var aa = i.node + '; ' + i.cmd + '(' + i.count + '); ['; | |
| 515 | + | |
| 516 | + for (var k = 0; k < i.result.length; k++) { | |
| 517 | + var bb = i.result[k].resultCode + '; ' + i.result[k].resultDesc + '(' + i.result[k].count + ')'; | |
| 518 | + | |
| 519 | + if (k === i.result.length - 1) { | |
| 520 | + aa += bb; | |
| 521 | + } else { | |
| 522 | + aa += (bb + ', '); | |
| 523 | + } | |
| 524 | + } | |
| 525 | + | |
| 526 | + if (j === this.blockDetail.length - 1) { | |
| 527 | + aa += ']'; | |
| 528 | + } else { | |
| 529 | + aa += '], '; | |
| 530 | + } | |
| 531 | + blockDetailTxt += aa; | |
| 532 | + } | |
| 533 | + blockDetailTxt += ']'; | |
| 534 | + | |
| 535 | + | |
| 536 | + var txt = dateFormat(this.requestTime, dateFMT) + '|'; | |
| 537 | + txt += this.session + '|'; | |
| 538 | + txt += this.initInvoke + '|' | |
| 539 | + txt += this.cmd + '|' | |
| 540 | + txt += this.identity + '|' | |
| 541 | + txt += resultCode + '|' | |
| 542 | + txt += resultDescription + '|' | |
| 543 | + txt += blockDetailTxt + '|' | |
| 544 | + txt += dateFormat(endTime, dateFMT) + '|' | |
| 545 | + txt += (endTime.getTime() - this.requestTime.getTime()); | |
| 546 | + | |
| 547 | + write('smr', txt); | |
| 548 | + } | |
| 549 | + }; | |
| 550 | + return summaryLog; | |
| 551 | +} | |
| 552 | + | |
| 553 | +function addBlock(store, node, cmd, resultCode, resultDesc) { | |
| 554 | + var found = null; | |
| 555 | + | |
| 556 | + for (var i = 0; i < store.length; i++) { | |
| 557 | + if (store[i].node === node && store[i].cmd === cmd) { | |
| 558 | + found = store[i]; | |
| 559 | + store[i].count++; | |
| 560 | + break; | |
| 561 | + } | |
| 562 | + } | |
| 563 | + | |
| 564 | + if (!found) { | |
| 565 | + var result = { | |
| 566 | + resultCode: resultCode, | |
| 567 | + resultDesc: resultDesc, | |
| 568 | + count: 1 | |
| 569 | + } | |
| 570 | + | |
| 571 | + var b = { | |
| 572 | + node: node, | |
| 573 | + cmd: cmd, | |
| 574 | + count: 1, | |
| 575 | + result: [result] | |
| 576 | + } | |
| 577 | + store.push(b); | |
| 578 | + | |
| 579 | + } else { | |
| 580 | + var foundResult = false; | |
| 581 | + for (var j = 0; j < found.result.length; j++) { | |
| 582 | + if (found.result[j].resultCode === resultCode && | |
| 583 | + found.result[j].resultDesc === resultDesc) { | |
| 584 | + | |
| 585 | + found.result[j].count++; | |
| 586 | + foundResult = true; | |
| 587 | + break; | |
| 588 | + } | |
| 589 | + } | |
| 590 | + | |
| 591 | + if (!foundResult) { | |
| 592 | + var result = { | |
| 593 | + resultCode: resultCode, | |
| 594 | + resultDesc: resultDesc, | |
| 595 | + count: 1 | |
| 596 | + } | |
| 597 | + found.result.push(result); | |
| 598 | + } | |
| 599 | + } | |
| 600 | +} | |
| 601 | + | |
| 602 | +log.init = function (_conf, express) { | |
| 603 | + log.initLog = true; | |
| 604 | + if (_conf) { | |
| 605 | + conf = _conf; | |
| 606 | + } | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + if (conf.log) { | |
| 611 | + if (conf.log.level === 'debug') { | |
| 612 | + conf.log.level = 0; | |
| 613 | + } else if (conf.log.level === 'info') { | |
| 614 | + conf.log.level = 1; | |
| 615 | + } else if (conf.log.level === 'warn') { | |
| 616 | + conf.log.level = 2; | |
| 617 | + } else if (conf.log.level === 'error') { | |
| 618 | + conf.log.level = 3; | |
| 619 | + } else { | |
| 620 | + conf.log.level = 4; | |
| 621 | + } | |
| 622 | + | |
| 623 | + //enable only debug | |
| 624 | + if (express && conf.log.level === 0) { | |
| 625 | + express.use(function (req, res, next) { | |
| 626 | + req._reqTimeForLog = Date.now(); | |
| 627 | + let sid; | |
| 628 | + if(typeof log.sessionID === 'function'){ | |
| 629 | + sid = log.sessionID(req, res); | |
| 630 | + } | |
| 631 | + | |
| 632 | + let txtLogReq = 'INCOMING|__Method=' +req.method | |
| 633 | + + ' __URL=' + req.url | |
| 634 | + + ' __Headers=' + JSON.stringify(req.headers?req.headers:"{}" ) | |
| 635 | + //+ ' __Params=' + JSON.stringify(req.params ) | |
| 636 | + + ' __Body=' + JSON.stringify(req.body?req.body:"{}"); | |
| 637 | + | |
| 638 | + if(sid){ | |
| 639 | + log.debug(sid, txtLogReq); | |
| 640 | + }else{ | |
| 641 | + log.debug(txtLogReq); | |
| 642 | + } | |
| 643 | + | |
| 644 | + onHeaders(res, ()=>{ | |
| 645 | + let txtLogRes = 'OUTGOING|__Statuscode=' + res.statusCode | |
| 646 | + + ' __Headers=' + JSON.stringify(res._headers ) | |
| 647 | + + ' __Body=' + req.res.resBody | |
| 648 | + + ' __Restime=' + ( Date.now() - req._reqTimeForLog ); | |
| 649 | + | |
| 650 | + if(sid){ | |
| 651 | + log.debug(sid, txtLogRes); | |
| 652 | + }else{ | |
| 653 | + log.debug(txtLogRes); | |
| 654 | + } | |
| 655 | + }); | |
| 656 | + next(); | |
| 657 | + }); | |
| 658 | + } | |
| 659 | + } | |
| 660 | + | |
| 661 | + initLog(); | |
| 662 | + | |
| 663 | + if (conf.stat) { | |
| 664 | + initDB(); | |
| 665 | + cron.schedule('*/' + conf.stat.statInterval + ' * * * *', function () { | |
| 666 | + fushStat(); | |
| 667 | + }); | |
| 668 | + } | |
| 669 | + return log; | |
| 670 | +}; | |
| 671 | + | |
| 672 | +module.exports = log; | |
| 673 | +// module.exports = commonLog(); | ... | ... |
| ... | ... | @@ -0,0 +1,28 @@ |
| 1 | + | |
| 2 | +const nodeEnv = process.env.NODE_ENV || 'default'; | |
| 3 | + | |
| 4 | +// setup config | |
| 5 | +const NodeJsonConfig = require('node-json-config'); | |
| 6 | +const conf = new NodeJsonConfig('app.config.json'); | |
| 7 | + | |
| 8 | +let defaultConfObj = conf.get('default'); | |
| 9 | +// console.log('defaultConfObj'); | |
| 10 | +// console.log(defaultConfObj); | |
| 11 | + | |
| 12 | +let envConfObj = conf.get(nodeEnv); | |
| 13 | +// console.log('envConfObj'); | |
| 14 | +// console.log(envConfObj); | |
| 15 | + | |
| 16 | +let currentConfObj = Object.assign(defaultConfObj, envConfObj); | |
| 17 | + | |
| 18 | +// console.log('currentConfObj'); | |
| 19 | +// console.log(currentConfObj); | |
| 20 | + | |
| 21 | +conf.put(nodeEnv, currentConfObj); | |
| 22 | + | |
| 23 | +conf.getOld = conf.get; | |
| 24 | +conf.get = function(configName) { | |
| 25 | + return conf.getOld(nodeEnv + '.' + configName); | |
| 26 | +}; | |
| 27 | + | |
| 28 | +module.exports = conf; | |
| 0 | 29 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,90 @@ |
| 1 | +module.exports = Object.freeze({ | |
| 2 | + REQUEST: 'Request', | |
| 3 | + RESPONSE: 'Response', | |
| 4 | + SUCCESS: 'Success', | |
| 5 | + ERROR: 'Error', | |
| 6 | + FAIL: 'Fail', | |
| 7 | + URLKEYWORD : ["filter","offset","orderby","limit","fields"], | |
| 8 | + METHOD:{ | |
| 9 | + GET : 'GET', | |
| 10 | + POST : 'POST', | |
| 11 | + PUT : 'PUT', | |
| 12 | + DELETE : 'DELETE' | |
| 13 | + }, | |
| 14 | + RESPONSECONN:{ | |
| 15 | + STATS:{ | |
| 16 | + ETIMEDOUT:'ETIMEDOUT', | |
| 17 | + ECONNREFUSED : 'ECONNREFUSED', | |
| 18 | + ESOCKETTIMEDOUT : 'ESOCKETTIMEDOUT', | |
| 19 | + MISSING : 'Missing' | |
| 20 | + }, | |
| 21 | + MESSAGE:{ | |
| 22 | + ETIMEDOUT:{ | |
| 23 | + resultCode : 'null', | |
| 24 | + resultDescription : 'Time out' | |
| 25 | + }, | |
| 26 | + ECONNREFUSED:{ | |
| 27 | + resultCode : 'null', | |
| 28 | + resultDescription : 'Reject' | |
| 29 | + }, | |
| 30 | + ESOCKETTIMEDOUT:{ | |
| 31 | + resultCode : 'null', | |
| 32 | + resultDescription : 'Time out' | |
| 33 | + }, | |
| 34 | + EOTHERERROR:{ | |
| 35 | + resultCode : 'null', | |
| 36 | + resultDescription : 'Error' | |
| 37 | + }, | |
| 38 | + ERROR:{ | |
| 39 | + resultCode : '50000', | |
| 40 | + resultDescription : 'System error' | |
| 41 | + }, | |
| 42 | + SUCCESS:{ | |
| 43 | + resultCode : '20000', | |
| 44 | + resultDescription : 'Success' | |
| 45 | + }, | |
| 46 | + EXCEED:{ | |
| 47 | + resultCode : '40302', | |
| 48 | + resultDescription : 'Exceed data allowances' | |
| 49 | + }, | |
| 50 | + DBERROR:{ | |
| 51 | + resultCode : '50001', | |
| 52 | + resultDescription : 'Database error' | |
| 53 | + }, | |
| 54 | + SUCCESSWITHCON:{ | |
| 55 | + resultCode : '20001', | |
| 56 | + resultDescription : 'Success with condition' | |
| 57 | + } | |
| 58 | + } | |
| 59 | + }, | |
| 60 | + RESPONSERESULT:{ | |
| 61 | + SUCCESS:{ | |
| 62 | + resultCode : '20000', | |
| 63 | + resultDescription : 'Success' | |
| 64 | + }, | |
| 65 | + MISSING_INVALID:{ | |
| 66 | + resultCode : '40300', | |
| 67 | + resultDescription : 'Missing or invalid parameter' | |
| 68 | + }, | |
| 69 | + DATA_NOT_FOUND:{ | |
| 70 | + resultCode : '40401', | |
| 71 | + resultDescription : 'Data Not Found' | |
| 72 | + }, | |
| 73 | + DATA_EXIST:{ | |
| 74 | + resultCode : '40301', | |
| 75 | + resultDescription : 'Data Exist' | |
| 76 | + }, | |
| 77 | + ERROR:{ | |
| 78 | + resultCode : '50000', | |
| 79 | + resultDescription : 'System error' | |
| 80 | + }, | |
| 81 | + DENIED:{ | |
| 82 | + resultCode : '40100', | |
| 83 | + resultDescription : 'Access Denied' | |
| 84 | + }, | |
| 85 | + UNKNOW:{ | |
| 86 | + resultCode : '50060', | |
| 87 | + resultDescription : 'Unknown URL' | |
| 88 | + } | |
| 89 | + } | |
| 90 | +}); | |
| 0 | 91 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,39 @@ |
| 1 | +var constants = require('./constants'); | |
| 2 | + | |
| 3 | +findCmdfromMethod = function (method) | |
| 4 | +{ | |
| 5 | + var cmd = ""; | |
| 6 | + switch (method) { | |
| 7 | + case constants.METHOD.GET: | |
| 8 | + cmd = 'QUERY'; | |
| 9 | + break; | |
| 10 | + case constants.METHOD.POST: | |
| 11 | + cmd = "INSERT" | |
| 12 | + break; | |
| 13 | + case constants.METHOD.PUT: | |
| 14 | + cmd = 'UPDATE'; | |
| 15 | + break; | |
| 16 | + case constants.METHOD.DELETE: | |
| 17 | + cmd = 'DELETE'; | |
| 18 | + break; | |
| 19 | + } | |
| 20 | + return cmd; | |
| 21 | +} | |
| 22 | + | |
| 23 | +undefinedToNull = function(data){ | |
| 24 | + if(!data){ | |
| 25 | + data = 'null'; | |
| 26 | + } else if(data.includes('undefined')){ | |
| 27 | + data = data.replace(/undefined/g,'null'); | |
| 28 | + } else { | |
| 29 | + data = data; | |
| 30 | + } | |
| 31 | + | |
| 32 | + return data; | |
| 33 | +} | |
| 34 | + | |
| 35 | + | |
| 36 | +module.exports = { | |
| 37 | + findCmdfromMethod, | |
| 38 | + undefinedToNull | |
| 39 | +}; | |
| 0 | 40 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,265 @@ |
| 1 | +var cfg = require('./config'); | |
| 2 | +var conf = cfg.get("configlog"); | |
| 3 | +conf.projectName = cfg.get("appName"); | |
| 4 | +var logg = require('./commonlog'); | |
| 5 | +var constants = require('./constants'); | |
| 6 | +var helper = require('./helper'); | |
| 7 | + | |
| 8 | +var logger = []; | |
| 9 | + | |
| 10 | +const mapLog = {}; | |
| 11 | + | |
| 12 | +logger.startlog = function (req,cmdData,session,identity) | |
| 13 | +{ | |
| 14 | + | |
| 15 | + | |
| 16 | + var start = { | |
| 17 | + session : helper.undefinedToNull(session), | |
| 18 | + cmd : cmdData, | |
| 19 | + identity : helper.undefinedToNull(identity) | |
| 20 | + }; | |
| 21 | + | |
| 22 | + mapLog[req.id].start = start; | |
| 23 | +} | |
| 24 | + | |
| 25 | +logger.stat = function (msg){ | |
| 26 | + logg.stat(msg); | |
| 27 | +} | |
| 28 | + | |
| 29 | +logger.log = function (msg,type) | |
| 30 | +{ | |
| 31 | + | |
| 32 | + if(!type) | |
| 33 | + type = 'debug'; | |
| 34 | + | |
| 35 | + type = type.toLowerCase(); | |
| 36 | + | |
| 37 | + switch(type) { | |
| 38 | + case 'debug': | |
| 39 | + logg.debug(msg); | |
| 40 | + break; | |
| 41 | + case 'info': | |
| 42 | + logg.info(msg); | |
| 43 | + break; | |
| 44 | + case 'warn': | |
| 45 | + logg.warn(msg); | |
| 46 | + break; | |
| 47 | + case 'error': | |
| 48 | + logg.error(msg); | |
| 49 | + break; | |
| 50 | + default: | |
| 51 | + logg.debug(msg); | |
| 52 | + } | |
| 53 | + | |
| 54 | + console.log(msg); | |
| 55 | +} | |
| 56 | + | |
| 57 | +logger.addSuccessSummary = function (req,nodeData,cmdData,result) | |
| 58 | +{ | |
| 59 | + summary = { | |
| 60 | + node : helper.undefinedToNull(nodeData), | |
| 61 | + cmd : cmdData, | |
| 62 | + resultCode : result.resultCode, | |
| 63 | + resultDescription : result.resultDescription, | |
| 64 | + type : constants.SUCCESS | |
| 65 | + }; | |
| 66 | + mapLog[req.id].summaryList.push(summary); | |
| 67 | +} | |
| 68 | + | |
| 69 | +logger.addErrorSummary = function (req,nodeData,cmdData,result) | |
| 70 | +{ | |
| 71 | + summary = { | |
| 72 | + node : helper.undefinedToNull(nodeData), | |
| 73 | + cmd : cmdData, | |
| 74 | + resultCode : result.resultCode, | |
| 75 | + resultDescription : result.resultDescription, | |
| 76 | + type : constants.ERROR | |
| 77 | + }; | |
| 78 | + | |
| 79 | + mapLog[req.id].summaryList.push(summary); | |
| 80 | +} | |
| 81 | + | |
| 82 | +logger.logSummary = function (req,response) | |
| 83 | +{ | |
| 84 | + | |
| 85 | + var start = mapLog[req.id].start; | |
| 86 | + var summaryList = mapLog[req.id].summaryList; | |
| 87 | + | |
| 88 | + var log = logg.summary(start.session,start.cmd,start.identity); | |
| 89 | + var row; | |
| 90 | + for (let index = 0; index < summaryList.length; index++) { | |
| 91 | + row = summaryList[index]; | |
| 92 | + if(row.type === constants.SUCCESS){ | |
| 93 | + log.addSuccessBlock(row.node,row.cmd,row.resultCode,row.resultDescription); | |
| 94 | + } else { | |
| 95 | + log.addErrorBlock(row.node,row.cmd,helper.undefinedToNull(row.resultCode),helper.undefinedToNull(row.resultDescription)); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + if(response){ | |
| 100 | + log.end(response.resultCode,response.resultDescription) | |
| 101 | + } else { | |
| 102 | + log.end() | |
| 103 | + } | |
| 104 | +} | |
| 105 | + | |
| 106 | +logger.startEC = function(req) | |
| 107 | +{ | |
| 108 | + var objectLog = { | |
| 109 | + summaryList : [], | |
| 110 | + start : {}, | |
| 111 | + detail : {}, | |
| 112 | + startTime : new Date() | |
| 113 | + } | |
| 114 | + | |
| 115 | + | |
| 116 | + mapLog[req.id] = objectLog; | |
| 117 | + | |
| 118 | + var newLine = "\r\n"; | |
| 119 | + var messageRes = ''; | |
| 120 | + var startTime = mapLog[req.id].startTime; | |
| 121 | + | |
| 122 | + | |
| 123 | + if(req.body) | |
| 124 | + { | |
| 125 | + if(typeof req.body === 'string') | |
| 126 | + body = req.bod; | |
| 127 | + else | |
| 128 | + body = JSON.stringify(req.body); | |
| 129 | + } | |
| 130 | + | |
| 131 | + var header = ""; | |
| 132 | + if(req.headers) | |
| 133 | + { | |
| 134 | + if(typeof req.headers === 'string') | |
| 135 | + header = req.headers; | |
| 136 | + else | |
| 137 | + header = JSON.stringify(req.headers); | |
| 138 | + } | |
| 139 | + | |
| 140 | + messageRes += newLine+newLine+"#====> Incoming Message <====# "; | |
| 141 | + messageRes += newLine+"Session : "+req.id; | |
| 142 | + messageRes += newLine+"Url : "+req.url; | |
| 143 | + messageRes += newLine+"Method : "+req.method; | |
| 144 | + messageRes += newLine+"Header : "+header; | |
| 145 | + messageRes += newLine+"Body : "+body; | |
| 146 | + messageRes += newLine+"Time in : "+startTime.toISOString(); | |
| 147 | + messageRes += newLine | |
| 148 | + | |
| 149 | + // logg.debug(messageRes); | |
| 150 | +} | |
| 151 | + | |
| 152 | +logger.endEC = function(req,res) | |
| 153 | +{ | |
| 154 | + | |
| 155 | + var newLine = "\r\n"; | |
| 156 | + var messageRes = ''; | |
| 157 | + var startTime = mapLog[req.id].startTime; | |
| 158 | + var endTime = new Date(); | |
| 159 | + | |
| 160 | + var body = ""; | |
| 161 | + if(res.resBody) | |
| 162 | + { | |
| 163 | + if(typeof res.resBody === 'string') | |
| 164 | + body = res.resBody; | |
| 165 | + else | |
| 166 | + body = JSON.stringify(res.resBody); | |
| 167 | + } | |
| 168 | + | |
| 169 | + var header = ""; | |
| 170 | + if(res.req.headers) | |
| 171 | + { | |
| 172 | + if(typeof res.req.headers === 'string') | |
| 173 | + header = res.req.headers; | |
| 174 | + else | |
| 175 | + header = JSON.stringify(res.req.headers); | |
| 176 | + } | |
| 177 | + | |
| 178 | + messageRes += newLine+newLine+"#====> Outgoing Message <====# "; | |
| 179 | + messageRes += newLine+"Session : "+req.id; | |
| 180 | + messageRes += newLine+"Header : "+header; | |
| 181 | + messageRes += newLine+"Body : "+body; | |
| 182 | + messageRes += newLine+"Time out : "+endTime.toISOString(); | |
| 183 | + messageRes += newLine+"Use times : "+Math.abs(endTime - startTime)+" ms"; | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + // logg.debug(messageRes); | |
| 189 | + messageRes += newLine | |
| 190 | +} | |
| 191 | + | |
| 192 | +//detail | |
| 193 | +logger.detailRequestFE = function(req) | |
| 194 | +{ | |
| 195 | + // console.log(req); | |
| 196 | + var protocal = "HTTP"; | |
| 197 | + var method = req.method; | |
| 198 | + var url = req.headers.host+req.originalUrl; | |
| 199 | + var body = JSON.stringify(req.body); | |
| 200 | + // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); | |
| 201 | + logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); | |
| 202 | + | |
| 203 | +} | |
| 204 | + | |
| 205 | +logger.detailResponseFE = function(req) | |
| 206 | +{ | |
| 207 | + var protocal = "HTTP"; | |
| 208 | + var method = req.method; | |
| 209 | + var url = req.headers.host+req.originalUrl; | |
| 210 | + var body = JSON.stringify(req.body); | |
| 211 | + // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); | |
| 212 | + logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); | |
| 213 | +} | |
| 214 | + | |
| 215 | +//mongo | |
| 216 | +logger.detailSqlQuery = function(req,method,collection,data) | |
| 217 | +{ | |
| 218 | + // logger4jDetail.info(logPrefix(req)+"BE Send Mongo Collection: \""+collection+"\" Query: "+JSON.stringify(data)); | |
| 219 | + logg.detail(logPrefix(req)+"BE Send Mongo Collection: "+collection+" Method:"+helper.findCmdfromMethod(method)+" Query: "+JSON.stringify(data)); | |
| 220 | +} | |
| 221 | + | |
| 222 | +logger.detailSqlResult = function(req,collection,data) | |
| 223 | +{ | |
| 224 | + // logger4jDetail.info(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); | |
| 225 | + logg.detail(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); | |
| 226 | + | |
| 227 | +} | |
| 228 | + | |
| 229 | +logger.detailRequestBE = function(req,data) | |
| 230 | +{ | |
| 231 | + var protocal = data.protocal; | |
| 232 | + var method = data.method; | |
| 233 | + var url = data.url; | |
| 234 | + var header = JSON.stringify(data.header); | |
| 235 | + var body = JSON.stringify(data.body); | |
| 236 | + // logger4jDetail.info(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); | |
| 237 | + logg.detail(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); | |
| 238 | +} | |
| 239 | + | |
| 240 | +logger.detailResponseBE = function(req,data) | |
| 241 | +{ | |
| 242 | + var protocal = data.protocal; | |
| 243 | + var method = data.method; | |
| 244 | + var url = data.url; | |
| 245 | + var header = JSON.stringify(data.header); | |
| 246 | + var body = JSON.stringify(data.body); | |
| 247 | + // logger4jDetail.info(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); | |
| 248 | + logg.detail(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); | |
| 249 | +} | |
| 250 | + | |
| 251 | +//private | |
| 252 | +function logPrefix(req) | |
| 253 | +{ | |
| 254 | + | |
| 255 | + var userName = "userName"; | |
| 256 | + var sessionID = "sessionID"; | |
| 257 | + var session = "session"; | |
| 258 | + | |
| 259 | + return ":: ## "+userName+" - "+sessionID+" - "+session+ " ## "; | |
| 260 | + | |
| 261 | +} | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | +module.exports = logger; | ... | ... |
| ... | ... | @@ -0,0 +1,51 @@ |
| 1 | +var config = require('./config'); | |
| 2 | +var log = require('./log.js'); | |
| 3 | +var helper = require('./helper'); | |
| 4 | + | |
| 5 | + | |
| 6 | +var nodeName = config.get("appName"); | |
| 7 | +var stat = []; | |
| 8 | + | |
| 9 | + | |
| 10 | +//client and Node | |
| 11 | +stat.receiveRequest = function (method,cmd){ | |
| 12 | + writeStats(nodeName+" Receive "+method+" "+cmd+" Request"); | |
| 13 | +}; | |
| 14 | + | |
| 15 | +stat.receiveBadRequest = function (method,cmd){ | |
| 16 | + writeStats(nodeName+" Receive Bad "+method+" "+cmd+" Request"); | |
| 17 | +}; | |
| 18 | + | |
| 19 | +stat.sendResponse = function (method,cmd,result){ | |
| 20 | + writeStats(nodeName+" Send "+method+" "+cmd+" Response "+(result?result:"Success")); | |
| 21 | +}; | |
| 22 | + | |
| 23 | +//node and mongo | |
| 24 | +stat.sendQuery = function (method,collection){ | |
| 25 | + writeStats(nodeName+" Send "+helper.findCmdfromMethod(method)+" "+collection+" Request"); | |
| 26 | +}; | |
| 27 | + | |
| 28 | +stat.receiveBadQuery = function (method,collection){ | |
| 29 | + writeStats(nodeName+" Receive Bad "+helper.findCmdfromMethod(method)+" "+collection+" Response"); | |
| 30 | +}; | |
| 31 | + | |
| 32 | +stat.receiveQuery = function (method,collection){ | |
| 33 | + writeStats(nodeName+" Receive "+helper.findCmdfromMethod(method)+" "+collection+" Response"); | |
| 34 | +}; | |
| 35 | + | |
| 36 | +//unknow | |
| 37 | +stat.receiveUnknow = function(){ | |
| 38 | + writeStats(nodeName+" Receive Unknown Error Request"); | |
| 39 | +}; | |
| 40 | + | |
| 41 | +stat.sendUnknow = function(){ | |
| 42 | + writeStats(nodeName+" Send Unknown Error Response"); | |
| 43 | +}; | |
| 44 | + | |
| 45 | +function writeStats(string) { | |
| 46 | + log.log(string); | |
| 47 | + log.stat(string); | |
| 48 | +} | |
| 49 | + | |
| 50 | + | |
| 51 | +module.exports = stat; | |
| 0 | 52 | \ No newline at end of file | ... | ... |
utils/log.js
| ... | ... | @@ -1,273 +0,0 @@ |
| 1 | -var cfg = require('./config'); | |
| 2 | -var utils = require('./utils'); | |
| 3 | -var conf = cfg.get("configlog"); | |
| 4 | -conf.projectName = cfg.get("appName"); | |
| 5 | -var logg = require('./commonlog'); | |
| 6 | -var constants = require('./constants'); | |
| 7 | -var utils = require('./utils'); | |
| 8 | - | |
| 9 | - | |
| 10 | -// var log4js = require('log4js'); | |
| 11 | -// var appenders = {}; | |
| 12 | -// appenders[conf.projectName] = { type: 'file', filename: conf.detail.path+"/"+os.hostname() + '_'+conf.projectName+'.detail' }; | |
| 13 | -// log4js.configure({ | |
| 14 | -// appenders: appenders , | |
| 15 | -// categories: { default: { appenders: [conf.projectName], level: 'info' } } | |
| 16 | -// }); | |
| 17 | - | |
| 18 | -// var logger4jDetail = log4js.getLogger(conf.projectName); | |
| 19 | -var logger = []; | |
| 20 | - | |
| 21 | -const mapLog = {}; | |
| 22 | - | |
| 23 | -logger.startlog = function (req,cmdData,session,identity) | |
| 24 | -{ | |
| 25 | - | |
| 26 | - | |
| 27 | - var start = { | |
| 28 | - session : utils.undefinedToNull(session), | |
| 29 | - cmd : cmdData, | |
| 30 | - identity : utils.undefinedToNull(identity) | |
| 31 | - }; | |
| 32 | - | |
| 33 | - mapLog[req.id].start = start; | |
| 34 | -} | |
| 35 | - | |
| 36 | -logger.stat = function (msg){ | |
| 37 | - logg.stat(msg); | |
| 38 | -} | |
| 39 | - | |
| 40 | -logger.log = function (msg,type) | |
| 41 | -{ | |
| 42 | - | |
| 43 | - if(!type) | |
| 44 | - type = 'debug'; | |
| 45 | - | |
| 46 | - type = type.toLowerCase(); | |
| 47 | - | |
| 48 | - switch(type) { | |
| 49 | - case 'debug': | |
| 50 | - logg.debug(msg); | |
| 51 | - break; | |
| 52 | - case 'info': | |
| 53 | - logg.info(msg); | |
| 54 | - break; | |
| 55 | - case 'warn': | |
| 56 | - logg.warn(msg); | |
| 57 | - break; | |
| 58 | - case 'error': | |
| 59 | - logg.error(msg); | |
| 60 | - break; | |
| 61 | - default: | |
| 62 | - logg.debug(msg); | |
| 63 | - } | |
| 64 | - | |
| 65 | - console.log(msg); | |
| 66 | -} | |
| 67 | - | |
| 68 | -logger.addSuccessSummary = function (req,nodeData,cmdData,result) | |
| 69 | -{ | |
| 70 | - summary = { | |
| 71 | - node : utils.undefinedToNull(nodeData), | |
| 72 | - cmd : cmdData, | |
| 73 | - resultCode : result.resultCode, | |
| 74 | - resultDescription : result.resultDescription, | |
| 75 | - type : constants.SUCCESS | |
| 76 | - }; | |
| 77 | - mapLog[req.id].summaryList.push(summary); | |
| 78 | -} | |
| 79 | - | |
| 80 | -logger.addErrorSummary = function (req,nodeData,cmdData,result) | |
| 81 | -{ | |
| 82 | - summary = { | |
| 83 | - node : utils.undefinedToNull(nodeData), | |
| 84 | - cmd : cmdData, | |
| 85 | - resultCode : result.resultCode, | |
| 86 | - resultDescription : result.resultDescription, | |
| 87 | - type : constants.ERROR | |
| 88 | - }; | |
| 89 | - | |
| 90 | - mapLog[req.id].summaryList.push(summary); | |
| 91 | -} | |
| 92 | - | |
| 93 | -logger.logSummary = function (req,response) | |
| 94 | -{ | |
| 95 | - | |
| 96 | - var start = mapLog[req.id].start; | |
| 97 | - var summaryList = mapLog[req.id].summaryList; | |
| 98 | - | |
| 99 | - var log = logg.summary(start.session,start.cmd,start.identity); | |
| 100 | - var row; | |
| 101 | - for (let index = 0; index < summaryList.length; index++) { | |
| 102 | - row = summaryList[index]; | |
| 103 | - if(row.type === constants.SUCCESS){ | |
| 104 | - log.addSuccessBlock(row.node,row.cmd,row.resultCode,row.resultDescription); | |
| 105 | - } else { | |
| 106 | - log.addErrorBlock(row.node,row.cmd,utils.undefinedToNull(row.resultCode),utils.undefinedToNull(row.resultDescription)); | |
| 107 | - } | |
| 108 | - } | |
| 109 | - | |
| 110 | - if(response){ | |
| 111 | - log.end(response.resultCode,response.resultDescription) | |
| 112 | - } else { | |
| 113 | - log.end() | |
| 114 | - } | |
| 115 | -} | |
| 116 | - | |
| 117 | -logger.startEC = function(req) | |
| 118 | -{ | |
| 119 | - var objectLog = { | |
| 120 | - summaryList : [], | |
| 121 | - start : {}, | |
| 122 | - detail : {}, | |
| 123 | - startTime : new Date() | |
| 124 | - } | |
| 125 | - | |
| 126 | - | |
| 127 | - mapLog[req.id] = objectLog; | |
| 128 | - | |
| 129 | - var newLine = "\r\n"; | |
| 130 | - var messageRes = ''; | |
| 131 | - var startTime = mapLog[req.id].startTime; | |
| 132 | - | |
| 133 | - | |
| 134 | - if(req.body) | |
| 135 | - { | |
| 136 | - if(typeof req.body === 'string') | |
| 137 | - body = req.bod; | |
| 138 | - else | |
| 139 | - body = JSON.stringify(req.body); | |
| 140 | - } | |
| 141 | - | |
| 142 | - var header = ""; | |
| 143 | - if(req.headers) | |
| 144 | - { | |
| 145 | - if(typeof req.headers === 'string') | |
| 146 | - header = req.headers; | |
| 147 | - else | |
| 148 | - header = JSON.stringify(req.headers); | |
| 149 | - } | |
| 150 | - | |
| 151 | - messageRes += newLine+newLine+"#====> Incoming Message <====# "; | |
| 152 | - messageRes += newLine+"Session : "+req.id; | |
| 153 | - messageRes += newLine+"Url : "+req.url; | |
| 154 | - messageRes += newLine+"Method : "+req.method; | |
| 155 | - messageRes += newLine+"Header : "+header; | |
| 156 | - messageRes += newLine+"Body : "+body; | |
| 157 | - messageRes += newLine+"Time in : "+startTime.toISOString(); | |
| 158 | - messageRes += newLine | |
| 159 | - | |
| 160 | - // logg.debug(messageRes); | |
| 161 | -} | |
| 162 | - | |
| 163 | -logger.endEC = function(req,res) | |
| 164 | -{ | |
| 165 | - | |
| 166 | - var newLine = "\r\n"; | |
| 167 | - var messageRes = ''; | |
| 168 | - var startTime = mapLog[req.id].startTime; | |
| 169 | - var endTime = new Date(); | |
| 170 | - | |
| 171 | - var body = ""; | |
| 172 | - if(res.resBody) | |
| 173 | - { | |
| 174 | - if(typeof res.resBody === 'string') | |
| 175 | - body = res.resBody; | |
| 176 | - else | |
| 177 | - body = JSON.stringify(res.resBody); | |
| 178 | - } | |
| 179 | - | |
| 180 | - var header = ""; | |
| 181 | - if(res.req.headers) | |
| 182 | - { | |
| 183 | - if(typeof res.req.headers === 'string') | |
| 184 | - header = res.req.headers; | |
| 185 | - else | |
| 186 | - header = JSON.stringify(res.req.headers); | |
| 187 | - } | |
| 188 | - | |
| 189 | - messageRes += newLine+newLine+"#====> Outgoing Message <====# "; | |
| 190 | - messageRes += newLine+"Session : "+req.id; | |
| 191 | - messageRes += newLine+"Header : "+header; | |
| 192 | - messageRes += newLine+"Body : "+body; | |
| 193 | - messageRes += newLine+"Time out : "+endTime.toISOString(); | |
| 194 | - messageRes += newLine+"Use times : "+Math.abs(endTime - startTime)+" ms"; | |
| 195 | - | |
| 196 | - | |
| 197 | - | |
| 198 | - | |
| 199 | - // logg.debug(messageRes); | |
| 200 | - messageRes += newLine | |
| 201 | -} | |
| 202 | - | |
| 203 | -//detail | |
| 204 | -logger.detailRequestFE = function(req) | |
| 205 | -{ | |
| 206 | - // console.log(req); | |
| 207 | - var protocal = "HTTP"; | |
| 208 | - var method = req.method; | |
| 209 | - var url = req.headers.host+req.originalUrl; | |
| 210 | - var body = JSON.stringify(req.body); | |
| 211 | - // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); | |
| 212 | - logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Request from FE - body: "+body); | |
| 213 | - | |
| 214 | -} | |
| 215 | - | |
| 216 | -logger.detailResponseFE = function(req) | |
| 217 | -{ | |
| 218 | - var protocal = "HTTP"; | |
| 219 | - var method = req.method; | |
| 220 | - var url = req.headers.host+req.originalUrl; | |
| 221 | - var body = JSON.stringify(req.body); | |
| 222 | - // logger4jDetail.info(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); | |
| 223 | - logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); | |
| 224 | -} | |
| 225 | - | |
| 226 | -//mongo | |
| 227 | -logger.detailSqlQuery = function(req,method,collection,data) | |
| 228 | -{ | |
| 229 | - // logger4jDetail.info(logPrefix(req)+"BE Send Mongo Collection: \""+collection+"\" Query: "+JSON.stringify(data)); | |
| 230 | - logg.detail(logPrefix(req)+"BE Send Mongo Collection: "+collection+" Method:"+utils.findCmdfromMethod(method)+" Query: "+JSON.stringify(data)); | |
| 231 | -} | |
| 232 | - | |
| 233 | -logger.detailSqlResult = function(req,collection,data) | |
| 234 | -{ | |
| 235 | - // logger4jDetail.info(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); | |
| 236 | - logg.detail(logPrefix(req)+"BE Receive Mongo Collection: \""+collection+"\" Result: "+JSON.stringify(data)); | |
| 237 | - | |
| 238 | -} | |
| 239 | - | |
| 240 | -logger.detailRequestBE = function(req,data) | |
| 241 | -{ | |
| 242 | - var protocal = data.protocal; | |
| 243 | - var method = data.method; | |
| 244 | - var url = data.url; | |
| 245 | - var header = JSON.stringify(data.header); | |
| 246 | - var body = JSON.stringify(data.body); | |
| 247 | - // logger4jDetail.info(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); | |
| 248 | - logg.detail(logPrefix(req)+"BE Send "+protocal+" "+method+" "+url+" request-header: "+header+" request-body:"+body); | |
| 249 | -} | |
| 250 | - | |
| 251 | -logger.detailResponseBE = function(req,data) | |
| 252 | -{ | |
| 253 | - var protocal = data.protocal; | |
| 254 | - var method = data.method; | |
| 255 | - var url = data.url; | |
| 256 | - var header = JSON.stringify(data.header); | |
| 257 | - var body = JSON.stringify(data.body); | |
| 258 | - // logger4jDetail.info(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); | |
| 259 | - logg.detail(logPrefix(req)+"BE Receive "+protocal+" "+method+" "+url+" response-header: "+header+" response-body:"+body); | |
| 260 | -} | |
| 261 | - | |
| 262 | -function logPrefix(req) | |
| 263 | -{ | |
| 264 | - | |
| 265 | - var userName = "userName"; | |
| 266 | - var sessionID = "sessionID"; | |
| 267 | - var session = "session"; | |
| 268 | - | |
| 269 | - return ":: ## "+userName+" - "+sessionID+" - "+session+ " ## "; | |
| 270 | - | |
| 271 | -} | |
| 272 | - | |
| 273 | -module.exports = logger; |
utils/mongoDB.js
| 1 | -const log = require('./log'); | |
| 2 | -const stat = require('./stats'); | |
| 3 | -var constants = require('./constants'); | |
| 4 | 1 | var MongoClient = require('mongodb').MongoClient; |
| 5 | -var config = require('./config'); | |
| 2 | + | |
| 3 | +const core = require('./core'); | |
| 4 | +const log = core.log; | |
| 5 | +const stat = core.stats; | |
| 6 | +const constants = core.constants; | |
| 7 | +const config = core.config; | |
| 8 | + | |
| 9 | + | |
| 6 | 10 | |
| 7 | 11 | var connection = {}; |
| 8 | 12 | connection.reqMongo = async function (req,method, query, collection) { | ... | ... |
utils/stats.js
| ... | ... | @@ -1,51 +0,0 @@ |
| 1 | -var config = require('./config'); | |
| 2 | -var log = require('./log.js'); | |
| 3 | -var utils = require('./utils'); | |
| 4 | - | |
| 5 | - | |
| 6 | -var nodeName = config.get("appName"); | |
| 7 | -var stat = []; | |
| 8 | - | |
| 9 | - | |
| 10 | -//client and Node | |
| 11 | -stat.receiveRequest = function (method,cmd){ | |
| 12 | - writeStats(nodeName+" Receive "+method+" "+cmd+" Request"); | |
| 13 | -}; | |
| 14 | - | |
| 15 | -stat.receiveBadRequest = function (method,cmd){ | |
| 16 | - writeStats(nodeName+" Receive Bad "+method+" "+cmd+" Request"); | |
| 17 | -}; | |
| 18 | - | |
| 19 | -stat.sendResponse = function (method,cmd,result){ | |
| 20 | - writeStats(nodeName+" Send "+method+" "+cmd+" Response "+(result?result:"Success")); | |
| 21 | -}; | |
| 22 | - | |
| 23 | -//node and mongo | |
| 24 | -stat.sendQuery = function (method,collection){ | |
| 25 | - writeStats(nodeName+" Send "+utils.findCmdfromMethod(method)+" "+collection+" Request"); | |
| 26 | -}; | |
| 27 | - | |
| 28 | -stat.receiveBadQuery = function (method,collection){ | |
| 29 | - writeStats(nodeName+" Receive Bad "+utils.findCmdfromMethod(method)+" "+collection+" Response"); | |
| 30 | -}; | |
| 31 | - | |
| 32 | -stat.receiveQuery = function (method,collection){ | |
| 33 | - writeStats(nodeName+" Receive "+utils.findCmdfromMethod(method)+" "+collection+" Response"); | |
| 34 | -}; | |
| 35 | - | |
| 36 | -//unknow | |
| 37 | -stat.receiveUnknow = function(){ | |
| 38 | - writeStats(nodeName+" Receive Unknown Error Request"); | |
| 39 | -}; | |
| 40 | - | |
| 41 | -stat.sendUnknow = function(){ | |
| 42 | - writeStats(nodeName+" Send Unknown Error Response"); | |
| 43 | -}; | |
| 44 | - | |
| 45 | -function writeStats(string) { | |
| 46 | - log.log(string); | |
| 47 | - log.stat(string); | |
| 48 | -} | |
| 49 | - | |
| 50 | - | |
| 51 | -module.exports = stat; | |
| 52 | 0 | \ No newline at end of file |