Skip to content

Commit 5f689a8

Browse files
committed
Fix: correctly stringify log messages.
1 parent 80194d5 commit 5f689a8

8 files changed

Lines changed: 38 additions & 266605 deletions

File tree

data/production/logs/2023.12.07T00.00.00Z.log

Lines changed: 0 additions & 265973 deletions
Large diffs are not rendered by default.

data/production/logs/app.log

Lines changed: 1 addition & 612 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "berlin-lea-performance-monitor",
3-
"version": "1.20.0",
3+
"version": "1.20.1",
44
"author": "David Leclerc",
55
"main": "./src/index.ts",
66
"scripts": {

src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const getLoggerByUseCase = () => {
4949
// When polling, output all logs to file and terminal
5050
if (POLL) {
5151
return pino({
52-
level: 'trace',
52+
level: 'debug',
5353
formatters: FORMATTERS,
5454
timestamp: pino.stdTimeFunctions.isoTime,
5555
}, pino.transport({

src/models/logs/Log.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,24 @@ class Log {
3535
}
3636

3737
public static fromText(line: string, index: number) {
38-
const { level, time, version, pid, hostname, msg, err } = JSON.parse(line);
39-
40-
return new Log({
41-
line: index + 1,
42-
level,
43-
time: new Date(time),
44-
version: Release.fromString(version),
45-
processId: pid,
46-
hostname,
47-
message: msg,
48-
error: err,
49-
});
38+
try {
39+
const { level, time, version, pid, hostname, msg, err } = JSON.parse(line);
40+
41+
return new Log({
42+
line: index + 1,
43+
level,
44+
time: new Date(time),
45+
version: Release.fromString(version),
46+
processId: pid,
47+
hostname,
48+
message: msg,
49+
error: err,
50+
});
51+
52+
// Ignore problematic lines
53+
} catch (err: unknown) {
54+
55+
}
5056
}
5157

5258
public toString() {
@@ -57,7 +63,7 @@ class Log {
5763
`"hostname":"${this.hostname}",` +
5864
`"version":"${this.version.toString()}",` +
5965
(this.error ? `"err":"${this.error}",` : '') +
60-
`"msg":"${this.message}"` +
66+
`"msg":${JSON.stringify(this.message)}` +
6167
`}`);
6268
}
6369

src/utils/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const doesFileExist = (filepath: string) => {
5959
return fs.existsSync(filepath);
6060
}
6161

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

src/utils/parsing.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import { NEW_LINE_REGEXP } from '../constants';
33
import logger from '../logger';
44
import Release from '../models/Release';
55
import { readFile } from './file';
6+
import pino from 'pino';
67

8+
const LEVEL_MAP = pino.levels.values;
79

810

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

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

23+
if (level) {
24+
logger.debug(`Keeping logs with level greater than or equal to: ${LEVEL_MAP[level]} (${level.toUpperCase()})`);
25+
}
26+
2027
const file = await readFile(filepath);
2128

22-
const logs: Log[] = file
29+
const rawLogs = file
2330
.split(NEW_LINE_REGEXP)
2431
.filter((line: string) => {
2532
return line.startsWith('{') && line.endsWith('}');
2633
})
2734
.map((line: string, index: number) => {
2835
return Log.fromText(line, index);
2936
})
37+
.filter(Boolean) as Log[];
38+
39+
const logs = rawLogs
40+
.filter((log: Log) => level === undefined || log.getLevel() >= LEVEL_MAP[level])
3041
.filter((log: Log) => {
3142
if (since instanceof Date) {
3243
return log.getTime() >= since;

0 commit comments

Comments
 (0)