spark_logging

A standardized logging package with configurable outputs, structured logging, and named loggers.

Features

  • Named loggers (singleton per name)
  • Six log levels: trace, debug, info, warning, error, fatal
  • Pluggable log handlers (console, JSON, custom)
  • Structured logging with context maps
  • ANSI color output for console
  • Multi-handler broadcasting
  • HTTP request logging middleware

Installation

dependencies:
  spark_logging: ^1.0.0

Usage

Basic Logging

import 'package:spark_logging/spark_logging.dart';

// Configure globally
Logger.configure(
  level: LogLevel.debug,
  handlers: [ConsoleHandler()],
);

// Get a named logger
final logger = Logger('MyService');

logger.trace('Detailed trace info');
logger.debug('Debugging details');
logger.info('Application started');
logger.warning('Disk space low');
logger.error('Failed to connect', error: exception, stackTrace: stack);
logger.fatal('Unrecoverable error');

Structured Logging with Context

logger.info('User logged in', context: {
  'userId': '123',
  'ip': '192.168.1.1',
  'method': 'oauth',
});

Handlers

// Console — colored output for terminal
Logger.configure(handlers: [ConsoleHandler()]);

// JSON — structured output for log aggregation
Logger.configure(handlers: [JsonHandler(prettyPrint: true)]);

// Multi-handler — broadcast to multiple handlers
Logger.configure(handlers: [
  MultiHandler([ConsoleHandler(), JsonHandler()]),
]);

// Filtered — minimum level filtering
Logger.configure(handlers: [
  FilteredHandler(ConsoleHandler(), minLevel: LogLevel.warning),
]);

HTTP Request Logging

final handler = Pipeline()
    .addMiddleware(logRequests())
    .addHandler(myRouter);

// Logs: method, URL, status code, duration, response size