logfrom
v1.0.0
Published
logFrom
Downloads
252
Readme
nginx配置正则验证
安装
npm install logfrom
配置
var logfrom = require ('logfrom');
var logfrom = function (logFormat) {}
/* @Params: logFormat参数就是标准NGINX配置文件中对应log_format内容 通过logFormat.match() 提取实际配置部分; 优先提取多行NGINX配置 通过单引号提取实际KEY列表 */
if (logFormat == undefined || logFormat == null)
return null;
示例
const logfrom = require('logfrom')//引入log
const log_sample = `42.110.55.127 - - [17/Jan/2019:16:52:17 +0800] "GET /favicon.ico HTTP/1.1" 45.16.35.141 404 404 570 "http://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 0.000 -` //配置 日志样例
const log_format = `log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $request_length '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';` //配置推荐的log_format日志格式
const result = logfrom(log_format)
const generateRegex = new RegExp(result.regex) //生成一个正则
console.log(result);
console.log(log_sample.match(generateRegex))//对正则进行macth操作,分割枚举
/*
console.log(result);运行生成的正则如下:
{ space_err: false,
err_list: [],
regex:
'(\\S*)\\s*-\\s*(\\S*)\\s*\\[(\\d+/\\S+/\\d+:\\d+:\\d+:\\d+)\\s+\\S+\\]\\s*"(\\S+)\\s+(\\S+)\\s+\\S+"\\s*(\\S*)\\s*(\\S*)\\s*(\\S*)\\s*(\\S*)\\s*"(\\S*)"\\s*"([\\S ]*)".*',
keys:
[ 'remote_addr',
'remote_user',
'time_local',
'request_method',
'request_uri',
'status',
'request_length',
'status',
'body_bytes_sent',
'http_referer',
'http_user_agent' ] }
console.log(log_sample.match(generateRegex));代码运行结果如下:
[ '42.110.55.127 - - [17/Jan/2019:16:52:17 +0800] "GET /favicon.ico HTTP/1.1" 45.16.35.141 404 404 570 "http://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 0.000 -',
'42.110.55.127',
'-',
'17/Jan/2019:16:52:17',
'GET',
'/favicon.ico',
'45.16.35.141',
'404',
'404',
'570',
'http://example.com/',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
index: 0,
input: '42.110.55.127 - - [17/Jan/2019:16:52:17 +0800] "GET /favicon.ico HTTP/1.1" 45.16.35.141 404 404 570 "http://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 0.000 -',
groups: undefined ]
*/