Skip to content

Commit 68f1ef3

Browse files
authored
feat(logging): improve client-side error logging format in CloudWatch (#173)
- Add global console.error override to format HttpErrorResponse objects - Create structured JSON logs matching backend Pino format - Add 'source: client' field to differentiate frontend from backend logs - Prevent verbose 30+ line error expansions in CloudWatch - Enable queryable error logs by status_code, err.type, and source LFXV2-782 Generated with [Claude Code](https://claude.ai/code) Signed-off-by: Asitha de Silva <[email protected]>
1 parent a22d326 commit 68f1ef3

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright The Linux Foundation and each contributor to LFX.
2+
// SPDX-License-Identifier: MIT
3+
4+
import { HttpErrorResponse } from '@angular/common/http';
5+
6+
/**
7+
* Overrides console.error to automatically format HttpErrorResponse objects
8+
* This prevents verbose object expansion in CloudWatch logs and matches backend Pino log format
9+
*/
10+
export function initializeConsoleOverride(): void {
11+
const originalConsoleError = console.error;
12+
13+
console.error = (...args: any[]) => {
14+
const formattedArgs = args.map((arg) => {
15+
// Format HttpErrorResponse objects to match backend logging structure
16+
if (arg instanceof HttpErrorResponse) {
17+
return formatHttpError(arg);
18+
}
19+
return arg;
20+
});
21+
22+
originalConsoleError.apply(console, formattedArgs);
23+
};
24+
}
25+
26+
/**
27+
* Formats HttpErrorResponse into structured error object matching backend Pino format
28+
* Uses 'err' field convention consistent with server-side logging
29+
*/
30+
function formatHttpError(error: HttpErrorResponse): Record<string, any> {
31+
// Extract backend error details if available
32+
let errorMessage = error.statusText;
33+
let errorCode: string | undefined;
34+
35+
if (error.error) {
36+
if (typeof error.error === 'string') {
37+
errorMessage = error.error;
38+
} else if (error.error.error) {
39+
errorMessage = error.error.error;
40+
errorCode = error.error.code;
41+
} else if (error.error.message) {
42+
errorMessage = error.error.message;
43+
}
44+
}
45+
46+
// Return structured error object matching backend format with client-side indicator
47+
return {
48+
source: 'client',
49+
err: {
50+
type: 'HttpErrorResponse',
51+
message: errorMessage,
52+
statusCode: error.status,
53+
statusText: error.statusText,
54+
url: error.url || undefined,
55+
code: errorCode,
56+
},
57+
status_code: error.status,
58+
url: error.url || undefined,
59+
};
60+
}

apps/lfx-one/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Copyright The Linux Foundation and each contributor to LFX.
22
// SPDX-License-Identifier: MIT
33

4+
// Initialize console override BEFORE any other imports to catch all errors
5+
import { initializeConsoleOverride } from './app/shared/utils/console-override';
6+
7+
initializeConsoleOverride();
8+
49
import { bootstrapApplication } from '@angular/platform-browser';
510
import '@linuxfoundation/lfx-ui-core';
611

0 commit comments

Comments
 (0)