Logger
Advanced logging system with colors, icons, and timestamps
Logger π
AzuraJS includes a powerful logging system with support for colors, icons, timestamps, and full customization.
Quick Start π
import { log } from 'azurajs/logger';
log.info('Server started on port 3000');
log.success('Task completed successfully');
log.warn('Memory usage is high');
log.error('Connection failed');Log Levels π
Available Levels
log.debug('Debugging information'); // π Gray
log.info('Informational message'); // βΉοΈ Cyan
log.success('Success message'); // β
Bright Green
log.warn('Warning message'); // β οΈ Yellow
log.error('Error message'); // β Red
log.fatal('Fatal error'); // π Bright Red
log.trace('Trace message'); // π Magenta
log.log('General log'); // π WhiteConfiguration βοΈ
Global Configuration
Configure logger globally for all subsequent calls:
import { configureLogger } from 'azurajs/logger';
configureLogger({
showTimestamp: true,
timestampFormat: 'datetime',
showIcon: true,
prefix: 'MyApp',
uppercase: true,
bold: true
});
// Now all logs use this configuration
log.info('Server started'); // [MyApp:INFO] βΉοΈ 1/11/2026, 2:30:45 PM Server startedPer-Call Configuration
Override global configuration for specific calls:
log.info('Custom message', {
showTimestamp: false,
bold: true,
colors: {
info: '#00ff00' // Custom green color
}
});Configuration Options π¨
Complete Configuration
interface LoggerConfig {
// Custom color mapping (hex, rgb, or predefined)
colors?: Record<string, string>;
// Custom icon mapping
icons?: Record<string, string>;
// Show timestamps
showTimestamp?: boolean;
// Show icons
showIcon?: boolean;
// Timestamp format: 'time', 'datetime', or 'iso'
timestampFormat?: 'time' | 'datetime' | 'iso';
// Custom prefix (default: 'Azura')
prefix?: string;
// Display level in uppercase
uppercase?: boolean;
// Apply bold styling
bold?: boolean;
// Apply dim/faded styling
dim?: boolean;
// Background color
backgroundColor?: string;
}Custom Colors π¨
Hex Colors
log.info('Message', {
colors: {
info: '#ff5733' // Custom orange
}
});RGB Colors
log.info('Message', {
colors: {
info: 'rgb(255, 87, 51)'
}
});Predefined Colors
log.info('Message', {
colors: {
info: 'brightBlue' // Use predefined color
}
});Available colors:
black,red,green,yellow,blue,magenta,cyan,white,graybrightRed,brightGreen,brightYellow,brightBlue,brightMagenta,brightCyan,brightWhite
Custom Icons π
log.info('Message', {
icons: {
info: 'π' // Custom rocket icon
}
});
// Disable icons
log.info('Message', {
showIcon: false
});Timestamps β°
Time Only (HH:MM:SS)
configureLogger({
showTimestamp: true,
timestampFormat: 'time'
});
log.info('Message'); // 14:23:45 βΉοΈ [Azura:INFO] MessageFull DateTime
configureLogger({
showTimestamp: true,
timestampFormat: 'datetime'
});
log.info('Message'); // 1/11/2026, 2:23:45 PM βΉοΈ [Azura:INFO] MessageISO 8601
configureLogger({
showTimestamp: true,
timestampFormat: 'iso'
});
log.info('Message'); // 2026-01-11T14:23:45.123Z βΉοΈ [Azura:INFO] MessageStyling Options π
Bold Text
log.info('Important message', { bold: true });Dim Text
log.trace('Less important', { dim: true });Background Color
log.error('Critical error', {
backgroundColor: 'bgRed'
});Combined Styling
log.success('Done!', {
bold: true,
colors: { success: '#00ff00' },
backgroundColor: 'bgBlack'
});Advanced Usage π―
Custom Prefix
configureLogger({
prefix: 'MyAPI'
});
log.info('Request received'); // [MyAPI:INFO] βΉοΈ Request receivedDifferent Prefixes Per Service
const dbLogger = (msg: string) => log.info(msg, { prefix: 'Database' });
const apiLogger = (msg: string) => log.info(msg, { prefix: 'API' });
dbLogger('Connection established'); // [Database:INFO] βΉοΈ Connection established
apiLogger('Server started'); // [API:INFO] βΉοΈ Server startedUsing Raw Logger Function
import { logger } from 'azurajs/logger';
logger('info', 'Custom log', {
showTimestamp: true,
colors: { info: '#00ff00' }
});Color Utilities ποΈ
Access color functions directly:
import { colors } from 'azurajs/logger';
// Convert hex to ANSI
const greenAnsi = colors.hex('#00ff00');
console.log(`${greenAnsi}Green text${colors.reset}`);
// Convert RGB to ANSI
const blueAnsi = colors.rgb(0, 0, 255);
console.log(`${blueAnsi}Blue text${colors.reset}`);
// Use predefined colors
console.log(`${colors.ansi.brightRed}Red text${colors.reset}`);
// Background colors
console.log(`${colors.bg.bgYellow}Yellow background${colors.reset}`);
// Styling
console.log(`${colors.bold}Bold text${colors.reset}`);
console.log(`${colors.dim}Dim text${colors.reset}`);Practical Examples π‘
HTTP Request Logging
app.use((req, res, next) => {
log.info(`${req.method} ${req.path}`, {
showTimestamp: true,
timestampFormat: 'time'
});
next();
});Error Tracking
try {
await riskyOperation();
log.success('Operation completed');
} catch (error) {
log.error(`Operation failed: ${error.message}`, {
showTimestamp: true,
bold: true
});
}Development vs Production
const isDev = process.env.NODE_ENV === 'development';
configureLogger({
showTimestamp: !isDev,
showIcon: isDev,
colors: isDev ? undefined : {
info: 'white',
error: 'red'
}
});Structured Logging
function logRequest(req: any) {
log.info('Request', {
prefix: 'API',
showTimestamp: true
});
log.debug(JSON.stringify({
method: req.method,
path: req.path,
ip: req.ip
}, null, 2));
}Best Practices π
- Configure globally once at application start
- Use appropriate log levels - don't use
errorfor warnings - Include timestamps in production for debugging
- Disable colors in CI/CD environments
- Use prefixes to identify log sources
- Keep messages concise but informative
- Disable debug logs in production
Migration Guide π
From console.log
// Before
console.log('Server started');
console.error('Error occurred');
// After
log.info('Server started');
log.error('Error occurred');From Winston/Pino
// Before (Winston)
logger.info('Message', { timestamp: true });
// After (Azura)
log.info('Message', { showTimestamp: true });API Reference π
Main Functions
log.debug(msg, options?)- Debug messagelog.info(msg, options?)- Informational messagelog.success(msg, options?)- Success messagelog.warn(msg, options?)- Warning messagelog.error(msg, options?)- Error messagelog.fatal(msg, options?)- Fatal error messagelog.trace(msg, options?)- Trace messagelog.log(msg, options?)- General log
Configuration
configureLogger(config)- Set global configurationlogger(level, msg, options?)- Raw logger functioncolors.*- Color utilities
Examples π
Check out the complete examples:
- See examples in the repository for more advanced usage patterns
