|
|
@@ -1,23 +1,36 @@
|
|
|
import { Logger as TypeOrmLoggerInterface, QueryRunner } from 'typeorm';
|
|
|
+import { LoggerOptions } from 'typeorm/logger/LoggerOptions';
|
|
|
|
|
|
import { Logger } from './vendure-logger';
|
|
|
|
|
|
const context = 'TypeORM';
|
|
|
|
|
|
+type typeOrmLogLevel = Exclude<LoggerOptions, 'all' | boolean>[number];
|
|
|
+
|
|
|
+const defaultLoggerOptions: LoggerOptions = ['error', 'warn', 'schema', 'migration'];
|
|
|
+
|
|
|
/**
|
|
|
* A custom logger for TypeORM which delegates to the Vendure Logger service.
|
|
|
*/
|
|
|
export class TypeOrmLogger implements TypeOrmLoggerInterface {
|
|
|
+ constructor(private options: LoggerOptions = defaultLoggerOptions) {}
|
|
|
+
|
|
|
log(level: 'log' | 'info' | 'warn', message: any, queryRunner?: QueryRunner): any {
|
|
|
switch (level) {
|
|
|
case 'info':
|
|
|
- Logger.info(message, context);
|
|
|
+ if (this.shouldDisplay('info')) {
|
|
|
+ Logger.info(message, context);
|
|
|
+ }
|
|
|
break;
|
|
|
case 'log':
|
|
|
- Logger.verbose(message, context);
|
|
|
+ if (this.shouldDisplay('log')) {
|
|
|
+ Logger.info(message, context);
|
|
|
+ }
|
|
|
break;
|
|
|
case 'warn':
|
|
|
- Logger.warn(message, context);
|
|
|
+ if (this.shouldDisplay('warn')) {
|
|
|
+ Logger.warn(message, context);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -27,18 +40,53 @@ export class TypeOrmLogger implements TypeOrmLoggerInterface {
|
|
|
}
|
|
|
|
|
|
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner): any {
|
|
|
- Logger.debug(`Query: "${query}" -- [${parameters}]`, context);
|
|
|
+ if (this.shouldDisplay('query')) {
|
|
|
+ const sql = this.formatQueryWithParams(query, parameters);
|
|
|
+ Logger.debug(`Query: ${sql}`, context);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner): any {
|
|
|
- Logger.error(`Query error: ${error}, "${query}" -- [${parameters}]`, context);
|
|
|
+ if (this.shouldDisplay('error')) {
|
|
|
+ const sql = this.formatQueryWithParams(query, parameters);
|
|
|
+ Logger.error(`Query error: ${sql}`, context);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner): any {
|
|
|
- Logger.warn(`Slow query (${time}): "${query}" -- [${parameters}]`, context);
|
|
|
+ const sql = this.formatQueryWithParams(query, parameters);
|
|
|
+ Logger.warn(`Query is slow: ` + sql);
|
|
|
+ Logger.warn(`Execution time: ` + time);
|
|
|
}
|
|
|
|
|
|
logSchemaBuild(message: string, queryRunner?: QueryRunner): any {
|
|
|
- Logger.info(message, context);
|
|
|
+ if (this.shouldDisplay('schema')) {
|
|
|
+ Logger.info(message, context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private shouldDisplay(logType: typeOrmLogLevel): boolean {
|
|
|
+ return (
|
|
|
+ this.options === 'all' ||
|
|
|
+ this.options === true ||
|
|
|
+ (Array.isArray(this.options) && this.options.includes(logType))
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private formatQueryWithParams(query: string, parameters?: any[]) {
|
|
|
+ return query + (parameters?.length ? ' -- PARAMETERS: ' + this.stringifyParams(parameters) : '');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Converts parameters to a string.
|
|
|
+ * Sometimes parameters can have circular objects and therefor we are handle this case too.
|
|
|
+ */
|
|
|
+ private stringifyParams(parameters: any[]) {
|
|
|
+ try {
|
|
|
+ return JSON.stringify(parameters);
|
|
|
+ } catch (error) {
|
|
|
+ // most probably circular objects in parameters
|
|
|
+ return parameters;
|
|
|
+ }
|
|
|
}
|
|
|
}
|