| 1 | package Mojo::Log::JSON; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use Mojo::Base 'Mojo::Log'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 4 | our $VERSION = '0.04'; | |||||||||
| 6 | |||||||||||
| 7 | 6 | ▶ | has codec => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 6 | require JSON::MaybeXS; | |||||||||
| 9 | 6 | JSON::MaybeXS->new->indent(0)->utf8->canonical; | |||||||||
| 10 | 4 | }; | |||||||||
| 11 | |||||||||||
| 12 | 4 | has include_level => 1; | |||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | has default_fields => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | ▶ | { datetime => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 5 | my ( $sec, $min, $hour, $mday, $mon, $year ) = gmtime(); | |||||||||
| 18 | 5 | sprintf( | |||||||||
| 19 | "%04d-%02d-%02d %02d:%02d:%02d", | ||||||||||
| 20 | $year + 1900, | ||||||||||
| 21 | $mon + 1, $mday, $hour, $min, $sec | ||||||||||
| 22 | ); | ||||||||||
| 23 | }, | ||||||||||
| 24 | 3 | }; | |||||||||
| 25 | 4 | }; | |||||||||
| 26 | |||||||||||
| 27 | 6 | ▶ | has format => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 6 | my $self = shift; | |||||||||
| 29 | |||||||||||
| 30 | 13 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 31 | 13 | my ( $time, $level, @lines ) = @_; | |||||||||
| 32 | |||||||||||
| 33 | my %msg = ( | ||||||||||
| 34 | ( map { | ||||||||||
| 35 | |||||||||||
| 36 | 20 | my $value = $self->default_fields->{$_}; | |||||||||
| 37 | |||||||||||
| 38 | 20 | ▶ | $_ => ref $value eq 'CODE' ? $value->() : $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 13 | ▶ | } keys %{ $self->default_fields } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | ), | ||||||||||
| 42 | |||||||||||
| 43 | ref $lines[0] # data structure? | ||||||||||
| 44 | 13 | ? %{ $lines[0] } # multiple keys | |||||||||
| 45 | : ( message => join( "\n", @lines ) ), # single 'message' key | ||||||||||
| 46 | |||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 13 | ▶ | $msg{level} = $level if $self->include_level; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 13 | return $self->codec->encode( \%msg ) . "\n"; | |||||||||
| 52 | 6 | }; | |||||||||
| 53 | 4 | }; | |||||||||
| 54 | |||||||||||
| 55 | 4 | 1; | |||||||||
| 56 | |||||||||||
| 57 | __END__ | ||||||||||