From 5018bcbffd94047e89e42b818c7a3882abfc2e23 Mon Sep 17 00:00:00 2001 From: Jeremy Fehr Date: Mon, 8 Jul 2024 16:30:30 -0700 Subject: [PATCH] fix: parse structured logs, and handle ANSI escape codes in logs --- .eslintrc.json | 5 ++++- src/logger.ts | 19 +++++++++++++++---- test/logger.ts | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index eb23147b..1281c0ee 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,3 +1,6 @@ { - "extends": "./node_modules/gts" + "extends": "./node_modules/gts", + "rules": { + "no-control-regex": 0 + } } diff --git a/src/logger.ts b/src/logger.ts index 90595f78..b9d4e219 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -127,14 +127,22 @@ export function getModifiedData( 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 (!(SEVERITY in dataWithContext)) { + dataWithContext[SEVERITY] = stderr ? 'ERROR' : 'INFO'; + } } else { dataWithContext = getTextWithContext(processedData, currentContext); - } - if (stderr) { - dataWithContext[SEVERITY] = 'ERROR'; + dataWithContext[SEVERITY] = stderr ? 'ERROR' : 'INFO'; } return JSON.stringify(dataWithContext) + '\n'; @@ -178,6 +186,9 @@ function processData(data: Uint8Array | string, encoding?: BufferEncoding) { return {isJSON: false, processedData: data}; } + // strip any leading ANSI terminal codes from the start of 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) { diff --git a/test/logger.ts b/test/logger.ts index 732598b4..0111d7f1 100644 --- a/test/logger.ts +++ b/test/logger.ts @@ -148,4 +148,42 @@ describe('getModifiedData', () => { ) + '\n'; assert.equal(modifiedData, expectedOutput); }); + + it('parses firebase log severity', () => { + const modifiedData = ( + getModifiedData('testing info log level', undefined, false) + ); + assert.equal('INFO', JSON.parse(modifiedData)['severity']); + assert.equal('testing info log level', JSON.parse(modifiedData)['message']); + }); + + it('parses firebase warning severity', () => { + const modifiedData = ( + 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', () => { + const modifiedData = ( + 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'] + ); + }); });