Created
June 26, 2022 20:29
-
-
Save gfldex/b9a0c2292ad6464fbcbff3e27c8d8bac to your computer and use it in GitHub Desktop.
This file contains 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
use Log; | |
dd ERROR('foo') ~~ LogLevel::ERROR; | |
dd ERROR('foo') ~~ LogLevel::DEBUG; | |
say ERROR('foo').&{ .file, .line }; | |
ERROR 'foo'; | |
VERBOSE 'Detailed, very much so indeed.'; | |
my $*LOGLEVEL = 2; | |
VERBOSE 'Detailed, very much so indeed.'; | |
This file contains 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
use v6.d; | |
my role Functor { | |
has $.message; | |
my $.level; | |
has $.captured-loglevel; | |
has $.file; | |
has $.line; | |
method CALL-ME(*@message) { | |
my ($line, $file) = callframe(1).&{ .line, .file }; | |
self.new: message => "$file:$line " ~ @message.join($?NL), :captured-loglevel($*LOGLEVEL // 0), :$line, :$file; | |
} | |
method sink { $*ERR.put(self.message) if ($!captured-loglevel // 0) ≥ $.level } | |
method Str(::?CLASS:D:) { self.message } | |
method gist(::?CLASS:D:) { self.^shortname ~ ' ' ~ self.message } | |
} | |
my $VERBOSE; | |
my $DEBUG; | |
my class ERROR {...} | |
class LogLevel { | |
constant ERROR = ERROR; | |
constant VERBOSE := $VERBOSE; | |
constant DEBUG := $DEBUG; | |
} | |
my class ERROR is LogLevel does Callable does Functor { my $.level = 1; } | |
$VERBOSE = class :: is LogLevel does Callable does Functor { my $.level = 2; } | |
$DEBUG = class :: is LogLevel does Callable does Functor { my $.level = 3; } | |
sub EXPORT { | |
Map.new: '&ERROR' => ERROR, | |
'&VERBOSE' => $VERBOSE, | |
'&DEBUG' => $DEBUG, | |
'LogLevel' => LogLevel, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment