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
Option | Type | Default | Description |
---|---|---|---|
persist | boolean | false | Enable file logging |
filePath | string | 'logs/app.log' | Path to log file |
fileMaxSize | number | 10MB | Maximum file size before rotation |
fileMaxFiles | number | 5 | Number of rotated files to keep |
fileCompress | boolean | false | Compress 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
});
}