Logging for 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';

    void main() {
    // This is the minimum required code to import logging utilities
    print('PubNub logging utilities imported successfully');
    }
  2. Create a logger instance which implements the ILogger interface.

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

    import 'package:pubnub/pubnub.dart';
    import 'package:pubnub/logging.dart';

    void main() {
    // Create a logger with name 'myApp' that logs everything
    final logger = StreamLogger.root('myApp', logLevel: Level.all);

    // Initialize PubNub with demo keys
    final pubnub = PubNub(
    defaultKeyset: Keyset(
    subscribeKey: 'demo', // Replace with your Subscribe Key from the PubNub Admin Portal
    publishKey: 'demo', // Replace with your Publish Key from the PubNub Admin Portal
    userId: UserId('loggingDemoUser'),
    ),
    );
    show all 18 lines

    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.

    import 'package:pubnub/pubnub.dart';
    import 'package:pubnub/logging.dart';

    void main() {
    // Create a logger with name 'myApp' that logs everything
    final logger = StreamLogger.root('myApp', logLevel: Level.all);

    // Listen to the log stream and format messages
    logger.stream.listen((record) {
    print('[${record.time}] ${Level.getName(record.level)}: ${record.message}');
    });

    // Initialize PubNub with demo keys
    final pubnub = PubNub(
    defaultKeyset: Keyset(
    show all 23 lines

    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.

    import 'package:pubnub/pubnub.dart';
    import 'package:pubnub/logging.dart';

    void main() {
    // Create a logger with name 'myApp' that logs everything
    final logger = StreamLogger.root('myApp', logLevel: Level.all);

    // Use the built-in default printer
    logger.stream.listen(LogRecord.defaultPrinter);

    // Initialize PubNub with demo keys
    final pubnub = PubNub(
    defaultKeyset: Keyset(
    subscribeKey: 'demo', // Replace with your Subscribe Key from the PubNub Admin Portal
    publishKey: 'demo', // Replace with your Publish Key from the PubNub Admin Portal
    show all 21 lines
  3. Wrap the parts of the code that you want to log in a provideLogger.

    import 'package:pubnub/pubnub.dart';
    import 'package:pubnub/logging.dart';

    Future<void> somethingThatWorks() async {
    print('This function works without issues');
    }

    Future<void> troublesomeFunction() async {
    print('This is the function we want to debug with logging');

    // Initialize PubNub to see logs
    final pubnub = PubNub(
    defaultKeyset: Keyset(
    subscribeKey: 'demo', // Replace with your Subscribe Key from the PubNub Admin Portal
    publishKey: 'demo', // Replace with your Publish Key from the PubNub Admin Portal
    show all 41 lines

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

    import 'package:flutter/material.dart';
    import 'package:pubnub/pubnub.dart';
    import 'package:pubnub/logging.dart';

    // Simple Flutter app for demo purposes
    class MyApp extends StatelessWidget {

    Widget build(BuildContext context) {
    return MaterialApp(
    home: Scaffold(
    appBar: AppBar(title: Text('PubNub Logging Example')),
    body: Center(child: Text('Check console for logs')),
    ),
    );
    }
    show all 39 lines

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