Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

logging - Evaluate log files line by line in Javascript

I have some log files from a Gameserver that I want to evaluate, there are some problems I ran into while doing so. I am reading the log file line by line:

const fs = require('fs');
const readline = require('readline');

async function processLineByLine() {
    const fileStream = fs.createReadStream('log.adm');

    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    for await (const line of rl) {
        console.log(`Line from file: ${line}`);
        }
    }
}

processLineByLine();

The Log file is different for each line, here is a short example:

00:03:06 | Player "XXX"(id=Unknown) has been disconnected
00:03:41 | ##### PlayerList log: 6 players
00:03:41 | Player "XXX" (id=XXX= pos=<13849, 2898.0, 32.6>)
00:03:41 | Player "XXX" (id=XXX= pos=<12895, 8573.3, 6.2>)
00:03:41 | Player "XXX (2)" (id=XXX= pos=<2471.7, 5129.1, 193.7>)
00:03:41 | Player "XXX" (id=XXX= pos=<13856, 2883.2, 32.7>)
00:03:41 | Player "XXX" (id=XXX= pos=<2480.8, 5153.3, 190.3>)
00:03:41 | Player "XXX" (id=XXX= pos=<12895.7, 8573.6, 6.2>)
00:03:41 | #####
00:04:10 | Player "XXX (2)" (id= pos=<2469.8, 5130.2, 193.7>)[HP: 45] hit by Player "XXX" (id=XXX= pos=<2479.9, 5116.7, 193.2>) into Torso(23) for 55 damage (Bullet_762x39) with KA-M from 16.8674 meters 
00:04:18 | Player "XXX"(id=Unknown) has been disconnected
00:08:40 | ##### PlayerList log: 6 players
00:08:40 | Player "XXX" (id=XXX= pos=<13843.5, 2894.8, 32.5>)
00:08:40 | Player "XXX" (id=XXX= pos=<13269.8, 3362.0, 7.7>)
00:08:40 | Player "XXX (2)" (id=XXX= pos=<2512.3, 5083.0, 193.5>)
00:08:40 | Player "XXX" (id=XXX= pos=<13844.5, 2894.5, 32.3>)
00:08:40 | Player "XXX" (id=XXX= pos=<2467.7, 5124.0, 193.3>)
00:08:40 | Player "XXX" (id=XXX= pos=<13270.2, 3361.4, 7.7>)
00:08:40 | #####

so I've tried to split the line on certain characters but there is nothing they really have in common. My goal is to get the relevant information in its own variable, for each player so like: Time, PlayerName, ID, Position and the event (e.g got hit, placed something)

for the event, I think it would be best to ask for different if clauses or switch statements, but first of all I need to get the important data for every line.

question from:https://stackoverflow.com/questions/65860932/evaluate-log-files-line-by-line-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One option is to use a pattern and capture all the parts using capturing groups.

When processing the groups, check if the index exists in the array as not all lines will match, and for some lines group 4 and 5 are optional.

^(dd:dd:dd)s+|s+Players+"([^"]+)"s*(id=([^s()]*)(?:s+pos=(<[^<>]*>)(.*))?)

Explanation

  • ^ Start of string
  • (dd:dd:dd) Capture in group 1 matching a time like pattern
  • s+|s+ Match a | between whitespace chars
  • Players+"([^"]+)"s* Match Players 1+ whitespace chars and capture in group 2 what is between the double quotes
  • ( Match (
  • id=([^s()]*) Match id= and capture 1 or more non whitespace chars exluding ( and ) in group 3
  • (?: Non caputure group
    • s+pos=(<[^<>]*>) Match 1+ whitespace chars and capture in group 4 what is between the angle brackets
  • (.*) Match 0+ times any character in group 5
  • )? Close non capture group and make it optional
  • ) Match )

See a regex demo

An example using a few lines in a loop (in your example code you would process a single line)

const regex = /^(dd:dd:dd)s+|s+Players+"([^"]+)"s*(id=([^s()]*)(?:s+pos=(<[^<>]*>)(.*))?)/g;
['00:03:06 | Player "XXX"(id=Unknown) has been disconnected',
  '00:03:41 | Player "XXX" (id=XXX= pos=<13849, 2898.0, 32.6>)',
  '00:04:10 | Player "XXX (2)" (id= pos=<2469.8, 5130.2, 193.7>)[HP: 45] hit by Player "XXX" (id=XXX= pos=<2479.9, 5116.7, 193.2>) into Torso(23) for 55 damage (Bullet_762x39) with KA-M from 16.8674 meters ',
].forEach(s => console.log([...s.matchAll(regex)]));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...