The template includes a sophisticated centralized logging system with component-based filtering and automatic debug elimination.
Quick Start
Basic Usage
import { createLogger } from './utils/Logger';
class MyComponent {
private logger = createLogger('myComponent');
someMethod() {
this.logger.debug('Method called', { data });
this.logger.info('Operation completed');
this.logger.warn('Warning message', error);
this.logger.error('Error occurred', error);
}
}
Features
Component-Based Logging
Each component gets its own logger with independent log level:
const logger = createLogger('modal'); // Component name
Available components:
main- Plugin entry pointmodal- Modal dialogsui- UI componentssettings- Settings managementapi- API callsevents- Event handlinggeneral- General/uncategorized
Log Levels
Four log levels in order of severity:
- debug - Detailed diagnostic information
- info - General informational messages
- warn - Warning messages for potential issues
- error - Error messages for failures
Filtering:
- Setting level to
warnshows warn + error (filters debug + info) - Setting level to
debugshows everything
Default Configuration
All components default to warn level:
const DEFAULT_CONFIG: LoggerConfig = {
main: 'warn',
modal: 'warn',
api: 'warn',
// ... etc
};
In development: Explicitly enable debug in main.ts:
if (typeof BUILD_ENV !== 'undefined' && BUILD_ENV === 'development') {
logger.setComponentLevel('main', 'debug');
logger.setComponentLevel('modal', 'debug');
// ... enable for all components
}
Tag-Based Filtering
For complex components with lots of debug output, use tags:
// Enable only specific debug categories
logger.setComponentLevel('npe', 'debug', ['validation', 'state']);
// Use tags in debug calls
logger.debug('Field valid', { field }, ['validation']); // ✅ Shows
logger.debug('Rendering', { time }, ['rendering']); // ❌ Hidden
See: Tag-Based Filtering Guide
File Logging
Optional logging to file (debug-log.txt in vault root):
// Enable file logging (in main.ts)
logger.initFileLogging(this.app);
// Disable on plugin unload
logger.disableFileLogging();
Features:
- Buffered writes (efficient)
- Automatic flush on plugin unload
- Timestamped entries
- Only active when enabled
Production Behavior
Automatic Debug Elimination
In production builds, all debug code is eliminated:
// Development: Full output
this.logger.debug('Processing', { data });
// Production: This line doesn't execute!
// Zero runtime overhead
How it works:
BUILD_ENVconstant replaced at build timeisDevelopment()returnsfalsein production- Debug method returns early
- Tree-shaking removes unreachable code
Result:
- Debug calls become no-ops
- No performance impact
- Smaller bundle size
Preserved Logging
info, warn, and error remain in production:
this.logger.info('Plugin loaded'); // ✅ Shows in production
this.logger.warn('Deprecated API'); // ✅ Shows in production
this.logger.error('Failed to load'); // ✅ Shows in production
API Reference
createLogger(component)
Creates a component-specific logger:
const logger = createLogger('componentName');
Returns: ComponentLogger with methods:
debug(message, data?, tags?)info(message, data?)warn(message, data?)error(message, data?)
logger.debug(message, data?, tags?)
Log debug message (eliminated in production):
this.logger.debug('User clicked button', {
buttonId: 'submit',
timestamp: Date.now()
}, ['user-interaction']);
Parameters:
message- String messagedata- Optional object with contexttags- Optional array of tags for filtering
logger.info(message, data?)
Log informational message:
this.logger.info('Settings saved', {
settingCount: 5
});
logger.warn(message, data?)
Log warning message:
this.logger.warn('Deprecated API used', {
api: 'oldMethod',
replacement: 'newMethod'
});
logger.error(message, data?)
Log error message:
this.logger.error('Failed to load data', error);
Configuration Methods
setComponentLevel(component, level, tags?)
Set log level for a component:
logger.setComponentLevel('modal', 'debug');
logger.setComponentLevel('npe', 'debug', ['validation']);
setConfig(config)
Set configuration for multiple components:
logger.setConfig({
main: 'debug',
modal: 'debug',
ui: 'info'
});
setEnabled(enabled)
Enable/disable all logging:
logger.setEnabled(false); // Silence all logs
Best Practices
Use Appropriate Levels
// ✅ Debug: Detailed diagnostic info
this.logger.debug('Validating field', { field, value });
// ✅ Info: General events
this.logger.info('Modal opened');
// ✅ Warn: Potential issues
this.logger.warn('Slow operation detected', { duration });
// ✅ Error: Failures
this.logger.error('API request failed', error);
Include Context
// ❌ Not helpful
this.logger.debug('Processing');
// ✅ Helpful
this.logger.debug('Processing user input', {
fieldName: 'email',
valueLength: value.length,
isValid: true
});
Use Tags for Complex Components
// In a large component with multiple concerns
this.logger.debug('Validation passed', { field }, ['validation']);
this.logger.debug('Rendering complete', { time }, ['rendering']);
this.logger.debug('State updated', { state }, ['state']);
// Enable only what you need
logger.setComponentLevel('component', 'debug', ['validation']);
Don’t Elevate Debug to Warn
// ❌ Wrong: This will show in production!
this.logger.warn('Debug info:', data);
// ✅ Right: Use debug level
this.logger.debug('Debug info:', data);
// ✅ Right: Use tags to filter noise
this.logger.debug('Debug info:', data, ['specific-tag']);
Troubleshooting
Debug Messages Not Showing
Check log level:
// In main.ts, ensure debug is enabled
if (BUILD_ENV === 'development') {
logger.setComponentLevel('yourComponent', 'debug');
}
Check component name matches:
// Must match exactly
createLogger('modal') // Component name
logger.setComponentLevel('modal', 'debug') // Same name
Too Much Debug Output
Use tag filtering:
logger.setComponentLevel('npe', 'debug', ['validation']);
Or raise level temporarily:
logger.setComponentLevel('noisy-component', 'info');
File Logging Not Working
Enable in main.ts:
logger.initFileLogging(this.app);
Check vault root:
- Look for
debug-log.txt - File created on first log write
- Buffered, may need flush
Related Documentation
- Tag-Based Filtering - Advanced filtering guide
- Debug System - How debug elimination works
- Code Examples - Practical examples