1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
package Log::ger::Output::String;
use strict;
use warnings;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2023-12-29'; # DATE
our $DIST = 'Log-ger'; # DIST
our $VERSION = '0.042'; # VERSION
sub meta { +{
v => 2,
} }
sub get_hooks {
my %plugin_conf = @_;
$plugin_conf{string} or die "Please specify string";
my $formatter = $plugin_conf{formatter};
my $append_newline = $plugin_conf{append_newline};
$append_newline = 1 unless defined $append_newline;
return {
create_outputter => [
__PACKAGE__, # key
50, # priority
sub { # hook
my %hook_args = @_; # see Log::ger::Manual::Internals/"Arguments passed to hook"
my $level = $hook_args{level};
my $outputter = sub {
my ($per_target_conf, $msg, $per_msg_conf) = @_;
if ($formatter) {
$msg = $formatter->($msg);
}
${ $plugin_conf{string} } .= $msg;
${ $plugin_conf{string} } .= "\n"
unless !$append_newline || $msg =~ /\R\z/;
};
[$outputter];
}],
};
}
1;
# ABSTRACT: Set output to a string
__END__
=pod
=encoding UTF-8
=head1 NAME
Log::ger::Output::String - Set output to a string
=head1 VERSION
version 0.042
=head1 SYNOPSIS
BEGIN { our $str }
use Log::ger::Output 'String' => (
string => \$str,
# append_newline => 0, # default is true, to mimic Log::ger::Output::Screen
);
use Log::ger;
log_warn "warn ...";
log_error "debug ...";
C<$str> will contain "warn ...\n".
=head1 DESCRIPTION
For testing only.
=for Pod::Coverage ^(.+)$
=head1 CONFIGURATION
=head2 string => scalarref
Required.
=head2 formatter => coderef
Optional.
=head2 append_newline => bool (default: 1)
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2023, 2022, 2020, 2019, 2018, 2017 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|