Logging Utility
The EDURange Cloud platform includes a logging utility that provides environment-aware logging capabilities. This utility helps prevent sensitive information from being exposed in production environments while still providing detailed logs during development.
Overview
The logging utility is implemented in dashboard/lib/logger.ts
and provides three main functions:
devLog
: For development-only loggingerrorLog
: For error logging with environment-aware sanitizationwarnLog
: For warning logging with environment-aware sanitization
Usage
Development-Only Logging
The devLog
function is designed to only output logs in development environments. In production, these logs are completely suppressed.
import { devLog } from '@/lib/logger';
// This will only appear in development, not in production
devLog('User session data:', session);
devLog('API response:', response);
Error Logging
The errorLog
function logs errors in all environments but sanitizes sensitive data in production.
import { errorLog } from '@/lib/logger';
try {
// Some operation that might fail
} catch (error) {
// In development: logs both the message and full error details
// In production: logs only the message without sensitive details
errorLog('Failed to fetch user data', error);
}
Warning Logging
The warnLog
function logs warnings in all environments but sanitizes sensitive data in production.
import { warnLog } from '@/lib/logger';
// In development: logs both the message and full data
// In production: logs only the warning message without sensitive details
warnLog('User session about to expire', userData);
Implementation Details
The logging utility uses the NODE_ENV
environment variable to determine the current environment:
export const devLog = (...args: any[]): void => {
if (process.env.NODE_ENV !== 'production') {
console.log(...args);
}
};
export const errorLog = (message: string, error?: any): void => {
if (process.env.NODE_ENV === 'production') {
// In production, log only the error message without sensitive details
console.error(message);
} else {
// In development, log full error details
console.error(message, error);
}
};
export const warnLog = (message: string, data?: any): void => {
if (process.env.NODE_ENV === 'production') {
// In production, log only the warning message without sensitive details
console.warn(message);
} else {
// In development, log full warning details
console.warn(message, data);
}
};
Best Practices
-
Use
devLog
for debugging information- Any information that is only useful during development should use
devLog
- This includes session data, API responses, and other potentially sensitive information
- Any information that is only useful during development should use
-
Use
errorLog
for error handling- Always include a descriptive message as the first parameter
- Pass the actual error object as the second parameter
-
Use
warnLog
for non-critical issues- Use for situations that aren’t errors but might need attention
- Useful for deprecation notices or potential issues
-
Never log sensitive information directly
- Always use the logging utility instead of direct
console.log
calls - Be mindful of what information might be sensitive (user data, tokens, etc.)
- Always use the logging utility instead of direct
Components Using the Logging Utility
The following components have been updated to use the logging utility:
MainNavigation
: UsesdevLog
to prevent logging sensitive session information in productiondashboard-nav
: UsesdevLog
for navigation state loggingNewChallengeForm
: UsesdevLog
anderrorLog
for challenge data loggingcreate-profile
: UsesdevLog
for form data loggingChallengeItem
: UsesdevLog
for URL availability checksDancingFrog
: UsesdevLog
anderrorLog
for animation and audio logging
Security Benefits
By implementing this logging utility, EDURange Cloud gains several security benefits:
- Reduced exposure of sensitive data in production environments
- Consistent logging behavior across the application
- Simplified debugging in development without compromising production security
- Compliance with security best practices by not exposing internal details in production logs
This approach aligns with the platform’s goal of providing a secure, educational environment for cybersecurity training.