fixed issue when logger cant handle circular parameters

This commit is contained in:
Umed Khudoiberdiev 2016-11-26 15:26:18 +05:00
parent bdf8779a1c
commit 68f48f1c47

View File

@ -22,7 +22,7 @@ export class Logger {
logQuery(query: string, parameters?: any[]) {
if (this.options.logQueries ||
process.env.LOGGER_CLI_SCHEMA_SYNC)
this.log("log", `executing query: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + JSON.stringify(parameters) : ""}`);
this.log("log", `executing query: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : ""}`);
}
/**
@ -32,7 +32,7 @@ export class Logger {
if (this.options.logQueries ||
this.options.logOnlyFailedQueries ||
process.env.LOGGER_CLI_SCHEMA_SYNC)
this.log("error", `query failed: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + JSON.stringify(parameters) : ""}`);
this.log("error", `query failed: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : ""}`);
}
/**
@ -80,4 +80,21 @@ export class Logger {
}
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Converts parameters to a string.
* Sometimes parameters can have circular objects and therefor we are handle this case too.
*/
protected stringifyParams(parameters: any[]) {
try {
return JSON.stringify(parameters);
} catch (error) { // most probably circular objects in parameters
return parameters;
}
}
}