This commit is contained in:
ddmt 2024-12-13 22:07:20 +08:00
parent 7f8b2e4f7a
commit 3ecf33036c
3 changed files with 75 additions and 47 deletions

View File

@ -82,3 +82,30 @@ export function getRelativeTime(date) {
return '很久以前';
}
/*
* 解析nginx日志(单行)
* @param {string} log
*/
function parseNginxLog (log) {
const logPattern = /^(\S+) - - \[([^\]]+)\] "(\S+) (\S+) HTTP\/\d\.\d" (\d+) (\d+) "([^"]*)" "([^"]*)"/;
try {
const match = log.match(logPattern);
if (match) {
return {
ip: match[1],
timestamp: match[2],
method: match[3],
url: match[4],
status: parseInt(match[5]),
responseSize: parseInt(match[6]),
referrer: match[7],
userAgent: match[8],
};
}
} catch (error) {
return error;
}
return null;
}

View File

@ -8,6 +8,6 @@
* @FilePath: /index.js
*/
export { animateStart, setClassVar, getStyleVar } from './Tool/animate.js';
export { randomNum, nextArray, ArrayDeHeavy, getRelativeTime } from './Tool/number.js';
export { randomNum, nextArray, ArrayDeHeavy, getRelativeTime, parseNginxLog } from './Tool/number.js';
console.log('ddmt-tool Loading successfully!! 😺');

1
types/index.d.ts vendored
View File

@ -8,5 +8,6 @@ declare module 'ddmt-tool' {
export function randomNum(minNum: number): number;
export function randomNum(minNum: number, maxNum: number): number;
export function nextArray(arr: any[], index: number): any[];
export function parseNginxLog(log: string): object;
}