log method

void log (Level logLevel, dynamic message, [ Object error, StackTrace stackTrace, 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, message,
    [Object error, StackTrace stackTrace, Zone zone]) {
  Object object;
  if (isLoggable(logLevel)) {
    if (message is Function) {
      message = message();
    }

    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';
    }
    if (zone == null) zone = Zone.current;

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

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