Skip to content

Commit

Permalink
feat(core): Use Logger to log TypeORM logs
Browse files Browse the repository at this point in the history
Relates to #86
  • Loading branch information
michaelbromley committed Apr 17, 2019
1 parent 65445cb commit 5966bec
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { NoAssetPreviewStrategy } from './asset-preview-strategy/no-asset-previe
import { NoAssetStorageStrategy } from './asset-storage-strategy/no-asset-storage-strategy';
import { AutoIncrementIdStrategy } from './entity-id-strategy/auto-increment-id-strategy';
import { DefaultLogger } from './logger/default-logger';
import { TypeOrmLogger } from './logger/typeorm-logger';
import { MergeOrdersStrategy } from './order-merge-strategy/merge-orders-strategy';
import { UseGuestStrategy } from './order-merge-strategy/use-guest-strategy';
import { defaultPromotionActions } from './promotion/default-promotion-actions';
Expand Down Expand Up @@ -53,6 +54,7 @@ export const defaultConfig: ReadOnlyRequired<VendureConfig> = {
},
dbConnectionOptions: {
type: 'mysql',
logger: new TypeOrmLogger(),
},
promotionOptions: {
promotionConditions: defaultPromotionConditions,
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/config/logger/default-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ export class DefaultLogger implements VendureLogger {
}
}

error(message: string, context: string | undefined = DEFAULT_CONTEXT, trace?: string | undefined): void {
error(message: string, context?: string, trace?: string | undefined): void {
if (this.level >= LogLevel.Error) {
this.logMessage(
chalk.red(`error`),
chalk.red(message + trace ? ` trace: \n${trace}` : ''),
chalk.red(message + (trace ? ` trace: \n${trace}` : '')),
context,
);
}
}
warn(message: string, context: string | undefined = DEFAULT_CONTEXT): void {
warn(message: string, context?: string): void {
if (this.level >= LogLevel.Warn) {
this.logMessage(
chalk.yellow(`warn`),
Expand All @@ -89,7 +89,7 @@ export class DefaultLogger implements VendureLogger {
);
}
}
info(message: string, context: string | undefined = DEFAULT_CONTEXT): void {
info(message: string, context?: string): void {
if (this.level >= LogLevel.Info) {
this.logMessage(
chalk.blue(`info`),
Expand All @@ -98,7 +98,7 @@ export class DefaultLogger implements VendureLogger {
);
}
}
verbose(message: string, context: string | undefined = DEFAULT_CONTEXT): void {
verbose(message: string, context?: string): void {
if (this.level >= LogLevel.Verbose) {
this.logMessage(
chalk.magenta(`verbose`),
Expand All @@ -107,7 +107,7 @@ export class DefaultLogger implements VendureLogger {
);
}
}
debug(message: string, context: string | undefined = DEFAULT_CONTEXT): void {
debug(message: string, context?: string): void {
if (this.level >= LogLevel.Debug) {
this.logMessage(
chalk.magenta(`debug`),
Expand Down
44 changes: 44 additions & 0 deletions packages/core/src/config/logger/typeorm-logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Logger as TypeOrmLoggerInterface, QueryRunner } from 'typeorm';

import { Logger } from './vendure-logger';

const context = 'TypeORM';

/**
* A custom logger for TypeORM which delegates to the Vendure Logger service.
*/
export class TypeOrmLogger implements TypeOrmLoggerInterface {
log(level: 'log' | 'info' | 'warn', message: any, queryRunner?: QueryRunner): any {
switch (level) {
case 'info':
Logger.info(message, context);;
break;
case 'log':
Logger.verbose(message, context);;
break;
case 'warn':
Logger.warn(message, context);;
break;
}
}

logMigration(message: string, queryRunner?: QueryRunner): any {
Logger.info(message, context);
}

logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner): any {
Logger.debug(`Query: "${query}" -- [${parameters}]`, context);
}

logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner): any {
Logger.error(`Query error: ${error}, "${query}" -- [${parameters}]`, context);
}

logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner): any {
Logger.warn(`Slow query (${time}): "${query}" -- [${parameters}]`, context);
}

logSchemaBuild(message: string, queryRunner?: QueryRunner): any {
Logger.info(message, context);
}
}

0 comments on commit 5966bec

Please sign in to comment.