Skip to content

fix: parse structured logs, and handle ANSI escape codes in logs #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "./node_modules/gts"
"extends": "./node_modules/gts",
"rules": {
"no-control-regex": 0
}
}
21 changes: 17 additions & 4 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@
}

export const errorHandler = (
err: Error | any,

Check warning on line 95 in src/logger.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
req: express.Request,
res: express.Response,
next: express.NextFunction

Check warning on line 98 in src/logger.ts

View workflow job for this annotation

GitHub Actions / lint

'next' is defined but never used
) => {
interceptStderrWrite();
res.status(500);
res.render('error', {error: err});
};

export function splitArgs(args: any[]) {

Check warning on line 105 in src/logger.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
let encoding, cb;
if (
args.length > 0 &&
Expand All @@ -127,14 +127,24 @@
return data;
}
const {isJSON, processedData} = processData(data, encoding);
let dataWithContext;

let dataWithContext: {
message: string | Uint8Array;
'logging.googleapis.com/labels': {execution_id: string | undefined};
'logging.googleapis.com/trace': string | undefined;
'logging.googleapis.com/spanId': string | undefined;
severity?: string | undefined;
};
if (isJSON) {
dataWithContext = getJSONWithContext(processedData, currentContext);
if (stderr && !(SEVERITY in dataWithContext)) {
dataWithContext[SEVERITY] = 'ERROR';
}
} else {
dataWithContext = getTextWithContext(processedData, currentContext);
}
if (stderr) {
dataWithContext[SEVERITY] = 'ERROR';
if (stderr) {
dataWithContext[SEVERITY] = 'ERROR';
}
}

return JSON.stringify(dataWithContext) + '\n';
Expand All @@ -152,7 +162,7 @@
};
}

function getJSONWithContext(json: any, context: ExecutionContext) {

Check warning on line 165 in src/logger.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (EXECUTION_CONTEXT_LABELS_KEY in json) {
json[EXECUTION_CONTEXT_LABELS_KEY]['execution_id'] = context.executionId;
} else {
Expand All @@ -178,6 +188,9 @@
return {isJSON: false, processedData: data};
}

// strip any leading ANSI terminal codes from the decoded data
// before trying to parse it as json
decodedData = decodedData.replace(/\x1b[[(?);]{0,2}(;?\d)*./g, '');
try {
return {isJSON: true, processedData: JSON.parse(decodedData)};
} catch (e) {
Expand Down
30 changes: 30 additions & 0 deletions test/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,34 @@ describe('getModifiedData', () => {
) + '\n';
assert.equal(modifiedData, expectedOutput);
});

it('parses firebase warning severity and message', () => {
const modifiedData = <string>(
getModifiedData(
'\u001b[33m{"severity":"WARNING","message":"testing warning log level"}\u001b[39m\n',
undefined,
true
)
);
assert.equal('WARNING', JSON.parse(modifiedData)['severity']);
assert.equal(
'testing warning log level',
JSON.parse(modifiedData)['message']
);
});

it('parses firebase error severity and message', () => {
const modifiedData = <string>(
getModifiedData(
'\u001b[31m{"severity":"ERROR","message":"testing error log level"}\u001b[39m\n',
undefined,
true
)
);
assert.equal('ERROR', JSON.parse(modifiedData)['severity']);
assert.equal(
'testing error log level',
JSON.parse(modifiedData)['message']
);
});
});
Loading