| 1 | package Log::Log4perl::Layout::LTSV; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008_001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use Encode; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use Log::Log4perl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use base qw(Log::Log4perl::Layout::PatternLayout); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 3 | $Log::Log4perl::ALLOW_CODE_IN_CONFIG_FILE = 1; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | Log::Log4perl::Layout::LTSV - Log4perl for LTSV | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | Version 1.0 | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 3 | our $VERSION = '1.0'; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | Log4perl implementation of LTSV. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head1 CONFIGURATION SAMPLE | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | use Log::Log4perl | ||||||||||||||||||||
| 32 | my $logger_conf = { | ||||||||||||||||||||
| 33 | 'log4perl.logger.test' => 'DEBUG, SERVER', | ||||||||||||||||||||
| 34 | 'log4perl.appender.SERVER' => 'Log::Log4perl::Appender::Socket', | ||||||||||||||||||||
| 35 | 'log4perl.appender.SERVER.PeerAddr' => '10.1.2.3', | ||||||||||||||||||||
| 36 | 'log4perl.appender.SERVER.PeerPort' => '514', | ||||||||||||||||||||
| 37 | 'log4perl.appender.SERVER.Proto' => 'tcp', | ||||||||||||||||||||
| 38 | 'log4perl.appender.SERVER.layout' => 'LTSV', | ||||||||||||||||||||
| 39 | 'log4perl.appender.SERVER.layout.facility' => 'Custom facility' | ||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||
| 41 | Log::Log4perl->init($logger_conf); | ||||||||||||||||||||
| 42 | my $LOGGER = Log::Log4perl->get_logger('test'); | ||||||||||||||||||||
| 43 | $LOGGER->debug('Debug log'); | ||||||||||||||||||||
| 44 | ... | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head2 new | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Can take most of options that Log::Log4perl::Layout::PatternLayout can. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 57 | 2 | my $class = shift; | |||||||||||||||||||
| 58 | 2 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 59 | 2 | ▶ | my $options = ref $_[0] eq 'HASH' ? shift : {}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 2 | my $record = { | |||||||||||||||||||
| 61 | 'time' => '%Z', | ||||||||||||||||||||
| 62 | 'host' => '%H', | ||||||||||||||||||||
| 63 | 'message' => '%m{chomp}', | ||||||||||||||||||||
| 64 | 'level' => '%Y', | ||||||||||||||||||||
| 65 | 'facility' => '%M', | ||||||||||||||||||||
| 66 | 'file' => '%F', | ||||||||||||||||||||
| 67 | 'line' => '%L', | ||||||||||||||||||||
| 68 | 'pid' => '%P', | ||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||
| 70 | 2 | ▶ | while ( my ($key) = each %{ $options->{field} } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | $record->{$key} = $options->{field}->{$key}->{value}; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 2 | my $conversion_pattern = _encode_ltsv($record); | |||||||||||||||||||
| 74 | 2 | $options->{ConversionPattern} = { value => $conversion_pattern }; | |||||||||||||||||||
| 75 | $options->{cspec} = { | ||||||||||||||||||||
| 76 | 'Y' => { value => \&_level_converter }, | ||||||||||||||||||||
| 77 | 'Z' => { | ||||||||||||||||||||
| 78 | 5 | ▶ | value => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 5 | return strftime( '[%Y-%m-%dT%H:%M:%SZ]', gmtime( time() ) ); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | 2 | }; | |||||||||||||||||||
| 83 | 2 | return $class->SUPER::new($options); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | ▶ | sub _encode_ltsv { | ||||||||||||||||||
|
Subroutine
_encode_ltsv: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 87 | 2 | my $hash = shift; | |||||||||||||||||||
| 88 | 2 | my @res; | |||||||||||||||||||
| 89 | 2 | ▶ | while ( my ($key, $value) = each %$hash ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 16 | $value =~ s/[\r\n\t]/ /g; | |||||||||||||||||||
| 91 | 16 | ▶ | if ( not Encode::is_utf8( $value, 1 ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 16 | $value = Encode::encode( 'UTF-8', $value, Encode::FB_CROAK ); | |||||||||||||||||||
| 93 | 16 | Encode::_utf8_on($value); | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | 16 | push( @res, join( ':', $key, $value ) ); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | 2 | return join( "\t", @res ); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 5 | ▶ | sub _level_converter { | ||||||||||||||||||
|
Subroutine
_level_converter: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 101 | 5 | my ( $layout, $message, $category, $priority, $caller_level ) = @_; | |||||||||||||||||||
| 102 | 5 | my $levels = { | |||||||||||||||||||
| 103 | 'FATAL' => 2, | ||||||||||||||||||||
| 104 | 'ERROR' => 3, | ||||||||||||||||||||
| 105 | 'WARN' => 4, | ||||||||||||||||||||
| 106 | 'NOTICE' => 5, | ||||||||||||||||||||
| 107 | 'INFO' => 6, | ||||||||||||||||||||
| 108 | 'DEBUG' => 7 | ||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||
| 110 | 5 | return $levels->{$priority}; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =head2 render | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Wraps the Log::Log4perl::Layout::PatternLayout return value | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 5 | ▶ | sub render { | ||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 120 | 5 | my ( $self, $message, $category, $priority, $caller_level ) = @_; | |||||||||||||||||||
| 121 | 5 | return $self->SUPER::render( $message, $category, $priority, $caller_level ) | |||||||||||||||||||
| 122 | . "\n"; | ||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | 3 | 1; | |||||||||||||||||||