Redux_logging은 Redux.dart용 미들웨어로, 각 액션과 처리 후의 결과 상태를 기록합니다. 개발자가 상태 변경을 디버깅할 수 있도록 실시간으로 액션과 그 앱 상태에 대한 영향을 출력해 줍니다. 개발 환경에 적합하며, 상태가 시간이 지남에 따라 어떻게 변화하는지 시각화하는 데 도움을 줍니다. 기존의 Redux.dart 설정과 원활하게 통합되며, Android, iOS, Web, macOS, Windows, Linux 등 모든 주요 플랫폼을 지원합니다.
사용 사례
디버깅 상태 전이
개발 중 작업 흐름 모니터링
복잡한 앱에서 상태 변경 추적
리듀서 로직 검증
디버깅에서 개발자 경험 향상
주요 기능
각 디스패치 후 작업 및 상태를 로그 기록
Redux.dart와의 간단한 통합
크로스 플랫폼 지원
최소 성능 오버헤드
사용자 정의 가능한 로깅 동작
적합한 대상
Redux.dart를 사용하는 개발자
상태 투명성을 우선시하는 팀
복잡한 상태 관리가 필요한 애플리케이션
디버깅에 집중하는 프론트엔드 개발자
Redux 패턴 학습 또는 가르치기
고려 사항
생산용 빌드에는 권장되지 않습니다
로그 기록이 너무 자주 이루어지면 성능에 영향을 미칠 수 있습니다
고급 사용을 위해서는 수동 구성이 필요합니다
기본 콘솔 출력만 가능
런타임 이후 로그를 지속하지 않습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
redux_logging
Connects a Logger to a Redux Store. Can also simply print the latest state / action changes.
Logs every Action that is dispatched to the Store, along with the current State.
By default, this class does not print anything to your console or send data to a web service, such as Fabric or Sentry. It simply logs entries to a Logger instance.
If you simply want to print the latest action and state to your console / terminal, create a new LoggingMiddleware.printer() and pass it to your Store upon creation.
If you want more control over where the logged data is sent, you can listen to your Logger's onRecord Stream.
Dart Versions
Dart 1 support: 0.1.x
Dart 2 Support: 0.2.x+
Simple Printing example
If you just want an easy way to print actions to your console / terminal as they are dispatched, use the new LoggingMiddleware.printer() factory.
Note: The LoggingMiddleware needs be the LAST middleware in the list.
import"package:redux/redux.dart";
import'package:redux_logging/redux_logging.dart';
final store = newStore<int>(
(int state, dynamic action) => state + 1,
initialValue: 0,
// Note the LoggingMiddleware should come last in the list of Middleware!
middleware: [myOtherMiddleware, newLoggingMiddleware.printer()]
);
store.dispatch("Hi"); // prints {Action: "Hi", Store: 1, Timestamp: ...}
Example
If you only want to log actions to a Logger, and choose how to handle the output, use the default constructor.
import'package:logging/logging.dart';
import"package:redux/redux.dart";
import'package:redux_logging/redux_logging.dart';
// Create your own Loggerfinal logger = newLogger("Redux Logger");
// Pass it to your Middlewarefinal middleware = newLoggingMiddleware(logger: logger);
final store = newStore<int>(
(int state, dynamic action) => state + 1,
initialState: 0,
middleware: [middleware],
);
// Note: One quirk about listening to a logger instance is that you're// actually listening to the Singleton instance of *all* loggers.
logger.onRecord
// Filter down to [LogRecord]s sent to your logger instance
.where((record) => record.loggerName == logger.name)
// Print them out (or do something more interesting!)
.listen((loggingMiddlewareRecord) => print(loggingMiddlewareRecord));
Formatting the log message
This library includes two formatters out of the box:
LoggingMiddleware.singleLineFormatter
LoggingMiddleware.multiLineFormatter
You can optionally control the format of the message that will be logged by implementing your own MessageFormatter and passing it to the LoggingMiddleware constructor. It is a simple function that takes three parameters: the State, Action, and Timestamp.
Formatting Example
import"package:redux/redux.dart";
import'package:redux_logging/redux_logging.dart';
// Create a formatter that only prints out the dispatched actionString onlyLogActionFormatter<State>(
State state,
dynamic action,
DateTime timestamp,
) {
return"{Action: $action}";
}
// Create your middleware using the formatter.final middleware = newLoggingMiddleware(formatter: onlyLogActionFormatter);
// Add the middleware with your formatter to your Storefinal store = newStore<int>(
(int state, dynamic action) => state + 1,
initialState: 0,
middleware: [middleware],
);