LoggingMiddleware<State> class
Connects a Logger
to a Redux Store.
Every action that is dispatched will be logged to the Logger
, along with
the new State
that was created as a result of the action reaching your
Store
's reducer.
By default, this class does not print anything to your console or to a web
service, such as Fabric or Sentry. It simply logs entries to a Logger
instance.
You can then listen to the Logger.onRecord
Stream, and print to the
console or send these actions to a web service.
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.
Simple Printing example
If you just want an easy way to print actions as they are dispatched to your
console / terminal, use the new LoggingMiddleware.printer()
factory.
final store = new Store<int>(
(int state, action) => state + 1,
initialValue: 0,
middleware: [new LoggingMiddleware.printer()]
);
store.dispatch('Hi'); // prints {Action: 'Hi', Store: 1, Timestamp: ...}
Example
If you only want to log actions to a Logger
, use the default constructor.
// Create your own Logger
final logger = new Logger('Redux Logger');
// Pass it to your Middleware
final middleware = new LoggingMiddleware(logger: logger);
final store = new Store<int>(
(int state, 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));
Constructors
-
LoggingMiddleware({Logger? logger, Level level = Level.INFO, MessageFormatter<
State> formatter = singleLineFormatter}) -
The default constructor. It will only log actions to the given
Logger
, but it will not print to the console or anything else. -
LoggingMiddleware.printer({Logger? logger, Level level = Level.INFO, MessageFormatter<
State> formatter = singleLineFormatter}) -
A helper factory for creating a piece of LoggingMiddleware that only
prints to the console.
factory
Properties
-
formatter
→ MessageFormatter<
State> -
A function that formats the String for printing
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- level → Level
-
The log
Level
at which the actions will be recordedfinal - logger → Logger
-
The
Logger
instance that actions will be logged to.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
Store< State> store, dynamic action, NextDispatcher next) → void -
A
Middleware
function that intercepts a dispatched action -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
multiLineFormatter(
dynamic state, dynamic action, DateTime timestamp) → String - A formatter that puts each attribute on it's own line
-
singleLineFormatter(
dynamic state, dynamic action, DateTime timestamp) → String - A simple formatter that puts all data on one line