-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18c64df
commit fb657c6
Showing
6 changed files
with
349 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import databaseConfig from './database.config'; | ||
export * from './database-config.type'; | ||
export * from './typeorm-custom-logger'; | ||
|
||
export { databaseConfig }; |
208 changes: 208 additions & 0 deletions
208
packages/database-typeorm/src/config/typeorm-custom-logger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { Logger } from '@nestjs/common'; | ||
import { | ||
QueryRunner, | ||
Logger as TypeOrmLogger, | ||
type LogLevel, | ||
type LogMessageType, | ||
type LoggerOptions, | ||
} from 'typeorm'; | ||
|
||
export class TypeOrmCustomLogger implements TypeOrmLogger { | ||
static getInstance(connectionName: string, options: LoggerOptions) { | ||
const logger = new Logger(`TypeORM[${connectionName}]`); | ||
return new TypeOrmCustomLogger(logger, options); | ||
} | ||
|
||
constructor( | ||
private readonly logger: Logger, | ||
private readonly options: LoggerOptions, | ||
) {} | ||
|
||
/** | ||
* Logs query and parameters used in it. | ||
*/ | ||
logQuery(query: string, parameters?: any[], _queryRunner?: QueryRunner) { | ||
if (!this.isLogEnabledFor('query')) { | ||
return; | ||
} | ||
|
||
const sql = | ||
query + | ||
(parameters && parameters.length | ||
? ' -- PARAMETERS: ' + this.stringifyParams(parameters) | ||
: ''); | ||
this.logger.log(`query: ${sql}`); | ||
} | ||
|
||
/** | ||
* Logs query that is failed. | ||
*/ | ||
logQueryError( | ||
error: string, | ||
query: string, | ||
parameters?: any[], | ||
_queryRunner?: QueryRunner, | ||
) { | ||
if (!this.isLogEnabledFor('query-error')) { | ||
return; | ||
} | ||
|
||
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, | ||
) { | ||
if (!this.isLogEnabledFor('query-slow')) { | ||
return; | ||
} | ||
|
||
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.isLogEnabledFor('schema-build')) { | ||
return; | ||
} | ||
|
||
this.logger.log(message); | ||
} | ||
|
||
/** | ||
* Logs events from the migrations run process. | ||
*/ | ||
logMigration(message: string, _queryRunner?: QueryRunner) { | ||
if (!this.isLogEnabledFor('migration')) { | ||
return; | ||
} | ||
|
||
this.logger.log(message); | ||
} | ||
|
||
/** | ||
* Perform logging using given logger, or by default to the this.logger. | ||
* Log has its own level and message. | ||
*/ | ||
log( | ||
level: 'log' | 'info' | 'warn', | ||
message: any, | ||
_queryRunner?: QueryRunner, | ||
) { | ||
switch (level) { | ||
case 'log': | ||
if (!this.isLogEnabledFor('log')) { | ||
return; | ||
} | ||
|
||
this.logger.log(message); | ||
break; | ||
case 'info': | ||
if (!this.isLogEnabledFor('info')) { | ||
return; | ||
} | ||
|
||
this.logger.log(message); | ||
break; | ||
case 'warn': | ||
if (!this.isLogEnabledFor('warn')) { | ||
return; | ||
} | ||
|
||
this.logger.warn(message); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Check is logging for level or message type is enabled. | ||
*/ | ||
protected isLogEnabledFor(type?: LogLevel | LogMessageType) { | ||
switch (type) { | ||
case 'query': | ||
return ( | ||
this.options === 'all' || | ||
this.options === true || | ||
(Array.isArray(this.options) && this.options.indexOf('query') !== -1) | ||
); | ||
|
||
case 'error': | ||
case 'query-error': | ||
return ( | ||
this.options === 'all' || | ||
this.options === true || | ||
(Array.isArray(this.options) && this.options.indexOf('error') !== -1) | ||
); | ||
|
||
case 'query-slow': | ||
return true; | ||
|
||
case 'schema': | ||
case 'schema-build': | ||
return ( | ||
this.options === 'all' || | ||
(Array.isArray(this.options) && this.options.indexOf('schema') !== -1) | ||
); | ||
|
||
case 'migration': | ||
return true; | ||
|
||
case 'log': | ||
return ( | ||
this.options === 'all' || | ||
(Array.isArray(this.options) && this.options.indexOf('log') !== -1) | ||
); | ||
|
||
case 'info': | ||
return ( | ||
this.options === 'all' || | ||
(Array.isArray(this.options) && this.options.indexOf('info') !== -1) | ||
); | ||
|
||
case 'warn': | ||
return ( | ||
this.options === 'all' || | ||
(Array.isArray(this.options) && this.options.indexOf('warn') !== -1) | ||
); | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} | ||
} | ||
|
||
export default TypeOrmCustomLogger; |
Oops, something went wrong.