log method

void log(
  1. Level logLevel,
  2. Object? message, [
  3. Object? error,
  4. StackTrace? stackTrace,
  5. Zone? zone,
])

Adds a log record for a message at a particular logLevel if isLoggable(logLevel) is true.

Use this method to create log entries for user-defined levels. To record a message at a predefined level (e.g. Level.INFO, Level.WARNING, etc) you can use their specialized methods instead (e.g. info, warning, etc).

If message is a Function, it will be lazy evaluated. Additionally, if message or its evaluated value is not a String, then 'toString()' will be called on the object and the result will be logged. The log record will contain a field holding the original object.

The log record will also contain a field for the zone in which this call was made. This can be advantageous if a log listener wants to handler records of different zones differently (e.g. group log records by HTTP request if each HTTP request handler runs in it's own zone).

Implementation

void log(Level logLevel, Object? message,
    [Object? error, StackTrace? stackTrace, Zone? zone]) {
  Object? object;
  if (isLoggable(logLevel)) {
    if (message is Function) {
      message = (message as Object? Function())();
    }

    String msg;
    if (message is String) {
      msg = message;
    } else {
      msg = message.toString();
      object = message;
    }

    if (stackTrace == null && logLevel >= recordStackTraceAtLevel) {
      stackTrace = StackTrace.current;
      error ??= 'autogenerated stack trace for $logLevel $msg';
    }
    zone ??= Zone.current;

    final record =
        LogRecord(logLevel, msg, fullName, error, stackTrace, zone, object);

    if (parent == null) {
      _publish(record);
    } else if (!hierarchicalLoggingEnabled) {
      root._publish(record);
    } else {
      Logger? target = this;
      while (target != null) {
        target._publish(record);
        target = target.parent;
      }
    }
  }
}