Logger logger

Fetches the logger instance.

Source

Logger get logger {
  if (_logger != null) {
    return _logger;
  }

  hierarchicalLoggingEnabled = true;
  _logger = new Logger("DSA");

  _logger.onRecord.listen((record) {
    List<String> lines = record.message.split("\n");
    bool inlineErrors = _getLogSetting(
      record,
      "dsa.logger.inline_errors",
      true
    );

    bool enableSequenceNumbers = _getLogSetting(
      record,
      "dsa.logger.sequence",
      false
    );

    if (inlineErrors) {
      if (record.error != null) {
        lines.addAll(record.error.toString().split("\n"));
      }

      if (record.stackTrace != null) {
        lines.addAll(record.stackTrace.toString()
          .split("\n")
          .where((x) => x.isNotEmpty)
          .toList());
      }
    }

    String rname = record.loggerName;

    if (record.zone["dsa.logger.name"] is String) {
      rname = record.zone["dsa.logger.name"];
    }

    bool showTimestamps = _getLogSetting(
      record,
      "dsa.logger.show_timestamps",
      false
    );

    if (!_getLogSetting(record, "dsa.logger.show_name", true)) {
      rname = null;
    }

    for (String line in lines) {
      String msg = "";

      if (enableSequenceNumbers) {
        msg += "[${record.sequenceNumber}]";
      }

      if (showTimestamps) {
        msg += "[${record.time}]";
      }

      msg += "[${record.level.name}]";

      if (rname != null) {
        msg += "[${rname}]";
      }

      msg += " ";
      msg += line;

      if (_getLogSetting(record, "dsa.logger.print", true)) {
        print(msg);
      }
    }

    if (!inlineErrors) {
      if (record.error != null) {
        print(record.error);
      }

      if (record.stackTrace != null) {
        print(record.stackTrace);
      }
    }
  });

  updateLogLevel(
    const String.fromEnvironment(
      "dsa.logger.default_level",
      defaultValue: "INFO"
    )
  );

  return _logger;
}