Log Levels Configuration
Log levels determine which messages are recorded based on their importance. ByteHide for JavaScript supports standard logging levels with flexible configuration.
Available Log Levels
Level | Priority | Description | Use Case |
---|---|---|---|
trace | 0 | Detailed execution flow | Development debugging |
debug | 1 | Diagnostic information | Development troubleshooting |
info | 2 | General information | Application flow |
warn | 3 | Warning conditions | Potential issues |
error | 4 | Error conditions | Runtime errors |
critical | 5 | Critical failures | System crashes |
Setting Minimum Log Level
Configure the minimum log level using the minimumLevel
option:
import { Log } from '@bytehide/logger';
Log.configure({
minimumLevel: 'info' // Only info, warn, error, and critical messages will be logged
});
Environment-Based Configuration
Development Environment
Log.configure({
minimumLevel: 'debug',
consoleEnabled: true
});
Log.trace('Detailed execution step');
Log.debug('Variable value', { metadata: { userId: 123 } });
Log.info('User logged in', { tags: ['auth'] });
Production Environment
Log.configure({
minimumLevel: 'warn',
consoleEnabled: false,
persist: true
});
Log.warn('API rate limit approaching');
Log.error('Database connection failed');
Log.critical('Application crashed');
Using Environment Variables
const logLevel = process.env.LOG_LEVEL || 'info';
Log.configure({
minimumLevel: logLevel,
consoleEnabled: process.env.NODE_ENV === 'development'
});
Level-Specific Logging
Trace Level
For detailed debugging information:
Log.trace('Function entry', {
metadata: {
function: 'processPayment',
args: { amount: 100, currency: 'USD' }
}
});
Debug Level
For development diagnostics:
Log.debug('API response received', {
metadata: {
statusCode: 200,
responseTime: 150,
endpoint: '/api/users'
}
});
Info Level
For general application flow:
Log.info('User action completed', {
metadata: {
action: 'purchase',
userId: 'user123'
}
});
Warning Level
For potential issues:
Log.warn('Deprecated API used', {
metadata: {
api: '/api/v1/users',
suggestedAlternative: '/api/v2/users'
}
});
Error Level
For runtime errors:
try {
await processPayment(paymentData);
} catch (error) {
Log.error('Payment processing failed', {
metadata: {
paymentId: paymentData.id
}
}, error);
}
Critical Level
For critical system failures:
process.on('uncaughtException', (error) => {
Log.critical('Uncaught exception', {}, error);
process.exit(1);
});
Best Practices
Level Selection Guidelines
- Development: Use
debug
ortrace
for detailed information - Staging: Use
info
to monitor application flow - Production: Use
warn
orerror
to minimize noise - Critical Systems: Use
error
orcritical
only