export static method

String? export(
  1. String name, {
  2. bool export = true,
  3. bool onlyOnError = false,
})

Exports a tag's logs to console as Markdown and returns the formatted string.

Prints the formatted Markdown with visual separators for easy copying. Copy the output between the separators and paste into Claude or any AI for analysis.

Returns the formatted Markdown string (without separators) for custom use, such as saving to a file. Returns null in release mode or if the tag doesn't exist.

The tag is cleared after export (or after skipping if export is disabled).

Parameters:

  • name: Tag identifier to export
  • export: If false, skips export entirely (clears tag without printing). Use this for custom conditions. Default: true.
  • onlyOnError: If true, only exports if there are WARNING/ERROR/CRITICAL entries. Useful for conditional export in try/catch blocks.

Example:

// Always export and get the string
final markdown = Log.export('auth');

// Save to file if needed
if (markdown != null) {
  File('debug_log.md').writeAsStringSync(markdown);
}

// Conditional export based on your own logic
bool shouldExport = myCustomValidation();
Log.export('flow', export: shouldExport);

// Only export if errors occurred
Log.export('flow', onlyOnError: true);

// Combine both: custom condition AND must have errors
Log.export('flow', export: isDebugMode, onlyOnError: true);

Implementation

static String? export(
  String name, {
  bool export = true,
  bool onlyOnError = false,
}) {
  String? result;
  assert(() {
    final entries = _tags[name];
    if (entries == null || entries.isEmpty) return true;

    // Skip export if export parameter is false
    if (!export) {
      _tags.remove(name);
      return true;
    }

    // Skip export if onlyOnError is true and no errors exist
    if (onlyOnError) {
      final hasErrors = entries.any((e) => e.isError);
      if (!hasErrors) {
        _tags.remove(name);
        return true;
      }
    }

    final errorCount = entries.where((e) => e.isError).length;
    final content = MdFormatter.format(name, entries);

    // Build full formatted output
    final buffer = StringBuffer()
      ..writeln('# Tag: $name')
      ..writeln(_separator)
      ..writeln(content)
      ..writeln(_separator);
    result = buffer.toString();

    // Announce export via logger
    _logger.info(
      'Exporting tag: $name (${entries.length} entries, $errorCount errors)',
    );

    // Print to console
    // ignore: avoid_print
    print('\n$result');

    _tags.remove(name);
    return true;
  }());
  return result;
}