Skip to content

Commit d9e5016

Browse files
committed
Core: Improve DX for circular refs and deep log context
1 parent 96303c4 commit d9e5016

File tree

4 files changed

+362
-346
lines changed

4 files changed

+362
-346
lines changed

packages/core/src/base.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ const defaultOptions: ILogtailOptions = {
3232
contextObjectMaxDepth: 50,
3333

3434
// produce a warn log when context object max depth is reached
35-
contextObjectMaxDepthWarn: true
35+
contextObjectMaxDepthWarn: true,
36+
37+
// produce a warning when circular reference is found in context object
38+
contextObjectCircularRefWarn: true,
3639
};
3740

3841
/**

packages/node/src/node.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class Node extends Base {
5252
*
5353
* @param message: string - Log message
5454
* @param level (LogLevel) - Level to log at (debug|info|warn|error)
55-
* @param log: (Partial<ILogtailLog>) - Initial log (optional)
55+
* @param context: (Context) - Log context for passing structured data
5656
* @returns Promise<ILogtailLog> after syncing
5757
*/
5858
public async log<TContext extends Context>(
@@ -104,12 +104,15 @@ export class Node extends Base {
104104
return value.toISOString();
105105
} else if ((typeof value === "object" || Array.isArray(value)) && (maxDepth < 1 || visitedObjects.has(value))) {
106106
if (visitedObjects.has(value)) {
107-
console.warn(`[Logtail] Found a circular reference when serializing logs. Please do not use circular references in your logs.`);
108-
} else if (this._options.contextObjectMaxDepthWarn) {
107+
if (this._options.contextObjectCircularRefWarn) {
108+
console.warn(`[Logtail] Found a circular reference when serializing logs. Please do not use circular references in your logs.`);
109+
}
110+
return '<omitted circular reference>'
111+
}
112+
if (this._options.contextObjectMaxDepthWarn) {
109113
console.warn(`[Logtail] Max depth of ${this._options.contextObjectMaxDepth} reached when serializing logs. Please do not use excessive object depth in your logs.`);
110114
}
111-
112-
return value.toString(); // results in "[object Object]"
115+
return `<omitted context beyond configured max depth: ${this._options.contextObjectMaxDepth}>`
113116
} else if (Array.isArray(value)) {
114117
visitedObjects.add(value);
115118
const sanitizedArray = value.map((item) => this.sanitizeForEncoding(item, maxDepth-1, visitedObjects));

packages/types/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export interface ILogtailOptions {
3939
* Boolean to produce a warning when context object max depth is reached
4040
**/
4141
contextObjectMaxDepthWarn: boolean;
42+
43+
/**
44+
* Boolean to produce a warning when circular reference is found in context
45+
**/
46+
contextObjectCircularRefWarn: boolean;
4247
}
4348

4449
export enum LogLevel {

0 commit comments

Comments
 (0)