Skip to content

Instantly share code, notes, and snippets.

@Diluka
Created July 7, 2020 02:33
Show Gist options
  • Save Diluka/110b0e9e07609bce2957fe451d239ad2 to your computer and use it in GitHub Desktop.
Save Diluka/110b0e9e07609bce2957fe451d239ad2 to your computer and use it in GitHub Desktop.
Integrate the nestjs logger into typeorm
/*
* Created by Diluka on 2020-06-29.
*
*
* ----------- 神 兽 佑 我 -----------
* ┏┓ ┏┓+ +
* ┏┛┻━━━━━━┛┻┓ + +
* ┃ ┃
* ┣ ━ ┃ ++ + + +
* ████━████ ┃+
* ┃ ┃ +
* ┃ ┴ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛ Code is far away from bug
* ┃ ┃ with the animal protecting
* ┃ ┃ + + + +
* ┃ ┃
* ┃ ┃ +
* ┃ ┃ + +
* ┃ ┃ +
* ┃ ┗━━━┓ + +
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
* ----------- 永 无 BUG ------------
*/
import { Logger } from '@nestjs/common';
import { Logger as ITypeORMLogger, QueryRunner } from 'typeorm';
import { LoggerOptions } from 'typeorm/logger/LoggerOptions';
/**
* Performs logging of the events in TypeORM.
* This version of logger uses console to log events and does not use syntax highlighting.
*/
export class TypeORMLogger implements ITypeORMLogger {
private logger = new Logger('TypeORM');
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(private options: LoggerOptions) {
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Logs query and parameters used in it.
*/
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
if (this.options === 'all' || this.options === true || (Array.isArray(this.options) && this.options.indexOf('query') !== -1)) {
const sql = query + (parameters && parameters.length ? ' -- PARAMETERS: ' + this.stringifyParams(parameters) : '');
this.logger.debug('query' + ': ' + sql);
}
}
/**
* Logs query that is failed.
*/
logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) {
if (this.options === 'all' || this.options === true || (Array.isArray(this.options) && this.options.indexOf('error') !== -1)) {
const sql = query + (parameters && parameters.length ? ' -- PARAMETERS: ' + this.stringifyParams(parameters) : '');
this.logger.error(`query failed: ` + sql);
this.logger.error(`error:`, error);
}
}
/**
* Logs query that is slow.
*/
logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) {
const sql = query + (parameters && parameters.length ? ' -- PARAMETERS: ' + this.stringifyParams(parameters) : '');
this.logger.warn(`query is slow: ` + sql);
this.logger.warn(`execution time: ` + time);
}
/**
* Logs events from the schema build process.
*/
logSchemaBuild(message: string, queryRunner?: QueryRunner) {
if (this.options === 'all' || (Array.isArray(this.options) && this.options.indexOf('schema') !== -1)) {
this.logger.debug(message);
}
}
/**
* Logs events from the migrations run process.
*/
logMigration(message: string, queryRunner?: QueryRunner) {
this.logger.debug(message);
}
/**
* Perform logging using given logger, or by default to the console.
* Log has its own level and message.
*/
log(level: 'log' | 'info' | 'warn', message: any, queryRunner?: QueryRunner) {
switch (level) {
case 'log':
if (this.options === 'all' || (Array.isArray(this.options) && this.options.indexOf('log') !== -1))
this.logger.debug(message);
break;
case 'info':
if (this.options === 'all' || (Array.isArray(this.options) && this.options.indexOf('info') !== -1))
this.logger.log(message);
break;
case 'warn':
if (this.options === 'all' || (Array.isArray(this.options) && this.options.indexOf('warn') !== -1))
this.logger.warn(message);
break;
}
}
// -------------------------------------------------------------------------
// 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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment