212 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
		
		
			
		
	
	
			212 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
|  | /** | ||
|  |  * SMTP Client Logging Utilities | ||
|  |  * Client-side logging utilities for SMTP operations | ||
|  |  */ | ||
|  | 
 | ||
|  | import { logger } from '../../../../logger.ts'; | ||
|  | import type { ISmtpResponse, ISmtpClientOptions } from '../interfaces.ts'; | ||
|  | 
 | ||
|  | export interface ISmtpClientLogData { | ||
|  |   component: string; | ||
|  |   host?: string; | ||
|  |   port?: number; | ||
|  |   secure?: boolean; | ||
|  |   command?: string; | ||
|  |   response?: ISmtpResponse; | ||
|  |   error?: Error; | ||
|  |   connectionId?: string; | ||
|  |   messageId?: string; | ||
|  |   duration?: number; | ||
|  |   [key: string]: any; | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Log SMTP client connection events | ||
|  |  */ | ||
|  | export function logConnection( | ||
|  |   event: 'connecting' | 'connected' | 'disconnected' | 'error', | ||
|  |   options: ISmtpClientOptions, | ||
|  |   data?: Partial<ISmtpClientLogData> | ||
|  | ): void { | ||
|  |   const logData: ISmtpClientLogData = { | ||
|  |     component: 'smtp-client', | ||
|  |     event, | ||
|  |     host: options.host, | ||
|  |     port: options.port, | ||
|  |     secure: options.secure, | ||
|  |     ...data | ||
|  |   }; | ||
|  |    | ||
|  |   switch (event) { | ||
|  |     case 'connecting': | ||
|  |       logger.info('SMTP client connecting', logData); | ||
|  |       break; | ||
|  |     case 'connected': | ||
|  |       logger.info('SMTP client connected', logData); | ||
|  |       break; | ||
|  |     case 'disconnected': | ||
|  |       logger.info('SMTP client disconnected', logData); | ||
|  |       break; | ||
|  |     case 'error': | ||
|  |       logger.error('SMTP client connection error', logData); | ||
|  |       break; | ||
|  |   } | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Log SMTP command execution | ||
|  |  */ | ||
|  | export function logCommand( | ||
|  |   command: string, | ||
|  |   response?: ISmtpResponse, | ||
|  |   options?: ISmtpClientOptions, | ||
|  |   data?: Partial<ISmtpClientLogData> | ||
|  | ): void { | ||
|  |   const logData: ISmtpClientLogData = { | ||
|  |     component: 'smtp-client', | ||
|  |     command, | ||
|  |     response, | ||
|  |     host: options?.host, | ||
|  |     port: options?.port, | ||
|  |     ...data | ||
|  |   }; | ||
|  |    | ||
|  |   if (response && response.code >= 400) { | ||
|  |     logger.warn('SMTP command failed', logData); | ||
|  |   } else { | ||
|  |     logger.debug('SMTP command executed', logData); | ||
|  |   } | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Log authentication events | ||
|  |  */ | ||
|  | export function logAuthentication( | ||
|  |   event: 'start' | 'success' | 'failure', | ||
|  |   method: string, | ||
|  |   options: ISmtpClientOptions, | ||
|  |   data?: Partial<ISmtpClientLogData> | ||
|  | ): void { | ||
|  |   const logData: ISmtpClientLogData = { | ||
|  |     component: 'smtp-client', | ||
|  |     event: `auth_${event}`, | ||
|  |     authMethod: method, | ||
|  |     host: options.host, | ||
|  |     port: options.port, | ||
|  |     ...data | ||
|  |   }; | ||
|  |    | ||
|  |   switch (event) { | ||
|  |     case 'start': | ||
|  |       logger.debug('SMTP authentication started', logData); | ||
|  |       break; | ||
|  |     case 'success': | ||
|  |       logger.info('SMTP authentication successful', logData); | ||
|  |       break; | ||
|  |     case 'failure': | ||
|  |       logger.error('SMTP authentication failed', logData); | ||
|  |       break; | ||
|  |   } | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Log TLS/STARTTLS events | ||
|  |  */ | ||
|  | export function logTLS( | ||
|  |   event: 'starttls_start' | 'starttls_success' | 'starttls_failure' | 'tls_connected', | ||
|  |   options: ISmtpClientOptions, | ||
|  |   data?: Partial<ISmtpClientLogData> | ||
|  | ): void { | ||
|  |   const logData: ISmtpClientLogData = { | ||
|  |     component: 'smtp-client', | ||
|  |     event, | ||
|  |     host: options.host, | ||
|  |     port: options.port, | ||
|  |     ...data | ||
|  |   }; | ||
|  |    | ||
|  |   if (event.includes('failure')) { | ||
|  |     logger.error('SMTP TLS operation failed', logData); | ||
|  |   } else { | ||
|  |     logger.info('SMTP TLS operation', logData); | ||
|  |   } | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Log email sending events | ||
|  |  */ | ||
|  | export function logEmailSend( | ||
|  |   event: 'start' | 'success' | 'failure', | ||
|  |   recipients: string[], | ||
|  |   options: ISmtpClientOptions, | ||
|  |   data?: Partial<ISmtpClientLogData> | ||
|  | ): void { | ||
|  |   const logData: ISmtpClientLogData = { | ||
|  |     component: 'smtp-client', | ||
|  |     event: `send_${event}`, | ||
|  |     recipientCount: recipients.length, | ||
|  |     recipients: recipients.slice(0, 5), // Only log first 5 recipients for privacy
 | ||
|  |     host: options.host, | ||
|  |     port: options.port, | ||
|  |     ...data | ||
|  |   }; | ||
|  |    | ||
|  |   switch (event) { | ||
|  |     case 'start': | ||
|  |       logger.info('SMTP email send started', logData); | ||
|  |       break; | ||
|  |     case 'success': | ||
|  |       logger.info('SMTP email send successful', logData); | ||
|  |       break; | ||
|  |     case 'failure': | ||
|  |       logger.error('SMTP email send failed', logData); | ||
|  |       break; | ||
|  |   } | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Log performance metrics | ||
|  |  */ | ||
|  | export function logPerformance( | ||
|  |   operation: string, | ||
|  |   duration: number, | ||
|  |   options: ISmtpClientOptions, | ||
|  |   data?: Partial<ISmtpClientLogData> | ||
|  | ): void { | ||
|  |   const logData: ISmtpClientLogData = { | ||
|  |     component: 'smtp-client', | ||
|  |     operation, | ||
|  |     duration, | ||
|  |     host: options.host, | ||
|  |     port: options.port, | ||
|  |     ...data | ||
|  |   }; | ||
|  |    | ||
|  |   if (duration > 10000) { // Log slow operations (>10s)
 | ||
|  |     logger.warn('SMTP slow operation detected', logData); | ||
|  |   } else { | ||
|  |     logger.debug('SMTP operation performance', logData); | ||
|  |   } | ||
|  | } | ||
|  | 
 | ||
|  | /** | ||
|  |  * Log debug information (only when debug is enabled) | ||
|  |  */ | ||
|  | export function logDebug( | ||
|  |   message: string, | ||
|  |   options: ISmtpClientOptions, | ||
|  |   data?: Partial<ISmtpClientLogData> | ||
|  | ): void { | ||
|  |   if (!options.debug) { | ||
|  |     return; | ||
|  |   } | ||
|  |    | ||
|  |   const logData: ISmtpClientLogData = { | ||
|  |     component: 'smtp-client-debug', | ||
|  |     host: options.host, | ||
|  |     port: options.port, | ||
|  |     ...data | ||
|  |   }; | ||
|  |    | ||
|  |   logger.debug(`[SMTP Client Debug] ${message}`, logData); | ||
|  | } |