Skip to content

Commit

Permalink
Fix: correctly stringify log messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
dleclercpro committed Dec 13, 2023
1 parent 80194d5 commit 5f689a8
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 266,605 deletions.
265,973 changes: 0 additions & 265,973 deletions data/production/logs/2023.12.07T00.00.00Z.log

Large diffs are not rendered by default.

613 changes: 1 addition & 612 deletions data/production/logs/app.log

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "berlin-lea-performance-monitor",
"version": "1.20.0",
"version": "1.20.1",
"author": "David Leclerc",
"main": "./src/index.ts",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const getLoggerByUseCase = () => {
// When polling, output all logs to file and terminal
if (POLL) {
return pino({
level: 'trace',
level: 'debug',
formatters: FORMATTERS,
timestamp: pino.stdTimeFunctions.isoTime,
}, pino.transport({
Expand Down
32 changes: 19 additions & 13 deletions src/models/logs/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,24 @@ class Log {
}

public static fromText(line: string, index: number) {
const { level, time, version, pid, hostname, msg, err } = JSON.parse(line);

return new Log({
line: index + 1,
level,
time: new Date(time),
version: Release.fromString(version),
processId: pid,
hostname,
message: msg,
error: err,
});
try {
const { level, time, version, pid, hostname, msg, err } = JSON.parse(line);

return new Log({
line: index + 1,
level,
time: new Date(time),
version: Release.fromString(version),
processId: pid,
hostname,
message: msg,
error: err,
});

// Ignore problematic lines
} catch (err: unknown) {

}
}

public toString() {
Expand All @@ -57,7 +63,7 @@ class Log {
`"hostname":"${this.hostname}",` +
`"version":"${this.version.toString()}",` +
(this.error ? `"err":"${this.error}",` : '') +
`"msg":"${this.message}"` +
`"msg":${JSON.stringify(this.message)}` +
`}`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const doesFileExist = (filepath: string) => {
return fs.existsSync(filepath);
}

export const touchFile = async (filepath: string) => {
export const touchFile = (filepath: string) => {
if (!doesFileExist(filepath)) {
fs.mkdirSync(path.dirname(filepath), { recursive: true });

Expand Down
15 changes: 13 additions & 2 deletions src/utils/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { NEW_LINE_REGEXP } from '../constants';
import logger from '../logger';
import Release from '../models/Release';
import { readFile } from './file';
import pino from 'pino';

const LEVEL_MAP = pino.levels.values;


export const parseLogs = async (filepath: string, since?: Date | Release) => {

export const parseLogs = async (filepath: string, since?: Date | Release, level?: pino.Level) => {
logger.trace(`Reading logs...`);

if (since) {
Expand All @@ -17,16 +20,24 @@ export const parseLogs = async (filepath: string, since?: Date | Release) => {
}
}

if (level) {
logger.debug(`Keeping logs with level greater than or equal to: ${LEVEL_MAP[level]} (${level.toUpperCase()})`);
}

const file = await readFile(filepath);

const logs: Log[] = file
const rawLogs = file
.split(NEW_LINE_REGEXP)
.filter((line: string) => {
return line.startsWith('{') && line.endsWith('}');
})
.map((line: string, index: number) => {
return Log.fromText(line, index);
})
.filter(Boolean) as Log[];

const logs = rawLogs
.filter((log: Log) => level === undefined || log.getLevel() >= LEVEL_MAP[level])
.filter((log: Log) => {
if (since instanceof Date) {
return log.getTime() >= since;
Expand Down

0 comments on commit 5f689a8

Please sign in to comment.