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 linesChange 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:
-
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');
} -
Create a logger instance which implements the
ILogger
interface.In this example we use a logger that ships with the PubNub SDK -
StreamLogger
.
show all 18 linesimport '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'),
),
);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 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 theLogRecord
class to see what information is available.
show all 23 linesimport '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(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.
show all 21 linesimport '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 -
Wrap the parts of the code that you want to log in a
provideLogger
.
show all 41 linesimport '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 PortalIn case of a Flutter app, you can wrap the entire
runApp
method.
show all 39 linesimport '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')),
),
);
}
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 offLevel.shout
is 10Level.fatal
is 20Level.severe
is 40Level.warning
is 80Level.info
is 160Level.verbose
is 320Level.fine
is 500Level.silly
is 640Level.all
is 10000 and turns logging on for all levels