Dart Logging Hello World Example

Dart is a new programming language being developed at Google. It has the advantage of running in a native virtual machine on the server, but cross compiling to javascript on the client. I decided to check out the most basic feature of any language: logging.

First, I installed the editor/SDK bundle. Installing this as root in /usr/local/dart did not work. The editor at least wants to write into the dart directories. Clearly the packaging is pretty immature. After reinstalling in my home directory as me, the editor worked.

I then started hacking the hello world program in Geany. The interpreter runs fine with a shebang line at the top of the script. Hello, world! was soon being output to the command window.

When I tried to convert to using the logging package (which is a fairly new addition to the SDK), I ran into trouble importing it. A question on Stack Overflow, and an answer from Justin Fagnani, and I had a solution. I copied the pubspec.yaml suggested by Justin, which included a dependency on the logging package, and edited it for my test package, ran the pub install command from the dart SDK, and it brought over the logging and unittest packages. Here’s the content of pubspec.yaml:

name: trace
description: Tracing library
dependencies:
  logging: { sdk: logging }

I could now run my logging hello world, but got no output and no sign of a log file. Delving into the documentation, I figured out how to add a log handler and print the log message. Here’s the finished program:

#!/home/jim/dart/dart-sdk/bin/dart

#import('package:logging/logging.dart');

Logger log = new Logger('');
void logHandler(LogRecord record) {
    print(record.message);
}

void
main() {
    log.on.record.add(logHandler);
    log.warning("Hello, world!\n");
}
Advertisements

About jimbelton

I'm a software developer, and a writer of both fiction and non-fiction, and I blog about movies, books, and philosophy. My interest in religious philosophy and the search for the truth inspires much of my writing.
This entry was posted in dart, programming and tagged , , , . Bookmark the permalink.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s