Flutter 로그 FLog 사용법

개발하자 백조·2022년 12월 7일
0

https://pub.dev/packages/f_logs
f_logs 2.0.1 기준 작성


처음에 init() 으로 초기 설정 해준다

init() {
  LogsConfig config = FLog.getDefaultConfigurations()
    ..isDevelopmentDebuggingEnabled = true
    ..timestampFormat = TimestampFormat.TIME_FORMAT_FULL_3
    ..formatType = FormatType.FORMAT_CUSTOM
    ..fieldOrderFormatCustom = [
      FieldName.TIMESTAMP,
      FieldName.LOG_LEVEL,
      FieldName.CLASSNAME,
      FieldName.TEXT,
      FieldName.EXCEPTION,
      FieldName.STACKTRACE
    ]
    ..customOpeningDivider = "["
    ..customClosingDivider = "]";

  FLog.applyConfigurations(config);
}

샘플 코드의 3번을 수정해서 사용중이다.

로그 찍는 법

INFO

FLog.info(text: "writing test info log");

결과
[2022-12-07 03:42:27] [LogLevel.INFO] [HomePageState] [writing test info log] [null] [1670384547762]

EXEPTION

    try {..} on Exception catch (exception) {
      FLog.error(
        text: "Exception Text",
        dataLogType: "Exception LogType",
        className: "Exception Class",
        exception: exception,
      );
    }

결과
[2022-12-07 03:44:34] [LogLevel.ERROR] [Exception Class] [Exception Text] [IntegerDivisionByZeroException] [Exception LogType] [1670384674836]

ERROR

    try {..} on Error catch (error) {
      FLog.error(
        text: "Error Text",
        dataLogType: "Error LogType",
        className: "Error Class",
        exception: error,
      );
    }

결과
[2022-12-07 03:44:34] [LogLevel.ERROR] [Error Class] [Error Text] [RangeError (index): Invalid value: Not in inclusive range 0..4: -1] [Error LogType] [1670384674840]

ERROR WITH STACKTRACE

FLog.error(
  text: "Error with StackTrace",
  dataLogType: "StackTrace LogType",
  className: "StackTrace Class",
  exception: Exception("Exception and StackTrace"),
  stacktrace: StackTrace.current,
);

stacktrace: 부분을 추가해주면 된다.

결과

[2022-12-07 04:12:56] [LogLevel.ERROR] [StackTrace Class] [Error with StackTrace] [Exception: Exception and StackTrace] 
[#0      HomePageState._buildRow4.<anonymous closure> (package:logging_test/main.dart:168:36)
#1      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:1005:21)
#2      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
...

WARNING

FLog.warning(
  className: "HomePage",
  methodName: "_buildRow1",
  text: "Warning",
  dataLogType: "Warning",
);

결과
[2022-12-07 03:44:34] [LogLevel.WARNING] [HomePage] [Warning] [Warning] [1670384674844]

로그 출력하기

사실 큰 의미가 없다. 위에 코드로 로그 만들면 콘솔에는 알아서 찍히고 print log 같은거 하면 상위 10줄 정도 밖에 안보여준다.
printLogs()랑 printFileLogs() 가 다르다는거만 알면 된다.

printLogs() : 앱 데이터베이스(FlogDao)에 저장된 로그
printFileLogs() : 디바이스 로컬 storage에 flog.txt로 저장된 로그

로그 저장하기

현재 앱 데이터베이스 로그를 디바이스에 'flog.txt'로 저장해준다. 위치는
flutter flog log directory
Device File Explorer 통해 찾으면 된다.

FLog.exportLogs();

굉장히 간단하다.

로그 지우기

지우는 법도 간단하다.

FLog.clearLogs();

하면 앱 데이터베이스 로그를 지워준다.

만약 flog.txt도 지우고 싶으면
로그 저장하기를 한 번 해주면 된다.

FLog.clearLogs();
FLog.exportLogs();
profile
개발자로서 100가지 일을 해보고 싶은 조경현의 개발 블로그

0개의 댓글