AzuraJS Logo
AzuraJSFramework
v2.2 Beta

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');                // πŸ“ White

Configuration βš™οΈ

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 started

Per-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, gray
  • brightRed, 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] Message

Full DateTime

configureLogger({
  showTimestamp: true,
  timestampFormat: 'datetime'
});

log.info('Message');  // 1/11/2026, 2:23:45 PM ℹ️ [Azura:INFO] Message

ISO 8601

configureLogger({
  showTimestamp: true,
  timestampFormat: 'iso'
});

log.info('Message');  // 2026-01-11T14:23:45.123Z ℹ️ [Azura:INFO] Message

Styling 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 received

Different 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 started

Using 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 πŸ’Ž

  1. Configure globally once at application start
  2. Use appropriate log levels - don't use error for warnings
  3. Include timestamps in production for debugging
  4. Disable colors in CI/CD environments
  5. Use prefixes to identify log sources
  6. Keep messages concise but informative
  7. 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 message
  • log.info(msg, options?) - Informational message
  • log.success(msg, options?) - Success message
  • log.warn(msg, options?) - Warning message
  • log.error(msg, options?) - Error message
  • log.fatal(msg, options?) - Fatal error message
  • log.trace(msg, options?) - Trace message
  • log.log(msg, options?) - General log

Configuration

  • configureLogger(config) - Set global configuration
  • logger(level, msg, options?) - Raw logger function
  • colors.* - Color utilities

Examples πŸ“š

Check out the complete examples:

  • See examples in the repository for more advanced usage patterns

On this page