setupLogger function

*<Null safety>*

void setupLogger ({String? name, Level? level, List<String>? globalTags, bool? forceShowCodeLocation})

Implementation

void setupLogger({
  String? name,
  Level? level,
  List<String>? globalTags,
  bool? forceShowCodeLocation,
}) {
  // set the log variable to the root logger and set the level to that
  // specified by level. We do this so subsequent calls to the log method
  // will not run the default setup method.
  log = Logger.root..level = level ?? Level.ALL;

  // connect to the logger writer here. If log has already been called this
  // method will be a noop. At this point, _logWriter will not be null
  _connectToLogWriterIfNeeded();

  final loggerBaseName =
      name ?? Platform.script?.pathSegments?.lastWhereOrNull((_) => true);

  // Tags get appended to each log statement. We put the name, if present
  // as the first tag so it makes it easier to identify.
  // We remove any null values before sending them to the logger
  final List<String> tags = []
    ..addAll(globalTags ?? const [])
    ..removeWhere((t) => t == null || t.isEmpty);

  bool inCheckedMode = false;
  assert(() {
    inCheckedMode = true;
    return true;
  }());

  _logWriter
    ..loggerBaseName = loggerBaseName
    ..globalTags = tags
    ..forceShowCodeLocation = forceShowCodeLocation ?? inCheckedMode;
}