Troubleshooting PubNub Dart SDK

By default, logging in the Dart SDK is disabled. To enable logging, follow these steps:

  1. Import logging utilities and the SDK.
import 'package:pubnub/pubnub.dart';
import 'package:pubnub/logging.dart';
  1. Create a logger instance which implements the ILogger interface.

    In this example we use a logger that ships with the PubNub SDK - StreamLogger.

    final logger = StreamLogger.root('myApp', logLevel: Level.all);

    StreamLogger.root constructor requires a name for the logger. Additionally, you can pass in your desired log level and instruct the logger to record stack traces.

    StreamLogger#stream exposes a Stream<LogRecord> that you can subscribe to. You can use this stream to print log lines to the screen, send them to your debugging platform or write them to a file. Take a look at the LogRecord class to see what information is available.

    logger.stream.listen((record) {
    print('[${record.time}] ${Level.getName(record.level)}: ${record.message}');
    });

    If all you want to do is to print the messages to the output you can use a LogRecord.defaultPrinter and pass that as a listener to the logger stream.

    logger.stream.listen(LogRecord.defaultPrinter);
  2. Wrap the parts of the code that you want to log in a provideLogger.

    void main() async {
    await somethingThatWorks();

    await provideLogger(logger, () async {
    await troublesomeFunction();
    });

    await somethingElseThatWorks();
    }

    In case of a Flutter app, you can wrap the entire runApp method.

    void main() {
    provideLogger(logger, () async {
    runApp();
    });
    }

Log Levels

StreamLogger assigns a weight to each log record. When you set the logging level, it only logs messages with a weight less than loglevel. For example, if logLevel is set to Level.warning (which is equal to 80), only messages with level 80 or less will be recorded.

The threshold levels are defined in the Level class as follows:

  • Level.off is 0 and turns logging off
  • Level.shout is 10,
  • Level.fatal is 20,
  • Level.severe is 40,
  • Level.warning is 80,
  • Level.info is 160,
  • Level.verbose is 320,
  • Level.silly is 640,
  • Level.all is 10000 and turns logging on for all levels.
Last updated on
On this page