This document explains the logging system implemented in Musync to control log output in different environments.
Musync uses a centralized logging utility that:
- Controls log verbosity based on environment
- Formats log messages consistently
- Prevents excessive logging in production environments
- Provides different log levels for different types of information
The logger supports the following log levels (in order of verbosity):
debug- Detailed information for debugging purposes (never shown in production by default)info- General information about application operationwarn- Warning messages about potential issueserror- Error messages when things go wrongnone- No logging at all
The log level is configured using the LOG_LEVEL environment variable in your .env file:
# Options: debug, info, warn, error, none
LOG_LEVEL=error # Only show errors in production
If not specified, the default log level is:
debugin development environmentserrorin production environments
In your code, import the logger from @/lib/logger and use the appropriate methods:
import logger from '@/lib/logger';
// Different log levels
logger.debug('Detailed debug information', { someData: 123 }); // Only in dev
logger.info('General information'); // General info
logger.warn('Warning about potential issue'); // Warnings
logger.error('Error occurred', new Error('Something failed')); // Errors-
Use the right log level:
- Use
debugfor developer-oriented details - Use
infofor general operational information - Use
warnfor issues that don't break functionality - Use
errorfor actual errors that need attention
- Use
-
Include context:
- Add relevant data objects as the second parameter
- Include enough information to understand the context
-
Avoid logging sensitive information:
- Never log authentication tokens, passwords, or sensitive user data
- If needed, mask sensitive data (e.g., only show first/last few characters)
-
Performance considerations:
- Avoid expensive operations in log messages
- Remember that string concatenation happens even if the log level is filtered out
// Good practice
logger.info(`User ${userId.slice(0, 4)}... logged in`, {
userAgent: req.headers['user-agent'],
timestamp: new Date().toISOString()
});
// Bad practice - contains sensitive data
logger.info(`User logged in with token: ${fullAuthToken}`);A utility script is included to help convert existing console.log statements to the new logger:
node scripts/update-logger.jsThis script identifies files containing console statements and provides guidance on updating them.