log static method

void log({
  1. required LogLevel logLevel,
  2. String? content,
  3. String? title,
  4. LogCategory? category,
  5. Object? exception,
  6. StackTrace? stackTrace,
  7. int maxStackTraceLine = 20,
  8. bool hideStackTrace = false,
})

터미널에 content에 해당하는 로그를 출력한다. Release 빌드일 때는 Sentry 측에 예외와 호출스택 정보만 전달하고, 터미널 출력부는 실행되지 않는다.

Implementation

static void log({
  required final LogLevel logLevel,
  final String? content,
  final String? title,
  final LogCategory? category,
  final Object? exception,
  final StackTrace? stackTrace,
  final int maxStackTraceLine = 20,
  final bool hideStackTrace = false,
}) {
  // 릴리즈 모드 로깅 제외
  if (_releaseMode) {
    return;
  }
  // 로그 카테고리 필터링
  if (categoryFilter != null && category != categoryFilter) {
    return;
  }

  final levelString = logLevel.string;
  final baseColor = logLevel.color;
  final brightColor = logLevel.brightColor;
  final levelColorTag = brightColor.toTag(logLevel.effect);
  final titleColorTag = brightColor.toTag();
  final contentColorTag = baseColor.toTag();
  final stackTraceColorTag = brightColor.toTag(LogEffectCode.dark);

  String? refinedStackTrace;
  if (stackTrace != null) {
    final splitStackTrace = stackTrace.toString().split('\n');
    refinedStackTrace = splitStackTrace
        .getRange(0, min(splitStackTrace.length, maxStackTraceLine))
        .fold(
          '',
          (previousValue, element) =>
              '${previousValue!}\n$stackTraceColorTag$element',
        );
  }

  CustomTrace? customTrace;
  if (stackTrace != null) {
    customTrace = CustomTrace(stackTrace);
  }

  final contentString = content?.replaceAll('\n', '\n$contentColorTag');
  var exceptionMessageString =
      exception?.toString().replaceAll('\n', '\n$contentColorTag');
  exceptionMessageString =
      exceptionMessageString == '' ? null : exceptionMessageString;

  final logString =
      // 로깅 레벨 표시
      '$levelColorTag[$levelString]'
      // 시간 표시
      ' $_resetColorTag$titleColorTag${DateTime.now()}'
      // 카테고리 표시
      '${category == null ? '' : ' [ $_resetColorTag$titleColorTag${category.name} ]'}'
      // 함수 이름, 파일 이름 표시
      '${customTrace == null ? '' : "$_resetColorTag$titleColorTag from '${customTrace.functionName}()' of '${customTrace.fileName}'"}'
      // 제목 표시
      '${title == null ? '' : '\n'
          '$_resetColorTag$titleColorTag<$title>'}'
      // 내용 표시
      '${contentString == null ? '' : '\n$_resetColorTag$contentColorTag$contentString'}'
      // Exception 표시
      '${exception == null ? '' : '\n'
          '\n$_resetColorTag${titleColorTag}Exception: $_resetColorTag$contentColorTag${exception.runtimeType}'}'
      '${exceptionMessageString == null ? '' : '\n'
          '\n$_resetColorTag$titleColorTag----- Start of Exception Message -----'
          '\n$_resetColorTag$titleColorTag$_resetColorTag$contentColorTag$exceptionMessageString'
          '\n$_resetColorTag$titleColorTag----- End of Exception Message -----'}'
      // StackTrace 표시
      '${hideStackTrace || stackTrace == null ? '' : '\n'
          '\n$_resetColorTag$titleColorTag----- Start of Stack Trace -----'
          '$refinedStackTrace'
          '\n$_resetColorTag$titleColorTag----- End of Stack Trace -----'
          '$_resetColorTag'}'
      '\n';
  developer.log(logString);
}