DeLog

codecov Build Status pub style: lint

The declarative logger allows you to create your own records and records handlers.

Usage

To use this logger, you must create your own record and pass the type to the logger. Also, you need to create handlers for this record and pass them to the logger constructor.

/// The record class.
class SimpleStringMessage {
  String message;
  String? description;
  SimpleStringMessage(this.message, this.description);
}

/// The handler that prints all records.
class PrintHandler<T> extends LogHandler<T> {
  @override
  void handle(RecordData data) {
    print(data.record);
  }

  @override
  Future<void> dispose() async {}
}

final log = DeLog<SimpleStringMessage>([PrintHandler()]);

/// log fatal message
log.fatal('fatal');

/// log trace message
log.trace('trace');

This package contains the LogRecord class that you can use as the record. This class has parameters for the log function from the dart:developer package.

Handlers

Handlers need to perform actions on received records, for example, print, store, send them through a network, etc. There are two classes for handlers:

  • LogHandler - The base class that handles records synchronously.
  • QueueLogHandler - The base class that handles records asynchronous in the queue.

You can see the example usage of these handlers in de_log_example.dart.

Dispose

You can dispose of the logger. When you call the dispose method, it disposes of all handlers.

All futures in QueueLogHandler descendants will throw the TerminatedException. It is good practice wrapping your code in the try block with on TerminatedException clause. When you catch the TerminatedException, you know that the logger was disposed of and perform resources cleanup.

class QueueAsyncHandler<T> extends QueueLogHandler<T> {
  @override
  Future<void> handleRecords() async {
    try {
      // Here we get one record from the queue when it is available.
      // You can use other commands from the QueueWorker class.
      // For example:
      // worker.take(4) - take 4 records
      // ignore: unused_local_variable
      final data = await worker.next;
      // handle records

      // Here we call this method again for waiting for new records.
      await handleRecords();
    } on TerminatedException {
      // logger was disposed
    }
  }
}

Libraries

de_log