Last active
September 26, 2016 13:42
-
-
Save jkramer/9e4431b293e9bf2a03a599e448de502f to your computer and use it in GitHub Desktop.
Log::Dispatch for Perl 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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