import type { Logger , LoggerNamespace , LogContext , LoggerOptions } from './Logger' ; import { Utils } from '../utils/Utils' ; import { colors } from './colors' ; export class DefaultLogger implements Logger { public debugMode = this . options . debugMode ?? false ; private readonly usesReplicas = this . options . usesReplicas ; private readonly highlighter = this . options . highlighter ; private readonly writer = this . options . writer ; constructor ( private readonly options ) { } inheritDoc log ( namespace , message , context ? ) { if ( ! this . isEnabled ( namespace ) ) { return ; } message = message . replace ( /\n/g , '' ) . replace ( / +/g , ' ' ) . trim ( ) ; if ( context ?. level === 'error' ) { message = colors . red ( message ) ; } if ( context ?. level === 'warning' ) { message = colors . yellow ( message ) ; } this . writer ( colors . grey ( `[${ namespace }] ` ) + message ) ; } inheritDoc error ( namespace , message , context ? ) { this . log ( namespace , message , { ... context , level : 'error' } ) ; } inheritDoc warn ( namespace , message , context ? ) { this . log ( namespace , message , { ... context , level : 'warning' } ) ; } inheritDoc setDebugMode ( debugMode ) { this . debugMode = debugMode ; } isEnabled ( namespace ) { return ! ! this . debugMode && ( ! Array . isArray ( this . debugMode ) || this . debugMode . includes ( namespace ) ) ; } inheritDoc logQuery ( context ) { if ( ! this . isEnabled ( 'query' ) ) { return ; } const query = this . highlighter ?. highlight ( context . query ) ?? context . query ; let msg = query + ( Utils . isDefined ( context . took ) ? colors . grey ( ` [took ${ context . took } ms]` ) : '' ) ; if ( this . usesReplicas && context . connection ) { msg += colors . cyan ( ` (via ${ context . connection . type } connection '${ context . connection . name }')` ) ; } return this . log ( 'query' , msg , context ) ; } }