Logger SDK Configuration
Configuration overview
ByteHide Logger is configured through the Log.configure()
method, which provides comprehensive options for logging behavior, file management, security, and performance optimization.
Configuration Overview
The Log.configure()
method is the main configuration function for ByteHide Logger. Here's how to use it:
import { Log } from '@bytehide/logger';
Log.configure({
// Configure your logging settings here
});
// Set your project token
Log.setProjectToken('your-project-token');
Project Token Configuration
By simply adding your project token, all logs will be automatically sent to the ByteHide cloud platform without any additional configuration. If you don't set a project token, logs will only be stored locally (console/files). For more information about obtaining and configuring your project token, see Project Token setup.
Configuration Options
Basic Configuration
Log.configure({
minimumLevel: 'info', // Minimum log level to process
consoleEnabled: true, // Enable/disable console output
maskSensitiveData: ['password', 'token'], // Property names to mask
duplicateSuppressionWindowMs: 1000 // Suppress duplicates within timeframe
});
Complete Configuration Reference
Property | Type | Default | Description |
---|---|---|---|
minimumLevel | string | 'warn' | Minimum log level ('trace' , 'debug' , 'info' , 'warn' , 'error' , 'critical' ) |
consoleEnabled | boolean | false | Enable/disable console output |
maskSensitiveData | string[] | ['password', 'token'] | Property names to mask in logs |
duplicateSuppressionWindowMs | number | null | 1000 | Time window for suppressing duplicate messages (ms) |
persist | boolean | false | Enable/disable file logging (Node.js only) |
filePath | string | 'logs.txt' | File path where logs are written (Node.js only) |
rollingInterval | string | 'day' | Interval for log file rotation ('none' , 'hour' , 'day' ) |
rollOnFileSizeLimit | boolean | true | Enable rotation when file reaches size limit |
fileSizeLimitBytes | number | 10485760 | Maximum file size before rotation (10MB) |
includeCallerInfo | boolean | true | Include function name and line number in logs |
Console Output Settings
Log.configure({
consoleEnabled: true, // Enable console output
includeCallerInfo: true, // Include caller information
minimumLevel: 'debug' // Show debug messages in console
});
Log Level Configuration
Set the minimum log level to process:
Log.configure({
minimumLevel: 'info' // Only process 'info' level and above
});
Available Log Levels (in order of severity):
'trace'
: Most detailed diagnostic information'debug'
: Detailed diagnostic information'info'
: General information messages'warn'
: Warning messages for potential issues'error'
: Error messages for failures'critical'
: Critical errors that may cause application termination
Security Settings
Configure data masking for sensitive information:
Log.configure({
// Mask sensitive data in logs
maskSensitiveData: ['password', 'token', 'secret', 'key', 'apiKey']
});
Performance Settings
Configure duplicate suppression:
Log.configure({
// Suppress duplicate messages within 5 second window
duplicateSuppressionWindowMs: 5000
});
// Disable duplicate suppression
Log.configure({
duplicateSuppressionWindowMs: null
});
File Logging Settings (Node.js Only)
Control how logs are saved to files:
Log.configure({
// Enable file logging
persist: true,
filePath: 'logs/application.log',
// File rotation settings
rollingInterval: 'day', // 'none', 'hour', 'day'
rollOnFileSizeLimit: true,
fileSizeLimitBytes: 10 * 1024 * 1024 // 10MB
});
Configuration Examples
Development Environment
Log.configure({
// Verbose logging for development
minimumLevel: 'debug',
consoleEnabled: true,
includeCallerInfo: true,
// Basic security
maskSensitiveData: ['password', 'token'],
// No duplicate suppression for debugging
duplicateSuppressionWindowMs: null
});
Production Environment
Log.configure({
// Production-level logging
minimumLevel: 'warn',
consoleEnabled: false,
includeCallerInfo: false,
// Enhanced security
maskSensitiveData: [
'password', 'token', 'secret', 'key',
'credential', 'apiKey', 'authToken'
],
// Performance optimization
duplicateSuppressionWindowMs: 10000 // 10 seconds
});
Node.js Server Configuration
Log.configure({
// Server logging settings
minimumLevel: 'info',
consoleEnabled: false,
// File logging
persist: true,
filePath: 'logs/server.log',
rollingInterval: 'day',
fileSizeLimitBytes: 50 * 1024 * 1024, // 50MB
// Security and performance
maskSensitiveData: ['password', 'token', 'apiKey'],
duplicateSuppressionWindowMs: 5000
});
Browser Application Configuration
Log.configure({
// Browser-specific settings
minimumLevel: 'warn',
consoleEnabled: true,
includeCallerInfo: false,
// Security (no file logging in browsers)
maskSensitiveData: ['password', 'token', 'sessionId'],
duplicateSuppressionWindowMs: 3000
});
Environment-Based Configuration
Automatic Environment Detection
const isDevelopment = process.env.NODE_ENV === 'development';
Log.configure({
minimumLevel: isDevelopment ? 'debug' : 'warn',
consoleEnabled: isDevelopment,
includeCallerInfo: isDevelopment,
duplicateSuppressionWindowMs: isDevelopment ? null : 5000
});
Configuration from Environment Variables
Log.configure({
minimumLevel: process.env.LOG_LEVEL || 'warn',
consoleEnabled: process.env.LOG_CONSOLE === 'true',
persist: process.env.LOG_FILE === 'true',
filePath: process.env.LOG_FILE_PATH || 'logs/app.log'
});
Global Metadata
Add metadata that appears in all log entries:
// Add global metadata
Log.addMetaContext('AppVersion', '1.2.3');
Log.addMetaContext('Environment', process.env.NODE_ENV);
Log.addMetaContext('ServerName', require('os').hostname());
// All subsequent logs will include this metadata
Log.info('Application started'); // Includes AppVersion, Environment, ServerName
Reset Configuration
Reset all configuration to default values:
// Reset configuration to defaults
Log.reset();
// Reconfigure after reset
Log.configure({
minimumLevel: 'info',
consoleEnabled: true
});
Runtime Configuration Changes
You can modify configuration at runtime:
// Initial configuration
Log.configure({
minimumLevel: 'warn',
consoleEnabled: false
});
// Later in your application
if (debugMode) {
Log.configure({
minimumLevel: 'debug',
consoleEnabled: true
});
}
TypeScript Configuration
For TypeScript projects, you can use type-safe configuration:
import { Log, LogSettings } from '@bytehide/logger';
const config: Partial<LogSettings> = {
minimumLevel: 'info',
consoleEnabled: true,
maskSensitiveData: ['password', 'token']
};
Log.configure(config);
Best Practices
Configuration Best Practices
- Configure early: Set up logging configuration at application startup
- Use environment variables: Make configuration flexible across environments
- Secure sensitive data: Always include common sensitive property names in masking
- Optimize for performance: Use appropriate log levels and duplicate suppression in production
- File logging: Only enable file persistence in Node.js environments
Next Steps
After configuring the basic settings, explore these topics:
Configuration:
Features: