import debug from 'debug' ; import chalk from 'chalk' ; import { v4 } from 'uuid' ; export enum TRANSFORM_TYPE { GET = 'GET' , PUT = 'PUT' , UPDATE = 'UPDATE' , DELETE = 'DELETE' , QUERY = 'QUERY' , RESPONSE = 'RESPONSE' } export enum MANAGER_NAME { ENTITY_MANAGER = 'ENTITY MANAGER' , TRANSACTION_MANAGER = 'TRANSACTION MANAGER' , BATCH_MANAGER = 'BATCH MANAGER' , SCAN_MANAGER = 'SCAN_MANAGER' } export enum TRANSFORM_TRANSACTION_TYPE { TRANSACTION_WRITE = 'TRANSACTION_WRITE' , TRANSACTION_READ = 'TRANSACTION_READ' } export enum TRANSFORM_SCAN_TYPE { SCAN = 'SCAN' , PARALLEL_SCAN = 'PARALLEL_SCAN' } export enum TRANSFORM_BATCH_TYPE { BATCH_WRITE = 'BATCH_WRITE' , BATCH_READ = 'BATCH_READ' } export enum STATS_TYPE { CONSUMED_CAPACITY = 'CONSUMED_CAPACITY' } export class DebugLogger { private debugTransformLog = debug ( 'typedorm:transform:log' ) ; private debugTransformBatchLog = debug ( 'typedorm:transform:batch:log' ) ; private debugTransformTransactionLog = debug ( 'typedorm:transform:transaction:log' ) ; private debugTransformScanLog = debug ( 'typedorm:transform:scan:log' ) ; private debugInfoLog = debug ( 'typedorm:info:log' ) ; private debugWarnLog = debug ( 'typedorm:warn:log' ) ; private debugErrorLog = debug ( 'typedorm:error:log' ) ; private debugStatsLog = debug ( 'typedorm:stats:log' ) ; returns getRequestId ( ) { return v4 ( ) ; } logTransform ( { requestId , operation , prefix , entityName , primaryKey , body , options , } ) { if ( this . debugTransformLog . enabled ) { this . debugTransformLog ( `${ chalk . bold . bgCyanBright ( requestId ) } ${ chalk . green ( operation ) } ${ chalk . blue ( entityName ) } ${ chalk . magenta ( prefix ) }:` , ... ( primaryKey ? [ chalk . blueBright ( '\nPrimary key: ' ) , chalk . white ( this . ensurePrintable ( primaryKey ) ) , ] : [ ] ) , ... ( body ? [ chalk . blueBright ( '\nBody: ' ) , chalk . white ( this . ensurePrintable ( body ) ) , ] : [ ] ) , ... ( options ? [ chalk . blueBright ( '\nOptions: ' ) , chalk . white ( this . ensurePrintable ( options ) ) , ] : [ ] ) ) ; } } logTransformBatch ( { requestId , operation , prefix , body , options , } ) { if ( this . debugTransformBatchLog . enabled ) { this . debugTransformBatchLog ( `${ chalk . bold . bgCyanBright ( requestId ) } ${ chalk . green ( operation ) } ${ chalk . magenta ( prefix ) }:` , ... ( body ? [ chalk . blueBright ( '\nBody: ' ) , chalk . white ( this . ensurePrintable ( body ) ) , ] : [ ] ) , ... ( options ? [ chalk . blueBright ( '\nOptions: ' ) , chalk . white ( this . ensurePrintable ( options ) ) , ] : [ ] ) ) ; } } logTransformTransaction ( { requestId , operation , prefix , body , options , } ) { if ( this . debugTransformTransactionLog . enabled ) { this . debugTransformTransactionLog ( `${ chalk . bold . bgCyanBright ( requestId ) } ${ chalk . green ( operation ) } ${ chalk . magenta ( prefix ) }:` , ... ( body ? [ chalk . blueBright ( '\nBody: ' ) , chalk . white ( this . ensurePrintable ( body ) ) , ] : [ ] ) , ... ( options ? [ chalk . blueBright ( '\nOptions: ' ) , chalk . white ( this . ensurePrintable ( options ) ) , ] : [ ] ) ) ; } } logTransformScan ( { requestId , prefix , operation , body , options , } ) { if ( this . debugTransformScanLog . enabled ) { this . debugTransformScanLog ( `${ chalk . bold . bgCyanBright ( requestId ) } ${ chalk . green ( operation ) } ${ chalk . magenta ( prefix ) }:` , ... ( body ? [ chalk . blueBright ( '\nBody: ' ) , chalk . white ( this . ensurePrintable ( body ) ) , ] : [ ] ) , ... ( options ? [ chalk . blueBright ( '\nOptions: ' ) , chalk . white ( this . ensurePrintable ( options ) ) , ] : [ ] ) ) ; } } logStats ( { requestId , requestSegment , scope , statsType , consumedCapacityData , } ) { if ( this . debugStatsLog . enabled ) { this . debugStatsLog ( `${ chalk . bold . bgCyanBright ( requestId ) + ( requestSegment ? ':' + chalk . bold . bgCyanBright ( requestId ) : '' ) } ${ chalk . green ( scope ) } ${ chalk . magenta ( statsType ) }:` , chalk . white ( this . ensurePrintable ( consumedCapacityData ) ) ) ; } } logInfo ( { requestId , scope , log , } ) { if ( this . debugInfoLog . enabled ) { this . debugInfoLog ( `${ chalk . bold . bgCyanBright ( requestId ) } ${ chalk . green ( scope ) }:` , chalk . white ( this . ensurePrintable ( log ) ) ) ; } } logWarn ( { requestId , scope , log , } ) { if ( this . debugWarnLog . enabled ) { this . debugWarnLog ( `${ chalk . bold . bgCyanBright ( requestId ) } ${ chalk . green ( scope ) }:` , chalk . yellow ( this . ensurePrintable ( log ) ) ) ; } } logError ( { requestId , scope , log , } ) { if ( this . debugErrorLog . enabled ) { this . debugErrorLog ( `${ chalk . bold . bgCyanBright ( requestId ) } ${ chalk . green ( scope ) }:` , chalk . red ( this . ensurePrintable ( log ) ) ) ; } } private ensurePrintable ( log ) { if ( typeof log === 'object' && log !== null ) { return `\n${ JSON . stringify ( log , null , 2 ) }\n` ; } else return log ; } }