Logging for Dart SDK
This page explains how to enable logging in the PubNub Dart Software Development Kit (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.
1import 'package:pubnub/pubnub.dart';
2import 'package:pubnub/logging.dart';
3
4void main() async {
5 final pubnub = PubNub(
6 defaultKeyset: Keyset(
7 subscribeKey: 'demo', // Replace with your Subscribe Key from the PubNub Admin Portal
8 publishKey: 'demo', // Replace with your Publish Key from the PubNub Admin Portal
9 userId: UserId('loggingDemoUser'),
10 ),
11 logging: LoggingConfiguration(
12 logLevel: Level.all, // Enable all logs
13 logToConsole: true, // Optional: print formatted logs to console automatically
14 // loggerName: 'myApp', // Optional: custom logger name
15 ),
show all 17 linesChange log level at runtime
1// Change logging verbosity dynamically later in your app
2pubnub.setLogLevel(Level.info);
Enable logging for a specific code path
To enable logging for specific parts of your code only, follow these steps:
-
Import logging utilities and the SDK.
1 import 'package:pubnub/pubnub.dart';
2 import 'package:pubnub/logging.dart';
3
4 void main() {
5 // This is the minimum required code to import logging utilities
6 print('PubNub logging utilities imported successfully');
7 } -
Create a logger instance which implements the
ILoggerinterface.In this example we use a logger that ships with the PubNub SDK -
StreamLogger.
show all 18 lines1import 'package:pubnub/pubnub.dart';
2import 'package:pubnub/logging.dart';
3
4void main() {
5 // Create a logger with name 'myApp' that logs everything
6 final logger = StreamLogger.root('myApp', logLevel: Level.all);
7
8 // Initialize PubNub with demo keys
9 final pubnub = PubNub(
10 defaultKeyset: Keyset(
11 subscribeKey: 'demo', // Replace with your Subscribe Key from the PubNub Admin Portal
12 publishKey: 'demo', // Replace with your Publish Key from the PubNub Admin Portal
13 userId: UserId('loggingDemoUser'),
14 ),
15 );StreamLogger.rootconstructor requires a name for the logger. Additionally, you can pass in your desired log level and instruct the logger to record stack traces.StreamLogger#streamexposes aStream<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 theLogRecordclass to see what information is available.
show all 23 lines1import 'package:pubnub/pubnub.dart';
2import 'package:pubnub/logging.dart';
3
4void main() {
5 // Create a logger with name 'myApp' that logs everything
6 final logger = StreamLogger.root('myApp', logLevel: Level.all);
7
8 // Listen to the log stream and format messages
9 logger.stream.listen((record) {
10 print('[${record.time}] ${Level.getName(record.level)}: ${record.message}');
11 });
12
13 // Initialize PubNub with demo keys
14 final pubnub = PubNub(
15 defaultKeyset: Keyset(If all you want to do is to print the messages to the output you can use a
LogRecord.defaultPrinterand pass that as a listener to the logger stream.
show all 21 lines1import 'package:pubnub/pubnub.dart';
2import 'package:pubnub/logging.dart';
3
4void main() {
5 // Create a logger with name 'myApp' that logs everything
6 final logger = StreamLogger.root('myApp', logLevel: Level.all);
7
8 // Use the built-in default printer
9 logger.stream.listen(LogRecord.defaultPrinter);
10
11 // Initialize PubNub with demo keys
12 final pubnub = PubNub(
13 defaultKeyset: Keyset(
14 subscribeKey: 'demo', // Replace with your Subscribe Key from the PubNub Admin Portal
15 publishKey: 'demo', // Replace with your Publish Key from the PubNub Admin Portal -
Wrap the parts of the code that you want to log in a
provideLogger.
show all 41 lines1import 'package:pubnub/pubnub.dart';
2import 'package:pubnub/logging.dart';
3
4Future<void> somethingThatWorks() async {
5 print('This function works without issues');
6}
7
8Future<void> troublesomeFunction() async {
9 print('This is the function we want to debug with logging');
10
11 // Initialize PubNub to see logs
12 final pubnub = PubNub(
13 defaultKeyset: Keyset(
14 subscribeKey: 'demo', // Replace with your Subscribe Key from the PubNub Admin Portal
15 publishKey: 'demo', // Replace with your Publish Key from the PubNub Admin PortalIn case of a Flutter app, you can wrap the entire
runAppmethod.
show all 39 lines1import 'package:flutter/material.dart';
2import 'package:pubnub/pubnub.dart';
3import 'package:pubnub/logging.dart';
4
5// Simple Flutter app for demo purposes
6class MyApp extends StatelessWidget {
7
8 Widget build(BuildContext context) {
9 return MaterialApp(
10 home: Scaffold(
11 appBar: AppBar(title: Text('PubNub Logging Example')),
12 body: Center(child: Text('Check console for logs')),
13 ),
14 );
15 }
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.offis 0 and turns logging offLevel.shoutis 10Level.fatalis 20Level.severeis 40Level.warningis 80Level.infois 160Level.verboseis 320Level.fineis 500Level.sillyis 640Level.allis 10000 and turns logging on for all levels