magillus / flutter-fimber

Fimber is Flutter logger inspired by Timber (logger API for Android)
106 stars 22 forks source link

NetworkTree idea #83

Closed nodinosaur closed 3 years ago

nodinosaur commented 4 years ago

A simple LogTree implementation that sends logs over a network connection (using Netcat) to a terminal window.

Usecase: view logs for an app that is being cold started: i.e. on push notification or app tray/launcher icon press

/// Terminal
/// nc -kvl 5601 | grep TheClassName
class NetworkLoggingTree extends CustomFormatTree {
  NetworkLoggingTree._(this._socket)
      : super(
          useColors: true,
          logFormat: '${CustomFormatTree.levelToken} ${CustomFormatTree.tagToken}: ${CustomFormatTree.messageToken}',
        );
  static Future<NetworkLogging> create(String server, int port) async {
    final socket = await Socket.connect(
      server,
      port,
      timeout: const Duration(seconds: 10),
    );
    return NetworkLogging._(socket);
  }
  final Socket _socket;
  @override
  void printLine(String line, {String level}) {
    super.printLine(line, level: level);
    _socket.writeln(line);
  }
}

Plant your tree:

/// enter your computer's IP address, and the port that comms will be using
Fimber.plantTree(
      await NetworkLoggingTree.create('192.168.86.28', 5601),
);

Usage: First start a terminal window and enter the net connection command. Use grep if you want to watch logs for a particular class Then launch your app.

nc -kvl 5601 | grep TheClassName

This is a rough, the NetworkLoggingTree currently extends CustomFormatTree and has the advantage of showing logs in both Terminal and in your regular IDE Output window. I have not managed to get colours to work yet

Further options for Netcat here: https://www.computerhope.com/unix/nc.htm

magillus commented 4 years ago

The Socket comes from dart:io and this LoggingTree can be implemented on flutter_io package. I am considering adding one using web_socket_channel (https://pub.dev/packages/web_socket_channel) to original fimber to add that functionality of the remote log collection for all (html and io). Also I would rather add new package to not make fimber depend on other packages like that web socket.

magillus commented 4 years ago

Started work with branch: 83.network.log.via.socket

The change is to use UDP and optionally socket with some message caching and reconnection logic. Already added PlantableTree to allow cleaning up resources of the socket when LogTree is removed.

magillus commented 3 years ago

@nodinosaur It is currently in prerelease https://pub.dev/packages/fimber_io/versions/0.6.1-dev