| 1 | package Sentry::Logger; | ||
| 2 | use Mojo::Base -base, -signatures; | ||
| 3 | |||
| 4 | use Exporter qw(import); | ||
| 5 | use List::Util 'any'; | ||
| 6 | |||
| 7 | our @EXPORT_OK = qw(logger); | ||
| 8 | |||
| 9 | has context => 'Sentry'; | ||
| 10 | has active_contexts => sub { [split(/,/, $ENV{DEBUG} // '')] }; | ||
| 11 | |||
| 12 | sub _should_print ($self, $context) { | ||
| 13 | return any { $context =~ $_ } $self->active_contexts->@*; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub _print ($self, $message, $context, $error = 0) { | ||
| 17 | return unless $self->_should_print($context); | ||
| 18 | print { $error ? *STDOUT : *STDERR } qq{[$context] $message\n}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub log ($self, $message, $context = $self->context) { | ||
| 22 | $self->_print($message, $context); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub warn ($self, $message, $context = $self->context) { | ||
| 26 | $self->_print($message, $context, 1); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub error ($self, $message, $context = $self->context) { | ||
| 30 | $self->_print($message, $context, 1); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub enable ($self) { | ||
| 34 | $self->enabled(1); | ||
| 35 | } | ||
| 36 | |||
| 37 | my $Instance; | ||
| 38 | |||
| 39 | sub logger() { | ||
| 40 | $Instance //= Sentry::Logger->new; | ||
| 41 | return $Instance; | ||
| 42 | } | ||
| 43 | |||
| 44 | 1; |