Skip to main content

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' });
Output
Build complete
User created { "userId": 123 }
All tests passed { "total": 42, "duration": "1.5s" }
🎮 Try it yourself

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'));
Output
Database connection failed
Invalid input { "field": "email", "value": "invalid" }
Unhandled exception Error: Connection timeout
🎮 Try it yourself

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 });
Output
Memory usage high { "used": "85%" }
Deprecated: Use newMethod() instead
Rate limit approaching { "remaining": 10 }
🎮 Try it yourself

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' });
Output
Server started { "port": 3000 }
Processing batch { "size": 100 }
Cache hit { "key": "user:123" }
🎮 Try it yourself

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: [] });
Output
Request received { "method": "POST", "url": "/api/users" }
Cache miss { "key": "user:123" }
SQL query { "sql": "SELECT...", "params": [] }
🎮 Try it yourself

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);
Output
User authenticated
{ "id": 123, "name": "John Doe", "email": "john@example.com", "roles": ["admin", "user"] }
🎮 Try it yourself

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);
}
Output
Operation failed
Error: Connection timeout at riskyOperation (operations.ts:42:11) at main (index.ts:15:5)
🎮 Try it yourself

Error with Additional Context

log.error('Database query failed', {
query: 'SELECT * FROM users',
error: 'Connection refused',
retries: 3
});
Output
Database query failed
{ "query": "SELECT * FROM users", "error": "Connection refused", "retries": 3 }
🎮 Try it yourself

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());
Output
Variables: a b c
Request: GET /api/users 200
Created: User 123 2024-01-15T10:30:00Z
🎮 Try it yourself

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);
Output
Debug info { "env": "development" }
Error occurred Error: Something went wrong
🎮 Try it yourself

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;
}
}
API Request Example
Fetching user { "id": 123 }
User fetched { "id": 123, "name": "John Doe" }
🎮 Try it yourself

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' });
Build Process
Starting build...
Compiling TypeScript { "files": 42 }
Bundling modules { "chunks": 5 }
Build completed with warnings { "warnings": 2 }
Build complete { "files": 15, "size": "1.2MB", "duration": "3.5s" }
🎮 Try it yourself

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
Filtered Output (level: warn)
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