/

File Logging Configuration (Node.js)

File logging allows you to persist log messages to disk files for later analysis and debugging. This feature is only available in Node.js environments.

Node.js Only

File logging is only available in Node.js environments. Browser applications cannot write to the file system directly.

Basic File Logging

Enable file logging using the persist option:

import { Log } from '@bytehide/logger';

Log.configure({
    persist: true,
    filePath: 'logs/app.log',
    minimumLevel: 'info'
});

Log.info('This message will be saved to file');

File Configuration Options

Log.configure({
    persist: true,
    filePath: 'logs/application.log',
    fileMaxSize: 10 * 1024 * 1024,      // 10MB max file size
    fileMaxFiles: 5,                     // Keep 5 log files
    fileCompress: true,                  // Compress rotated files
    minimumLevel: 'info'
});

Available Options

OptionTypeDefaultDescription
persistbooleanfalseEnable file logging
filePathstring'logs/app.log'Path to log file
fileMaxSizenumber10MBMaximum file size before rotation
fileMaxFilesnumber5Number of rotated files to keep
fileCompressbooleanfalseCompress rotated log files

Log Rotation

Automatic rotation prevents log files from growing too large:

Log.configure({
    persist: true,
    filePath: 'logs/app.log',
    fileMaxSize: 5 * 1024 * 1024,  // 5MB
    fileMaxFiles: 10,              // Keep 10 files
    fileCompress: true             // Compress old files
});

// Files will be rotated as:
// app.log (current)
// app.log.1 (previous)
// app.log.2.gz (compressed)
// ...

Environment-Specific Configuration

const environment = process.env.NODE_ENV || 'development';

Log.configure({
    persist: true,
    filePath: `logs/${environment}.log`,
    fileMaxSize: environment === 'production' ? 50 * 1024 * 1024 : 10 * 1024 * 1024,
    fileMaxFiles: environment === 'production' ? 20 : 5
});

Best Practices

File Logging Best Practices

  • Implement log rotation to prevent disk space issues
  • Set appropriate file permissions for security
  • Monitor disk space usage regularly
  • Use different configurations for different environments

Production Configuration

if (process.env.NODE_ENV === 'production') {
    Log.configure({
        persist: true,
        filePath: '/var/log/myapp/application.log',
        fileMaxSize: 100 * 1024 * 1024,    // 100MB
        fileMaxFiles: 50,                   // Keep 50 files
        fileCompress: true,                 // Compress old files
        minimumLevel: 'warn',               // Production level
        consoleEnabled: false               // No console in production
    });
}
Previous
Console Output