configureLogging function

Logger configureLogging({
  1. Level level = Level.INFO,
  2. void logCallback(
    1. Level,
    2. String
    )?,
  3. bool enableHierarchicalLogging = true,
})

Configures the logging for the GenUI package.

This function should be called by applications using the GenUI package to configure the desired log level and to listen for log messages.

If enableHierarchicalLogging is true (the default), this function will set hierarchicalLoggingEnabled to true on the Logger class.

Implementation

Logger configureLogging({
  Level level = Level.INFO,
  void Function(Level, String)? logCallback,
  bool enableHierarchicalLogging = true,
}) {
  logCallback ??= (level, message) {
    // ignore: avoid_print
    print(message);
  };
  if (enableHierarchicalLogging) {
    hierarchicalLoggingEnabled = true;
  }
  recordStackTraceAtLevel = Level.SEVERE;
  genUiLogger.level = level;
  _loggingSubscription?.cancel();
  _loggingSubscription = genUiLogger.onRecord.listen((record) {
    logCallback?.call(
      record.level,
      '[${record.level.name}] ${record.time}: ${record.message}',
    );
    if (record.error != null) {
      logCallback?.call(record.level, '  Error: ${record.error}');
    }
    if (record.stackTrace != null) {
      logCallback?.call(record.level, '  Stack trace:\n${record.stackTrace}');
    }
  });

  return genUiLogger;
}