11import type { BackupConfig , BackupResult , BackupSummary , DatabaseConfig , FileConfig } from '../types'
22import { mkdir , readdir , stat , unlink } from 'node:fs/promises'
33import { join } from 'node:path'
4+ import { Logger } from '@stacksjs/clarity'
45import { BackupType } from '../types'
56import { backupDirectory } from './directory'
67import { backupFile } from './file'
@@ -13,9 +14,10 @@ export class BackupManager {
1314
1415 async createBackup ( ) : Promise < BackupSummary > {
1516 const startTime = performance . now ( )
17+ const logger = new Logger ( 'backupx:manager' )
1618
1719 if ( this . config . verbose ) {
18- console . warn ( '🚀 Starting backup process...' )
20+ logger . warn ( '🚀 Starting backup process...' )
1921 }
2022
2123 // Ensure output directory exists
@@ -28,7 +30,7 @@ export class BackupManager {
2830 // Process database backups
2931 for ( const dbConfig of this . config . databases ) {
3032 if ( this . config . verbose ) {
31- console . warn ( `\n📋 Processing database: ${ dbConfig . name } (${ dbConfig . type } )` )
33+ logger . warn ( `\n📋 Processing database: ${ dbConfig . name } (${ dbConfig . type } )` )
3234 }
3335
3436 try {
@@ -49,7 +51,7 @@ export class BackupManager {
4951 } )
5052
5153 if ( this . config . verbose ) {
52- console . error ( `❌ Failed to backup ${ dbConfig . name } : ${ errorMessage } ` )
54+ logger . error ( `❌ Failed to backup ${ dbConfig . name } : ${ errorMessage } ` )
5355 }
5456 }
5557 }
@@ -68,7 +70,7 @@ export class BackupManager {
6870 }
6971
7072 if ( this . config . verbose ) {
71- console . warn ( `\n📁 Processing ${ fileType } : ${ fileConfig . name } ` )
73+ logger . warn ( `\n📁 Processing ${ fileType } : ${ fileConfig . name } ` )
7274 }
7375
7476 try {
@@ -89,7 +91,7 @@ export class BackupManager {
8991 } )
9092
9193 if ( this . config . verbose ) {
92- console . error ( `❌ Failed to backup ${ fileConfig . name } : ${ errorMessage } ` )
94+ logger . error ( `❌ Failed to backup ${ fileConfig . name } : ${ errorMessage } ` )
9395 }
9496 }
9597 }
@@ -179,6 +181,7 @@ export class BackupManager {
179181 if ( ! this . config . retention )
180182 return
181183
184+ const logger = new Logger ( 'backupx:retention' )
182185 try {
183186 const files = await readdir ( outputPath )
184187 const backupFiles : Array < { name : string , path : string , stats : any , age : number } > = [ ]
@@ -241,60 +244,59 @@ export class BackupManager {
241244 // Delete the files
242245 for ( const filePath of filesToDelete ) {
243246 await unlink ( filePath )
244- if ( this . config . verbose ) {
245- console . warn ( `🗑️ Removed old backup: ${ filePath } ` )
246- }
247+ logger . warn ( `🗑️ Removed old backup: ${ filePath } ` )
247248 }
248249
249- if ( filesToDelete . length > 0 && this . config . verbose ) {
250- console . warn ( `🧹 Cleaned up ${ filesToDelete . length } old backup files` )
250+ if ( filesToDelete . length > 0 ) {
251+ logger . warn ( `🧹 Cleaned up ${ filesToDelete . length } old backup files` )
251252 }
252253 }
253254 catch ( error ) {
254255 if ( this . config . verbose ) {
255- console . error ( `⚠️ Failed to cleanup old backups: ${ error } ` )
256+ logger . error ( `⚠️ Failed to cleanup old backups: ${ error } ` )
256257 }
257258 }
258259 }
259260
260261 private printSummary ( summary : BackupSummary ) : void {
261- console . warn ( '\n📊 Backup Summary:' )
262- console . warn ( `⏱️ Total duration: ${ summary . totalDuration . toFixed ( 2 ) } ms` )
263- console . warn ( `✅ Successful: ${ summary . successCount } ` )
264- console . warn ( `❌ Failed: ${ summary . failureCount } ` )
262+ const logger = new Logger ( 'backupx:summary' )
263+ logger . warn ( '\n📊 Backup Summary:' )
264+ logger . warn ( `⏱️ Total duration: ${ summary . totalDuration . toFixed ( 2 ) } ms` )
265+ logger . warn ( `✅ Successful: ${ summary . successCount } ` )
266+ logger . warn ( `❌ Failed: ${ summary . failureCount } ` )
265267
266268 if ( summary . databaseBackups . length > 0 ) {
267- console . warn ( '\n🗄️ Database Backups:' )
269+ logger . warn ( '\n🗄️ Database Backups:' )
268270 for ( const result of summary . databaseBackups ) {
269271 const status = result . success ? '✅' : '❌'
270272 const size = result . success ? `${ ( result . size / 1024 / 1024 ) . toFixed ( 2 ) } MB` : 'N/A'
271273 const duration = `${ result . duration . toFixed ( 2 ) } ms`
272274
273- console . warn ( `${ status } ${ result . name } (${ result . type } ): ${ size } in ${ duration } ` )
275+ logger . warn ( `${ status } ${ result . name } (${ result . type } ): ${ size } in ${ duration } ` )
274276
275277 if ( ! result . success && result . error ) {
276- console . warn ( ` Error: ${ result . error } ` )
278+ logger . warn ( ` Error: ${ result . error } ` )
277279 }
278280 }
279281 }
280282
281283 if ( summary . fileBackups . length > 0 ) {
282- console . warn ( '\n📁 File Backups:' )
284+ logger . warn ( '\n📁 File Backups:' )
283285 for ( const result of summary . fileBackups ) {
284286 const status = result . success ? '✅' : '❌'
285287 const size = result . success ? `${ ( result . size / 1024 / 1024 ) . toFixed ( 2 ) } MB` : 'N/A'
286288 const duration = `${ result . duration . toFixed ( 2 ) } ms`
287289 const fileCount = result . fileCount ? ` (${ result . fileCount } files)` : ''
288290
289- console . warn ( `${ status } ${ result . name } (${ result . type } ): ${ size } in ${ duration } ${ fileCount } ` )
291+ logger . warn ( `${ status } ${ result . name } (${ result . type } ): ${ size } in ${ duration } ${ fileCount } ` )
290292
291293 if ( ! result . success && result . error ) {
292- console . warn ( ` Error: ${ result . error } ` )
294+ logger . warn ( ` Error: ${ result . error } ` )
293295 }
294296 }
295297 }
296298
297- console . warn ( '' )
299+ logger . warn ( '' )
298300 }
299301}
300302
0 commit comments