Giter VIP home page Giter VIP logo

redux_logging's Introduction

redux_logging

Build Status codecov

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 = new Store<int>(
  (int state, dynamic action) => state + 1,
  initialValue: 0,
  // Note the LoggingMiddleware should come last in the list of Middleware!
  middleware: [myOtherMiddleware, new LoggingMiddleware.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 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, 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 action
String onlyLogActionFormatter<State>(
  State state,
  dynamic action,
  DateTime timestamp,
) {
  return "{Action: $action}";
}

// Create your middleware using the formatter.
final middleware = new LoggingMiddleware(formatter: onlyLogActionFormatter);

// Add the middleware with your formatter to your Store
final store = new Store<int>(
  (int state, dynamic action) => state + 1,
  initialState: 0,
  middleware: [middleware],
);

redux_logging's People

Contributors

basti1253 avatar bcko avatar brianegan avatar dmitriibogatko avatar zoechi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

redux_logging's Issues

Make timestamp optional in formatter

2020-06-22 02:57:32.185 INFO Redux - {
  Action: Clear Sprints Action,
  State: {
      sprints: []
    },
  Timestamp: 2020-06-22 02:57:32.185
}

Hi, I would love to make timestamp optional here. We can create our custom formatter to solve this issue but would be really great if timestamp is boolean in default formatter

redux_logging

// A simple formatter that puts all data on one line
  static String singleLineFormatter(
    dynamic state,
    dynamic action,
    DateTime timestamp,
  ) {
    return "{Action: $action, State: $state, ts: ${new DateTime.now()}}";
  }

  /// A formatter that puts each attribute on it's own line
  static String multiLineFormatter(
    dynamic state,
    dynamic action,
    DateTime timestamp,
  ) {
    return "{\n" +
        "  Action: $action,\n" +
        "  State: $state,\n" +
        "  Timestamp: ${new DateTime.now()}\n" +
        "}";
  }

Upgrade logging package to 1.2.0

Hi !

Thanks for this package, its very useful.
Could you just update the logging package to 1.2.0?
This causes conflicts in some projects.

Thank you very much

usage with built_redux

Hi guys
Actually this is a question.
When can use this loggin with built_redux?

Thank you in advance

printer() causes Not a Constant Expression exception when using with Flutter, Dart2 preview

Trying to initialise the store with logging middleware:

import 'package:redux_logging/redux_logging.dart';

class MyApp {
  final store = new Store<AppState>(appReducer,
    initialState: new AppState(), 
    middleware: [new LoggingMiddleware.printer()]
  );
}

But get the following exception at runtime:

Exception has occurred.
'package:redux_logging/redux_logging.dart': error: Not a constant expression.

Appears to be a Flutter/Dart 2 issue, works fine with Flutter and Dart 1.

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.1.5, on Mac OS X 10.13.3 17D102, locale en-AU)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 9.2)
[✓] Android Studio (version 3.0)
[✓] VS Code (version 1.21.1)
[✓] Connected devices (1 available)

type 'List<Object>' is not a subtype of type 'List<(Store<AppState>, dynamic, (dynamic) => void) => void>'

Error

Launching lib/main.dart on iPhone X in debug mode...
[VERBOSE-2:dart_error.cc(16)] Unhandled exception:
type 'List<Object>' is not a subtype of type 'List<(Store<AppState>, dynamic, (dynamic) => void) => void>'
#0      new TangboleApp (file:///Users/jagger/projects/tangbole/app/lib/main.dart:36:79)
#1      main (file:///Users/jagger/projects/tangbole/app/lib/main.dart:18:10)
#2      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
#3      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)

Code

...

    final persistor = Persistor<AppState>(
      storage: FlutterStorage('tangbole'),
      decoder: AppState.fromJson,
      version: 10000,
      migrations: {},
    );

    var mws = [persistor.createMiddleware()];
    mws.addAll(createMiddlewares());
    mws.add(LoggingMiddleware.printer());

    final store = Store<AppState>(
      appReducer,
      initialState: AppState(),
      middleware: mws,
    );

    persistor.load(store);

...

Env

name: tangbole
description: A tumblr client app.

dependencies:
  cupertino_icons: ^0.1.2
  english_words: ^3.1.0
  flutter:
    sdk: flutter
  flutter_redux: ^0.5.1
  http: ^0.11.3+16
  meta: ^1.1.5
  redux: ^3.0.0
  redux_logging: ^0.3.0
  redux_persist: ^0.7.0-rc.2
  redux_persist_flutter: ^0.6.0-rc.1

dev_dependencies:
  flutter_driver:
    sdk: flutter
  flutter_test:
    sdk: flutter
  mockito: ^2.2.3

flutter:
  uses-material-design: true
  assets:
    - images/lake.jpg

Logging only displays so many lines

When the logging is too many lines of my AppState, the logging is cut off.

When home.toString is short:

I/flutter ( 3632):       me: {
I/flutter ( 3632):         user: { id: 2, socialType: SocialType.facebook, socialId: 1905457732907903, displayName: Curious Chloe, email: [email protected] },
I/flutter ( 3632):       },
I/flutter ( 3632):       home: {
I/flutter ( 3632):         stores: 19 stores,
I/flutter ( 3632):       },
I/flutter ( 3632):       user: Instance of 'UserState',
I/flutter ( 3632):       reward: Instance of 'RewardState',
I/flutter ( 3632):       error: {
I/flutter ( 3632):         message: null,
I/flutter ( 3632):       }
I/flutter ( 3632):     }, ts: 2018-12-01 02:38:08.406835}

When home.toString is too long:

I/flutter ( 3632):       me: {
I/flutter ( 3632):         user: { id: 2, socialType: SocialType.facebook, socialId: 1905457732907903, displayName: Curious Chloe, email: [email protected] },
I/flutter ( 3632):       },
I/flutter ( 3632):       home: {
I/flutter ( 3632):         stores: {4: { id: 4, name: Burn's Cafe }, 5: { id: 5, name: Red Sparrow Pizza }, 6: { id: 6, name: Cie Lest }, 7: { id: 7, name: The Hungry Cartel }, 8: { id: 8, name: Higher Ground }, 15: { id: 15, name: CoCo Fresh Tea & Juice }, 16: { id: 16, name: CoCo Fresh Tea & Juice }, 10: { id: 10, name: CoCo Fresh Tea & Juice }, 11: { id: 11, name: CoCo Fresh Tea & Juice }, 12: { id: 12, name: CoCo Fresh Tea & Juice }, 13: { id: 13, name: CoCo Fresh Tea & Juice }, 14: { id: 14, name: CoCo Fresh Tea & Juice }, 3: { id: 3, name: Workshop Meowpresso }, 9: { id: 9, name: 8bit }, 2: { id: 2, name: Sokyo }, 1: { id: 1, name: Dumplings & Co. }, 17: { id: 17, name: Mad Mex }, 18: { id: 18, name: Mad Mex }, 19: { id: 19, name: Mad Mex }},
I/flutter ( 3632):       },
I/flutter ( 3632):       user: Instance 

Since the last line was cut off at user: Instance maybe it's a character limit???

Flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v0.11.9, on Microsoft Windows [Version 10.0.17134.407], locale en-AU)
[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
    X Android license status unknown.
[√] Android Studio (version 3.1)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
[√] IntelliJ IDEA Ultimate Edition (version 2018.2)
[√] IntelliJ IDEA Ultimate Edition (version 2018.3)
[√] Connected device (1 available)

! Doctor found issues in 1 category.

Dependencies:

    flutter:
        sdk: flutter
    redux: ^3.0.0
    flutter_redux: ^0.5.2
    redux_logging: ^0.3.0
    redux_thunk: ^0.2.0
    redux_persist_flutter: ^0.8.0-rc.0
    redux_persist: ^0.8.0-rc.0
    flutter_facebook_login: ^1.1.1
    google_sign_in: ^3.2.4
    http: ^0.12.0
    graphql_flutter: ^0.9.4
    cupertino_icons: ^0.1.2
    timeago: ^2.0.9
    intl: ^0.15.7
    flutter_svg: ^0.6.3

Maybe this is the desired behaviour?

Thanks for taking the time to read my issue.

State: Instance of 'AppState'

I would like to see the actual output of the AppState, i.e. my state object. But I get only this message:

State: Instance of 'AppState'

How to print all action properties?

I've come from the JS world and printing the object props was the default behaviour there.
Here everything is classes and printing a class gives me just the instance: Instance of MyAction.

Maybe there's some known recipe to make a redux_logging behave like a JS version?

Support redux ^4.0.0

Currently can't use flutter redux with this library as it requires redux version ^4.0.0.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.