log method

void log(
  1. Level level,
  2. dynamic message, {
  3. DateTime? time,
  4. Object? error,
  5. StackTrace? stackTrace,
  6. String? tag,
})

Log a message with level, optionally tagged with tag so it can be filtered by tag in the inspector's Logs tab.

Implementation

void log(
  Level level,
  dynamic message, {
  DateTime? time,
  Object? error,
  StackTrace? stackTrace,
  String? tag,
}) {
  if (!_active) {
    throw ArgumentError('AppLog has already been closed.');
  } else if (error != null && error is StackTrace) {
    throw ArgumentError('Error parameter cannot take a StackTrace!');
  } else if (level == Level.all) {
    throw ArgumentError('Log events cannot have Level.all');
    // ignore: deprecated_member_use
  } else if (level == Level.off || level == Level.nothing) {
    throw ArgumentError('Log events cannot have Level.off');
  }

  // Prefix the tag onto the message that actually reaches the printer, so
  // the console (or whatever [output] you passed) shows it too — not just
  // the inspector's Logs tab.
  final taggedMessage = tag == null || tag.isEmpty
      ? message
      : '[$tag] $message';

  final event = LogEvent(
    level,
    taggedMessage,
    time: time,
    error: error,
    stackTrace: stackTrace,
  );
  if (!_filter.shouldLog(event)) return;

  final lines = _printer.log(event);
  if (lines.isEmpty) return;

  // Issues with the user's own output should NOT influence app behavior.
  try {
    _output.output(OutputEvent(event, lines));
  } catch (_) {
    // Swallow — matches Logger's own defensive behavior around output().
  }

  AppLogStore.instance.add(
    AppLogEntry(
      id: _nextId++,
      level: _mapLevel(level),
      message: message.toString(),
      tag: tag,
      time: event.time,
      lines: lines,
      error: error,
      stackTrace: stackTrace,
    ),
  );
}