Semantic Logging
Semantic logging methods give your logs meaning and context through consistent colors, icons, and formatting. Instead of generic console.log(), use methods that describe what's happening.
Log Levels
log.success(message, ...args)
Use for successful operations, completions, and positive outcomes.
log.success('Build complete');
log.success('User created', { userId: 123 });
log.success('All tests passed', { total: 42, duration: '1.5s' });
Appearance: ✔ Green text
log.error(message, ...args)
Use for errors, failures, and critical issues.
log.error('Database connection failed');
log.error('Invalid input', { field: 'email', value: 'invalid' });
log.error('Unhandled exception', new Error('Connection timeout'));
Appearance: ✖ Red text, bold
log.warn(message, ...args)
Use for warnings, deprecations, and potential issues.
log.warn('Memory usage high', { used: '85%' });
log.warn('Deprecated: Use newMethod() instead');
log.warn('Rate limit approaching', { remaining: 10 });
Appearance: ⚠ Yellow text
log.info(message, ...args)
Use for informational messages, status updates, and general information.
log.info('Server started', { port: 3000 });
log.info('Processing batch', { size: 100 });
log.info('Cache hit', { key: 'user:123' });
Appearance: ℹ Cyan text
log.debug(message, ...args)
Use for debugging information, verbose output, and development-only logs.
log.debug('Request received', { method: 'POST', url: '/api/users' });
log.debug('Cache miss', { key: 'user:123' });
log.debug('SQL query', { sql: 'SELECT * FROM users', params: [] });
Appearance: ● Gray text
Logging Objects
All semantic methods support additional arguments. Objects are automatically formatted:
const user = {
id: 123,
name: 'John Doe',
email: 'john@example.com',
roles: ['admin', 'user']
};
log.info('User authenticated', user);
Error Handling
When you pass an Error object, Logluxe formats it specially:
try {
throw new Error('Connection timeout');
} catch (error) {
log.error('Operation failed', error);
}
Error with Additional Context
log.error('Database query failed', {
query: 'SELECT * FROM users',
error: 'Connection refused',
retries: 3
});
Multiple Arguments
Log multiple values at once:
log.debug('Variables:', 'a', 'b', 'c');
log.info('Request:', 'GET', '/api/users', 200);
log.success('Created:', 'User', 123, new Date().toISOString());
Conditional Logging
Combine with short-circuit evaluation:
const isDev = true;
const error = new Error('Something went wrong');
// Only log in development
isDev && log.debug('Debug info', { env: 'development' });
// Log only if condition is met
error && log.error('Error occurred', error);
Real-World Examples
API Request Logging
async function fetchUser(id) {
log.info('Fetching user', { id });
try {
const user = await api.get(`/users/${id}`);
log.success('User fetched', { id, name: user.name });
return user;
} catch (error) {
log.error('Failed to fetch user', { id, error: error.message });
throw error;
}
}
Build Process
log.info('Starting build...');
log.debug('Compiling TypeScript', { files: 42 });
log.debug('Bundling modules', { chunks: 5 });
log.warn('Build completed with warnings', { warnings: 2 });
log.success('Build complete', { files: 15, size: '1.2MB', duration: '3.5s' });
Log Levels Filtering
Control which log levels are shown:
import { createLogger } from 'logluxe';
// Only show warnings and errors
const log = createLogger({ level: 'warn' });
log.debug('Hidden'); // Not shown
log.info('Hidden'); // Not shown
log.warn('Shown'); // Shown
log.error('Shown'); // Shown
Level Hierarchy
debug < info < warn < error
│ │ │ │
│ │ │ └── Always shown (unless muted)
│ │ └── Shown when level >= 'warn'
│ └── Shown when level >= 'info'
└── Shown when level >= 'debug' (default)
Next: Manual Colors →