Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var spawn = require('child_process').spawn;
- var filename = '/opt/traccar/logs/tracker-server.log';
- const fs = require("fs");
- console.log("Starting logger service...");
- var tail = spawn("tail", ['-f',filename]);
- const imeiBindings = {};
- const hashBindings = {};
- const tmpLogs = {};
- tail.stdout.on("data", async function (data) {
- const text = data.toString();
- //const matches = text.matchAll(/(?<data>[0-9]{4}-[0-9]{2}-[0-9]{2}) (?<hora>[0-9]{2}:[0-9]{2}:[0-9]{2}) [A-Za-z]*: \[(?<hash>[A-Za-z0-9]*): [0-9]*( (>|<) [0-9.:]*\] \[[A-Z]*\] HEX: ([A-Za-z0-9]*)|\] [0-9.:]* .*id: (?<imeib>[A-Za-z0-9]*), (.*)|\] .*id: (?<imeic>[A-Za-z0-9]*), (.*)|\] [0-9.:]* .*)\n/gm);
- const matches = text.matchAll(/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} (.*?): \[([A-Za-z0-9]*)(: [A-Za-z0-9]* (>|<) ([A-Za-z0-9.]*))?\] ([A-Za-z0-9]*|id: (.*?), (.*?))\n/g);
- for(m of matches){
- console.log(m);
- let imei = false;
- let hash = false;
- if(m[7]){
- imei = m[7];
- }else if(m[8]){
- imei = m[8];
- }
- if(m[2]){
- hash = m[2];
- }
- if(imei){
- imeiBindings[imei] = hash;
- hashBindings[hash] = imei;
- }
- if(hashBindings[m[2]]){
- const folderName = "./logs/"+new Date().toISOString().split("T")[0];
- const fileName = hashBindings[hash]+".log";
- if (!fs.existsSync(folderName)) {
- fs.mkdirSync(folderName);
- }
- if(tmpLogs[hash]){
- for(let t of tmpLogs[hash]){
- const _folderName = "./logs/"+new Date().toISOString().split("T")[0];
- console.log("insert "+fileName+" from tmp");
- await fs.promises.appendFile(_folderName+"/"+fileName,t[0]);
- }
- delete tmpLogs[hash];
- }
- await fs.promises.appendFile(folderName+"/"+fileName,m[0]);
- }else{
- if(!tmpLogs[hash]){
- tmpLogs[hash] = [];
- }
- tmpLogs[hash].push(m);
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement