Logging for Dart SDK

By default, logging in the Dart SDK is disabled. You can enable it either at the SDK instance level or by providing a zone-scoped logger for specific code paths.

Enable logging at the instance level

Use the logging parameter on the PubNub constructor with LoggingConfiguration to enable and control logging globally for that instance. You can also print to console automatically.

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

void main() async {
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'),
),
logging: LoggingConfiguration(
logLevel: Level.all, // Enable all logs
logToConsole: true, // Optional: print formatted logs to console automatically
// loggerName: 'myApp', // Optional: custom logger name
),
show all 17 lines

Change log level at runtime

// Change logging verbosity dynamically later in your app
pubnub.setLogLevel(Level.info);

Enable logging for a specific code path

To enable logging for specific parts of your code only, 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

Where logs are emitted

Typical sources include:

  • API operations (for example publish)
  • Networking requests and responses
  • Instance initialization details

Log levels

StreamLogger assigns a weight to each log record. A message is logged if its level is less than or equal to the active logLevel. For example, if logLevel is set to Level.warning (80), 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.fine is 500
  • Level.silly is 640
  • Level.all is 10000 and turns logging on for all levels
Last updated on