Skip to content

Commit

Permalink
feat(logs): use separate json file to parse logs for log viewer (#2399)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Cohen <ryan@sct.dev>
  • Loading branch information
danshilm and sct committed Jan 20, 2022
1 parent 1f5785d commit ce31bef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
20 changes: 18 additions & 2 deletions server/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import * as winston from 'winston';
import 'winston-daily-rotate-file';
import path from 'path';
import fs from 'fs';

// Migrate away from old log
const OLD_LOG_FILE = path.join(__dirname, '../config/logs/overseerr.log');
Expand Down Expand Up @@ -52,6 +52,22 @@ const logger = winston.createLogger({
createSymlink: true,
symlinkName: 'overseerr.log',
}),
new winston.transports.DailyRotateFile({
filename: process.env.CONFIG_DIRECTORY
? `${process.env.CONFIG_DIRECTORY}/logs/.machinelogs-%DATE%.json`
: path.join(__dirname, '../config/logs/.machinelogs-%DATE%.json'),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '1d',
createSymlink: true,
symlinkName: '.machinelogs.json',
format: winston.format.combine(
winston.format.splat(),
winston.format.timestamp(),
winston.format.json()
),
}),
],
});

Expand Down
56 changes: 32 additions & 24 deletions server/routes/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,38 +303,46 @@ settingsRoutes.get(
}

const logFile = process.env.CONFIG_DIRECTORY
? `${process.env.CONFIG_DIRECTORY}/logs/overseerr.log`
: path.join(__dirname, '../../../config/logs/overseerr.log');
? `${process.env.CONFIG_DIRECTORY}/logs/.machinelogs.json`
: path.join(__dirname, '../../../config/logs/.machinelogs.json');
const logs: LogMessage[] = [];
const logMessageProperties = [
'timestamp',
'level',
'label',
'message',
'data',
];

try {
fs.readFileSync(logFile)
.toString()
.split(/(?=\n\d{4}-\d{2})/g)
fs.readFileSync(logFile, 'utf-8')
.split('\n')
.forEach((line) => {
if (!line.length) return;

const jsonRegexp = new RegExp(
/[{[]{1}([,:{}[\]0-9.\-+Eaeflnr-u \n\r\t]|"[^"\n]*?")+[}\]]{1}/
);
const logMessage = JSON.parse(line);

const timestamp = line.match(new RegExp(/.{24}/)) || [];
const level = line.match(new RegExp(/(?<=.{24}\s\[).+?(?=\])/)) || [];
const label =
line.match(new RegExp(/(?<=.{24}\s\[.+\]\[).+?(?=\])/)) || [];
const message =
line.match(new RegExp(/(?<=\[.+\]:\s)[\s\S][^\r]+/)) || [];
const data = message[0].match(jsonRegexp) || [];

if (level.length && filter.includes(level[0])) {
logs.push({
timestamp: timestamp[0],
level: level[0],
label: label[0],
message: message[0].replace(jsonRegexp, ''),
data: data.length ? JSON.parse(data[0]) : undefined,
});
if (!filter.includes(logMessage.level)) {
return;
}

if (
!Object.keys(logMessage).every((key) =>
logMessageProperties.includes(key)
)
) {
Object.keys(logMessage)
.filter((prop) => !logMessageProperties.includes(prop))
.forEach((prop) => {
Object.assign(logMessage, {
data: {
[prop]: logMessage[prop],
},
});
});
}

logs.push(logMessage);
});

const displayedLogs = logs.reverse().slice(skip, skip + pageSize);
Expand Down

0 comments on commit ce31bef

Please sign in to comment.