Advertisement
Guest User

Untitled

a guest
Dec 25th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.40 KB | None | 0 0
  1. use v6;
  2. unit module JK::Logging;
  3.  
  4. role Handler is export {
  5.     method handle(Str:D $entry) { ... }
  6. }
  7.  
  8. class StdoutHandler does Handler is export {
  9.     method handle(Str:D $entry) {
  10.         say $entry;
  11.     }
  12. }
  13.  
  14. enum Severity <debug info warn error>;
  15.  
  16. role LogEntry is export {
  17.     has Str $.message;
  18. }
  19.  
  20. role Formatter[::LogEntryType] is export {
  21.     method format(Severity $severity, LogEntryType:D $entry) returns Str { ... }
  22. }
  23.  
  24. class DefaultFormatter does Formatter[LogEntry] is export {
  25.     method format(Severity $severity, LogEntry:D $entry) returns Str {
  26.         return "$severity.uc(): $entry";
  27.     }
  28. }
  29.  
  30. role Logger[::LogEntryType] is export {
  31.     method debug(LogEntryType:D $entry) { ... }
  32.     method info(LogEntryType:D $entry) { ... }
  33.     method warn(LogEntryType:D $entry) { ... }
  34.     method err(LogEntryType:D $entry) { ... }
  35. }
  36.  
  37.  
  38. role DefaultLogger[LogEntry:U ::LogEntryType] does Logger[LogEntryType] is export {
  39.     has Formatter[LogEntryType] $.formatter is rw;
  40.     has Handler @handlers = StdoutHandler.new();
  41.  
  42.     submethod do-log(Severity $severity, LogEntryType:D $entry) {
  43.         say $!formatter;
  44.         my Str $formatted-entry = $!formatter.format($severity, $entry);
  45.         for @handlers {
  46.             $_.handle($formatted-entry);
  47.         }
  48.     }
  49.  
  50.     method debug(LogEntryType:D $entry) {
  51.         self.do-log(Severity::debug, $entry);
  52.     }
  53.  
  54.     method info(LogEntryType:D $entry) {
  55.         self.do-log(Severity::info, $entry);
  56.     }
  57.  
  58.     method warn(LogEntryType:D $entry) {
  59.         self.do-log(Severity::warn, $entry);
  60.     }
  61.  
  62.     method err(LogEntryType:D $entry) {
  63.         self.do-log(Severity::error, $entry);
  64.     }
  65. }
  66.  
  67. my Logger %logger-registry{LogEntry};
  68. %logger-registry{LogEntry} = DefaultLogger[LogEntry].new(:formatter(DefaultFormatter.new()));
  69.  
  70. sub registerLogger(LogEntry:U ::LogEntryType $logEntryType, Logger[LogEntryType] $logger) is export {
  71.     unless %logger-registry{$logEntryType}:exists {
  72.         %logger-registry{$logEntryType} = $logger;
  73.     }
  74. }
  75.  
  76. sub getLogger(Str $name, LogEntry:U $logEntryType=LogEntry) returns Logger[$logEntryType] is export {
  77.     given %logger-registry{$logEntryType} {
  78.         when $_ { $_.new(); }
  79.         default { die "Cannot get logger for unknown LogEntry type $logEntryType" }
  80.     }
  81. }
  82.  
  83. my $logger = getLogger('anything');
  84. my $entry = LogEntry.new(message => 'hello, logger');
  85. $logger.info($entry);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement