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:
JavaScript
import { Log } from '@bytehide/logger';
Log.configure({
minimumLevel: 'info' // Only info, warn, error, and critical messages will be logged
});import { Log } from '@bytehide/logger';
Log.configure({
minimumLevel: 'info' // Only info, warn, error, and critical messages will be logged
});Environment-Based Configuration
Development Environment
JavaScript
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'] });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
JavaScript
Log.configure({
minimumLevel: 'warn',
consoleEnabled: false,
persist: true
});
Log.warn('API rate limit approaching');
Log.error('Database connection failed');
Log.critical('Application crashed');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
JavaScript
const logLevel = process.env.LOG_LEVEL || 'info';
Log.configure({
minimumLevel: logLevel,
consoleEnabled: process.env.NODE_ENV === 'development'
});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:
JavaScript
Log.trace('Function entry', {
metadata: {
function: 'processPayment',
args: { amount: 100, currency: 'USD' }
}
});Log.trace('Function entry', {
metadata: {
function: 'processPayment',
args: { amount: 100, currency: 'USD' }
}
});Debug Level
For development diagnostics:
JavaScript
Log.debug('API response received', {
metadata: {
statusCode: 200,
responseTime: 150,
endpoint: '/api/users'
}
});Log.debug('API response received', {
metadata: {
statusCode: 200,
responseTime: 150,
endpoint: '/api/users'
}
});Info Level
For general application flow:
JavaScript
Log.info('User action completed', {
metadata: {
action: 'purchase',
userId: 'user123'
}
});Log.info('User action completed', {
metadata: {
action: 'purchase',
userId: 'user123'
}
});Warning Level
For potential issues:
JavaScript
Log.warn('Deprecated API used', {
metadata: {
api: '/api/v1/users',
suggestedAlternative: '/api/v2/users'
}
});Log.warn('Deprecated API used', {
metadata: {
api: '/api/v1/users',
suggestedAlternative: '/api/v2/users'
}
});Error Level
For runtime errors:
JavaScript
try {
await processPayment(paymentData);
} catch (error) {
Log.error('Payment processing failed', {
metadata: {
paymentId: paymentData.id
}
}, error);
}try {
await processPayment(paymentData);
} catch (error) {
Log.error('Payment processing failed', {
metadata: {
paymentId: paymentData.id
}
}, error);
}Critical Level
For critical system failures:
JavaScript
process.on('uncaughtException', (error) => {
Log.critical('Uncaught exception', {}, error);
process.exit(1);
});process.on('uncaughtException', (error) => {
Log.critical('Uncaught exception', {}, error);
process.exit(1);
});Best Practices
Level Selection Guidelines
- Development: Use
debugortracefor detailed information - Staging: Use
infoto monitor application flow - Production: Use
warnorerrorto minimize noise - Critical Systems: Use
errororcriticalonly