Dart on AppEngine logging on local dev server



While logging works fine on AppEngine I could not manage to have print or any logging when using local development target. It turns out that stderr output is working fine so the solution I ended up with was to create a logger handler (when running locally) that output logging output on to the console. Below is the simplest hello world app that do log fine both locally and on the server

import 'dart:io';
import 'package:appengine/appengine.dart';
import 'package:logging/logging.dart';
import 'package:args/args.dart';

main(List args) async {
  Logger _log = new Logger("app");
  ArgParser parser = new ArgParser();
  parser.addOption("port", abbr: "p", defaultsTo: "8080");
  parser.addFlag("help", abbr: "h");

  ArgResults results = parser.parse(args);
  if (results["help"]) {
    print(parser.usage);
    exit(0);
  }

  // On dev server we have GAE_PARTITION="dev"
  // Redirect local logging to stderr
  bool devServer = Platform.environment['GAE_PARTITION'] == 'dev';
  if (devServer) {
    // redirect logs to stderr
    hierarchicalLoggingEnabled = true;
    Logger.root.onRecord.listen((LogRecord logRecord) {
      stderr.writeln(
          "${logRecord.time} ${logRecord.loggerName} ${logRecord.level} ${logRecord.message}");
    });
  } else {
    // Make Logging calls redirect to appengine logging
    useLoggingPackageAdaptor();
  }

  int port = int.parse(results["port"]);

  _log.info("Listening on $port");
  await runAppEngine((HttpRequest request) {
    _log.info("Main log ${request.headers}");
    request.response
      ..write('Log Dart Hello, world!')
      ..close();
  }, port: port);
}

Popular posts from this blog

Fixing "DerInputStream.getLength(): lengthTag=109, too big" in Android Studio

Simple Samba setup on Lubuntu

Solving: insufficient permissions for device: user in plugdev group; are your udev rules wrong