| 1 | package Log::Any::Adapter::LinuxJournal 0.173471; | ||
| 2 | |||
| 3 | # ABSTRACT: Log::Any adapter for the systemd journal on Linux | ||
| 4 | |||
| 5 | 2 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use Linux::Systemd::Journal::Write 1.172760; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Log::Any::Adapter::Util 1.700; | ||
| 10 | use base 'Log::Any::Adapter::Base'; | ||
| 11 | |||
| 12 | sub init { | ||
| 13 | my $self = shift; | ||
| 14 | $self->{jnl} = Linux::Systemd::Journal::Write->new(@_, caller_level => 2); | ||
| 15 | return; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub structured { | ||
| 19 | my ($self, $level, $category, @args) = @_; | ||
| 20 | |||
| 21 | my %details = ( | ||
| 22 | PRIORITY => $level, | ||
| 23 | CATEGORY => $category, | ||
| 24 | ); | ||
| 25 | |||
| 26 | my @msg; | ||
| 27 | while (my $arg = shift @args) { | ||
| 28 | |||
| 29 | # TODO journal can only usefully take k => v, flatten v | ||
| 30 | if (!ref $arg) { | ||
| 31 | push @msg, $arg; | ||
| 32 | } elsif (ref $arg eq 'HASH') { | ||
| 33 | @details{keys %{$arg}} = values %{$arg}; | ||
| 34 | } elsif (ref $arg eq 'ARRAY') { | ||
| 35 | while (my ($k, $v) = (shift @{$arg}, shift @{$arg})) { | ||
| 36 | $details{$k} = $v; | ||
| 37 | } | ||
| 38 | } else { | ||
| 39 | push @msg, Log::Any::Adapter::Util::dump_one_line($arg); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | $self->{jnl}->send(join(' ', @msg), \%details); | ||
| 44 | |||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | # TODO optionally disable debug | ||
| 49 | for my $method (Log::Any::Adapter::Util::detection_methods()) { | ||
| 50 | no strict 'refs'; ## no critic | ||
| 51 | *$method = sub {1}; | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | __END__ | ||