Skip to content

Commit

Permalink
fix(logger): do not crash on circular json
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Aug 10, 2022
1 parent 350e7dc commit 3fdd200
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/logger/src/console.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { format, transports } from 'winston';
import * as rTracer from 'cls-rtracer';
import { globalLoggerCtxKey, traceFormatter } from './utils';
import { getCircularReplacer, globalLoggerCtxKey, traceFormatter } from './utils';

export interface RestoreLoggerConsoleTransportOptions extends transports.ConsoleTransportOptions {
prettyPrint?: boolean | any;
Expand All @@ -23,10 +23,10 @@ function createTracerFormat(opts: RestoreLoggerConsoleTransportOptions) {
delete info.timestamp;
let object = {};
if (splat) {
object = JSON.stringify(splat);
object = JSON.stringify(splat, getCircularReplacer());
}
if (message && Object.entries(message).length !== 0 && message.constructor === Object) {
message = JSON.stringify(message);
message = JSON.stringify(message, getCircularReplacer());
}
let ret: string[] = [];
ret.push(`${level}: ${time}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/logger/src/elasticsearch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ElasticsearchTransport, ElasticsearchTransportOptions, Transformer } from 'winston-elasticsearch';
import * as os from 'os';
import * as rTracer from 'cls-rtracer';
import { globalLoggerCtxKey, getRealTrace } from './utils';
import { globalLoggerCtxKey, getRealTrace, getCircularReplacer } from './utils';

export const indexTemplate = require('../elasticsearch-index-template.json');

Expand Down Expand Up @@ -42,7 +42,7 @@ function createTransformer(opts: RestoreLoggerElasticsearchTransportOptions) {
transformed.source_host = os.hostname();
transformed.message = logData.message;
if (typeof transformed.message === 'object') {
transformed.message = JSON.stringify(transformed.message);
transformed.message = JSON.stringify(transformed.message, getCircularReplacer());
}
transformed.severity = logData.level;
transformed.fields = logData.meta;
Expand Down
13 changes: 13 additions & 0 deletions packages/logger/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,16 @@ export const getRealTrace = (): any => {

// A symbol used as key in the global name space to put the AsyncLocalStorage store under
export const globalLoggerCtxKey = Symbol('loggerCtx');

export const getCircularReplacer = () => {
const seen = new WeakSet();
return (key: string, value: unknown) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
6 changes: 6 additions & 0 deletions packages/logger/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ describe('a logger', () => {
it('an error with stack trace', (done) => {
logger.error('Generic Error!');
done();
});
it('a circular object', (done) => {
const obj: any = {name: "Bob"};
obj.child = obj;
logger.info(obj);
done();
});
esTransport.bulkWriter.stop();
});
Expand Down

0 comments on commit 3fdd200

Please sign in to comment.