Comparing version 0.0.20 to 0.0.21
206
index.js
@@ -8,7 +8,7 @@ var fs = require('fs'), | ||
stackTrace = require('stack-trace'), | ||
colors = require('colors'), | ||
// colors = require('colors'), | ||
mkdirp = require('mkdirp'); | ||
var data_path = __dirname + "/"; //模板地址默认在模块里 | ||
var log_path = __dirname + "/log"; | ||
var data_path = __dirname + '/'; //模板地址默认在模块里 | ||
var log_path = __dirname + '/log'; | ||
var LOGGER_CACHE = {}; | ||
@@ -33,3 +33,5 @@ var LOGFILE_CACHE = {}; | ||
for (var num in LEVELS) { | ||
LEVELS_REVERSE[LEVELS[num]] = num; | ||
if (LEVELS_REVERSE.hasOwnProperty(num)) { | ||
LEVELS_REVERSE[LEVELS[num]] = num; | ||
} | ||
} | ||
@@ -65,5 +67,5 @@ | ||
'log_path': log_path, | ||
'access_log_path': log_path + "/access", | ||
'access_error_log_path': log_path + "/access", | ||
'data_path': data_path + "data" | ||
'access_log_path': log_path + '/access', | ||
'access_error_log_path': log_path + '/access', | ||
'data_path': data_path + 'data' | ||
}, opts); | ||
@@ -89,3 +91,3 @@ | ||
} | ||
}; | ||
@@ -111,3 +113,3 @@ | ||
log: function (level, obj) { | ||
var level = String(level).toUpperCase(); // WARNING格式 | ||
level = String(level).toUpperCase(); // WARNING格式 | ||
var intLevel = this.getLogLevelInt(level); // 2格式 | ||
@@ -120,6 +122,6 @@ var format = this.getLogFormat(level); | ||
if (obj) { | ||
if (typeof obj == "string") { | ||
if (typeof obj === 'string') { | ||
option['msg'] = obj; | ||
} | ||
else if (typeof obj == "object") { | ||
else if (typeof obj === 'object') { | ||
option = obj; | ||
@@ -131,3 +133,3 @@ } | ||
//解析自定义字段,存放在对应ODP的 encoded_str_array中 | ||
this.params['encoded_str_array'] = ""; | ||
this.params['encoded_str_array'] = ''; | ||
if (option['custom']) { | ||
@@ -137,3 +139,3 @@ this.parseCustomLog(option['custom']); | ||
if (intLevel == 0 || intLevel == 3) { //访问日志 | ||
if (intLevel === 0 || intLevel === 3) { //访问日志 | ||
this.writeLog(intLevel, option, format); | ||
@@ -143,4 +145,4 @@ } | ||
//IS_OMP等于0打印两种格式日志,等于1打印STD日志,等于2打印WF/Default日志 | ||
if (this.opts['IS_OMP'] == 0 || this.opts['IS_OMP'] == 2) { | ||
option['filename_suffix'] = ""; | ||
if (this.opts['IS_OMP'] === 0 || this.opts['IS_OMP'] === 2) { | ||
option['filename_suffix'] = ''; | ||
option['escape_msg'] = false; //错误消息不转义 | ||
@@ -150,4 +152,4 @@ this.writeLog(intLevel, option, format); | ||
if (this.opts['IS_OMP'] == 0 || this.opts['IS_OMP'] == 1) { | ||
option['filename_suffix'] = ".new"; | ||
if (this.opts['IS_OMP'] === 0 || this.opts['IS_OMP'] === 1) { | ||
option['filename_suffix'] = '.new'; | ||
option['escape_msg'] = true; //错误消息转义 | ||
@@ -163,3 +165,5 @@ this.writeLog(intLevel, option, this.format['STD']); | ||
for (var property in source) { | ||
destination[property] = source[property]; | ||
if (source.hasOwnProperty(property)) { | ||
destination[property] = source[property]; | ||
} | ||
} | ||
@@ -177,6 +181,6 @@ return destination; | ||
// ACCESS为访问格式 | ||
if (level == "ACCESS") { | ||
if (level === 'ACCESS') { | ||
format = formats['ACCESS']; | ||
} | ||
else if (level == "ACCESS_ERROR") { | ||
else if (level === 'ACCESS_ERROR') { | ||
format = formats['ACCESS_ERROR']; //访问错误 404 301等单独存储 | ||
@@ -186,3 +190,3 @@ } | ||
//warning和fatal格式不一样,且单独存储 | ||
if (level == "WARNING" || level == "FATAL") { | ||
if (level === 'WARNING' || level === 'FATAL') { | ||
format = formats['WF']; | ||
@@ -202,9 +206,9 @@ } | ||
this.params['errno'] = option['errno'] || 0; //错误号 | ||
this.params['error_msg'] = option['msg'] || ""; //自定义错误信息,%M默认不转义 | ||
this.params['TypeName'] = ""; | ||
this.params['FunctionName'] = ""; | ||
this.params['MethodName'] = ""; | ||
this.params['FileName'] = ""; | ||
this.params['LineNumber'] = ""; | ||
this.params['isNative'] = ""; | ||
this.params['error_msg'] = option['msg'] || ''; //自定义错误信息,%M默认不转义 | ||
this.params['TypeName'] = ''; | ||
this.params['FunctionName'] = ''; | ||
this.params['MethodName'] = ''; | ||
this.params['FileName'] = ''; | ||
this.params['LineNumber'] = ''; | ||
this.params['isNative'] = ''; | ||
if (option['stack']) { | ||
@@ -214,3 +218,3 @@ try { | ||
this.params['error_msg'] = this.opts['debug'] ? option['stack'] : String(option['stack']).replace( | ||
/(\n)+|(\r\n)+/g, " "); | ||
/(\n)+|(\r\n)+/g, ' '); | ||
} | ||
@@ -233,3 +237,3 @@ var trace = stackTrace.parse(option['stack']); | ||
parseCustomLog: function (obj) { | ||
if ("object" != typeof (obj)) { | ||
if ('object' !== typeof (obj)) { | ||
return false; | ||
@@ -240,7 +244,7 @@ } | ||
if (obj.hasOwnProperty(key)) { | ||
items.push(escape(key) + "=" + escape(obj[key])); | ||
items.push(escape(key) + '=' + escape(obj[key])); | ||
} | ||
} | ||
if (items.length > 0) { | ||
this.params['encoded_str_array'] = items.join(" "); | ||
this.params['encoded_str_array'] = items.join(' '); | ||
} | ||
@@ -261,4 +265,4 @@ }, | ||
this.params['SERVER_PROTOCOL'] = String(req.protocol).toUpperCase(); | ||
this.params['REQUEST_METHOD'] = req.method || ""; | ||
this.params['SERVER_PORT'] = req.app.settings ? req.app.settings.port : ""; | ||
this.params['REQUEST_METHOD'] = req.method || ''; | ||
this.params['SERVER_PORT'] = req.app.settings ? req.app.settings.port : ''; | ||
this.params['QUERY_STRING'] = req.query; | ||
@@ -270,3 +274,3 @@ this.params['REQUEST_URI'] = req.originalUrl; | ||
this.params['STATUS'] = res._header ? res.statusCode : null; | ||
this.params['CONTENT_LENGTH'] = (res._headers || {})['content-length'] || "-"; | ||
this.params['CONTENT_LENGTH'] = (res._headers || {})['content-length'] || '-'; | ||
@@ -284,12 +288,10 @@ this.params['pid'] = process.pid; | ||
} | ||
if (this.opts['IS_ODP'] == true) { | ||
if (this.opts['IS_ODP'] === true) { | ||
return this.opts['app']; | ||
} | ||
else { | ||
return 'unknow'; | ||
} | ||
return 'unknow'; | ||
}, | ||
//获取logID,如果没有生成唯一随机数 | ||
getLogID: function (req) { | ||
getLogID: function (req, logIDName) { | ||
var logId = 0; | ||
@@ -302,16 +304,16 @@ | ||
if (req) { | ||
if (req.headers['HTTP_X_BD_LOGID']) { | ||
logId = parseInt(req.headers['HTTP_X_BD_LOGID']); | ||
if (req.headers[logIDName || 'http_x_bd_logid']) { | ||
logId = parseInt(req.headers[logIDName || 'http_x_bd_logid'], 10); | ||
} | ||
else if (parseInt(req.query['logid']) > 0) { | ||
logId = parseInt(req.query['logid']); | ||
else if (parseInt(req.query['logid'], 10) > 0) { | ||
logId = parseInt(req.query['logid'], 10); | ||
} | ||
else if (parseInt(this.getCookie('logid')) > 0) { | ||
logId = parseInt(this.getCookie('logid')); | ||
else if (parseInt(this.getCookie('logid'), 10) > 0) { | ||
logId = parseInt(this.getCookie('logid'), 10); | ||
} | ||
} | ||
if (logId == 0) { | ||
if (logId === 0) { | ||
var obj = util.gettimeofday(); | ||
logId = ((obj['sec'] * 100000 + obj['usec'] / 10 + parseInt(Math.random() * 100)) & 0x7FFFFFFF); | ||
logId = ((obj['sec'] * 100000 + obj['usec'] / 10 + Math.floor(Math.random() * 100) & 0x7FFFFFFF)); | ||
} | ||
@@ -324,11 +326,11 @@ return logId; | ||
var prefix = this.getLogPrefix() || 'yog'; | ||
var logFile = "", | ||
log_path = ""; | ||
var logFile = '', | ||
log_path = ''; | ||
switch (intLevel) { | ||
case '0': //访问日志前缀默认access | ||
logFile = this.opts['access_log_file'] || "access"; //访问日志 | ||
logFile = this.opts['access_log_file'] || 'access'; //访问日志 | ||
log_path = this.opts['access_log_path'] || this.opts['log_path']; | ||
break; | ||
case '3': | ||
logFile = this.opts['access_error_log_file'] || "error"; //访问日志 | ||
logFile = this.opts['access_error_log_file'] || 'error'; //访问日志 | ||
log_path = this.opts['access_error_log_path'] || this.opts['log_path']; | ||
@@ -338,8 +340,7 @@ break; | ||
//是否使用子目录,app区分 | ||
log_path = this.opts['use_sub_dir'] ? | ||
(this.opts['log_path'] + "/" + prefix) : this.opts['log_path']; | ||
log_path = this.opts['use_sub_dir'] ? (this.opts['log_path'] + '/' + prefix) : this.opts['log_path']; | ||
logFile = prefix; | ||
} | ||
return log_path + "/" + logFile + ".log"; | ||
return log_path + '/' + logFile + '.log'; | ||
}, | ||
@@ -364,7 +365,7 @@ | ||
var logFile = this.getLogFile(intLevel), | ||
filename_suffix = options['filename_suffix'] || "", | ||
filename_suffix = options['filename_suffix'] || '', | ||
errno = options['errno'] || 0; | ||
if (this.getLogLevelInt('WARNING') == intLevel || this.getLogLevelInt('FATAL') == intLevel) { | ||
logFile += ".wf"; | ||
if (this.getLogLevelInt('WARNING') === intLevel || this.getLogLevelInt('FATAL') === intLevel) { | ||
logFile += '.wf'; | ||
} | ||
@@ -376,3 +377,3 @@ //文件后缀 | ||
if (this.opts['auto_rotate']) { | ||
logFile += "." + util.strftime(new Date(), '%Y%m%d%H'); | ||
logFile += '.' + util.strftime(new Date(), '%Y%m%d%H'); | ||
} | ||
@@ -409,7 +410,9 @@ | ||
for (var oldFile in fdCache) { | ||
try { | ||
fdCache[oldFile].end(); | ||
if (fdCache.hasOwnProperty(oldFile)) { | ||
try { | ||
fdCache[oldFile].end(); | ||
} | ||
catch (e) {} | ||
delete fdCache[oldFile]; | ||
} | ||
catch (e) {} | ||
delete fdCache[oldFile]; | ||
} | ||
@@ -451,3 +454,3 @@ var pathname = path.dirname(logFile); | ||
} | ||
return LOGGER_CACHE[format](this, util) + "\n"; | ||
return LOGGER_CACHE[format](this, util) + '\n'; | ||
}, | ||
@@ -482,3 +485,3 @@ | ||
var _act = "util.strftime(new Date(),'%y-%m-%d %H:%M:%S')"; | ||
if (param && param != "") { | ||
if (param && param !== '') { | ||
_act = "util.strftime(new Date(), '" + String(param) + "')"; | ||
@@ -502,3 +505,3 @@ } | ||
case 'C': | ||
if (param == '') { | ||
if (param === '') { | ||
action.push("logger.getParams('HTTP_COOKIE')"); | ||
@@ -624,10 +627,10 @@ } | ||
var strformat = util.preg_split(regex, format); | ||
var code = "'" + strformat[0] + "'"; | ||
var logCode = "'" + strformat[0] + "'"; | ||
for (var i = 1; i < strformat.length; i++) { | ||
code = code + ' + ' + action[i - 1] + " + '" + strformat[i] + "'"; | ||
}; | ||
var cmt = "/* Used for app " + this.opts['app'] + "\n"; | ||
cmt += " * Original format string:" + format.replace(/\*\//g, "* /"); | ||
logCode = logCode + ' + ' + action[i - 1] + " + '" + strformat[i] + "'"; | ||
} | ||
var cmt = '/* Used for app ' + this.opts['app'] + '\n'; | ||
cmt += ' * Original format string:' + format.replace(/\*\//g, '* /'); | ||
var str = cmt + "*/ \n module.exports=function(logger, util){\n return " + code + "; \n}"; | ||
var str = cmt + '*/ \n module.exports=function(logger, util){\n return ' + logCode + '; \n}'; | ||
return str; | ||
@@ -638,6 +641,6 @@ | ||
getParams: function (name) { | ||
if (this.params.hasOwnProperty(name) && this.params[name] != undefined && this.params[name] != "") { | ||
if (this.params.hasOwnProperty(name) && this.params[name] !== undefined && this.params[name] !== "") { | ||
return this.params[name]; | ||
} | ||
return "-"; | ||
return '-'; | ||
}, | ||
@@ -650,4 +653,4 @@ | ||
getCookie: function (name) { | ||
var name = String(name).replace(/(^\s*)|(\s*$)/g, ""); | ||
var match = String(this.getParams("COOKIE")).match(new RegExp(name + '=([^;]+)')); | ||
name = String(name).replace(/(^\s*)|(\s*$)/g, ''); | ||
var match = String(this.getParams('COOKIE')).match(new RegExp(name + '=([^;]+)')); | ||
if (match) { | ||
@@ -658,3 +661,3 @@ return match[1]; | ||
} | ||
} | ||
}; | ||
@@ -671,14 +674,12 @@ function requireFromString(src, filename) { | ||
var config = config || {}; | ||
/* if (config.mode === 'production') { | ||
process.on('uncaughtException', function(e) { | ||
if (config.mode === 'production') { | ||
console.log(config); | ||
logger.log("error2", e,"fatal"); | ||
return process.exit(1); | ||
} | ||
}); | ||
}*/ | ||
config = config || {}; | ||
return function (req, res, next) { | ||
var current; | ||
var logger; | ||
current = domain.create(); | ||
logger = new Logger(config); | ||
config._req = req; | ||
current.add(logger); | ||
current.logger = logger; // Add request object to custom property | ||
@@ -690,3 +691,3 @@ function logRequest() { | ||
logger.params['STATUS'] = res._header ? res.statusCode : null; | ||
logger.params['CONTENT_LENGTH'] = (res._headers || {})['content-length'] || "-"; | ||
logger.params['CONTENT_LENGTH'] = (res._headers || {})['content-length'] || '-'; | ||
if (req._startAt) { | ||
@@ -696,15 +697,7 @@ var diff = process.hrtime(req._startAt); | ||
logger.params['REQUEST_TIME'] = ms.toFixed(3); | ||
}; | ||
//暂时不区分访问错误日志 | ||
logger.log("ACCESS"); | ||
/*if(res.statusCode == '301' || res.statusCode == '404'){ | ||
logger.log("ACCESS_ERROR"); | ||
}else{ | ||
logger.log("ACCESS"); | ||
} */ | ||
} | ||
//不区分访问错误日志 | ||
logger.log('ACCESS'); | ||
} | ||
var current = domain.create(); | ||
config._req = req; | ||
var logger = new Logger(config); | ||
@@ -714,15 +707,12 @@ //只在请求过来的时候才设置LogId | ||
logger.parseReqParams(req, res); | ||
current.add(logger); | ||
current.logger = logger; // Add request object to custom property | ||
//response-time启动埋点 | ||
req._startAt = process.hrtime(); | ||
req._startTime = new Date; | ||
res.on('finish', logRequest); | ||
res.on('close', logRequest); | ||
res.once('finish', logRequest); | ||
res.once('close', logRequest); | ||
res.on('log', function (e, level) { | ||
var option = e || {}; | ||
var level = level || "notice"; | ||
level = level || 'notice'; | ||
// logger.parseReqParams(req, res); | ||
@@ -738,3 +728,3 @@ logger.log(level, option); | ||
current.run(next); | ||
} | ||
}; | ||
}; | ||
@@ -748,5 +738,3 @@ | ||
} | ||
else { | ||
return new Logger(config); | ||
} | ||
return new Logger(config); | ||
}; |
{ | ||
"name": "yog-log", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"description": "yog logger", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1
73313
1284