Skip to content

Instantly share code, notes, and snippets.

@jkramer
Last active September 26, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkramer/9e4431b293e9bf2a03a599e448de502f to your computer and use it in GitHub Desktop.
Save jkramer/9e4431b293e9bf2a03a599e448de502f to your computer and use it in GitHub Desktop.
Log::Dispatch for Perl 6
class Log::Dispatch {
enum Level <DEBUG INFO NOTICE WARNING ERROR CRITICAL ALERT EMERGENCY>;
class Output {
has Level $.level = DEBUG;
method format(Level $level, DateTime $ts, @args) {
$ts ~ ' [' ~ $level.lc ~ '] ' ~ @args>>.gist.join(' ')
}
method output(Str $msg) { ... }
}
class Screen is Output {
method output(Str $msg) { $msg.note }
}
has Output @.outputs is rw;
method log(Level $level, *@things) {
my $ts = DateTime.now;
@!outputs>>.&{.output(.format($level, $ts, @things)) if $level >= .level};
}
for Level::.values -> $level {
::?CLASS.^add_method(
$level.lc,
method (*@things) { $.log($level, @things) }
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment