reset static method

void reset()

Reset Logar to initial state.

This method resets the Logar logging system to its initial state, clearing all configuration, history, and collectors. This is useful for testing, debugging, or when you want to start fresh with the logging system.

Reset Behavior

The reset operation performs the following actions:

  • Configuration Reset: Restores default configuration values
  • History Clear: Removes all stored log entries
  • Collector Cleanup: Disposes of all external collectors
  • State Reset: Returns to initial enabled state

Default Configuration

After reset, the system uses these default values:

  • minimumLevel: LogLevel.info
  • format: LogFormat.pretty
  • globalPrefix: null (no prefix)
  • showTimestamp: true
  • enabled: true
  • collectors: [] (empty list)

Usage Examples

// Reset for testing
Logar.reset();
print('Logar reset to initial state');

// Reset before reconfiguration
Logar.reset();
Logar.configure(LogarConfig(
  minimumLevel: LogLevel.debug,
  format: LogFormat.json,
));

// Reset for debugging
Logar.reset();
await runDebugScenario();

Impact on Logging

After reset:

  • New Logs: Continue to work normally
  • Configuration: Uses default settings
  • History: Starts fresh with empty history
  • Collectors: No external collectors active
  • Performance: Returns to default performance characteristics

Thread Safety

Reset operations are thread-safe:

  • Safe to call from multiple threads
  • Atomic reset operation
  • No race conditions during reset
  • Immediate effect across all threads

Example: Testing Workflow

class TestWorkflow {
  static Future<void> runTest(String testName) async {
    // Reset logging system for clean test
    Logar.reset();
    print('Starting test: $testName');

    try {
      // Run test operations
      await _performTestOperations();

      // Verify test logs
      final logs = Logar.history;
      print('Test generated ${logs.length} logs');

      // Verify specific log entries
      _verifyTestLogs(logs);

      print('Test completed successfully: $testName');

    } catch (error) {
      print('Test failed: $testName - $error');
      rethrow;
    } finally {
      // Reset for next test
      Logar.reset();
    }
  }

  static void _verifyTestLogs(List<LogEntry> logs) {
    // Test-specific log verification logic
    final infoLogs = logs.where((log) => log.level == LogLevel.info).length;
    final errorLogs = logs.where((log) => log.level == LogLevel.error).length;

    print('Info logs: $infoLogs, Error logs: $errorLogs');
  }
}

Example: Debug Session Management

class DebugSessionManager {
  static void startNewSession() {
    // Reset to clean state
    Logar.reset();
    print('New debug session started');
  }

  static void endSession() {
    // Save session logs before reset
    final sessionLogs = Logar.history;
    _saveSessionLogs(sessionLogs);

    // Reset for next session
    Logar.reset();
    print('Debug session ended, logs saved');
  }

  static void _saveSessionLogs(List<LogEntry> logs) {
    // Implementation to save session logs
    print('Session logs saved: ${logs.length} entries');
  }
}

Example: Configuration Reset

class ConfigurationManager {
  static void resetToDefaults() {
    // Reset logging configuration to defaults
    Logar.reset();
    print('Logging configuration reset to defaults');
  }

  static void resetAndReconfigure(LogarConfig newConfig) {
    // Reset and apply new configuration
    Logar.reset();
    Logar.configure(newConfig);
    print('Logging reset and reconfigured');
  }
}

Implementation

static void reset() {
  instance._config = const LogarConfig();
  instance._logHistory.clear();
  instance._collectors.clear();
}