File Coverage

blib/lib/Data/DTO/GELF.pm
Criterion Covered Total %
statement 33 33 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod n/a
total 44 44 100.0


line stmt bran cond sub pod time code
1             package Data::DTO::GELF;
2              
3             # ABSTRACT: The DTO object for GELF version 1.1
4             our $VERSION = '1.6'; # VERSION 1.6
5             our $VERSION = 1.6;
6 5     5   707002 use strict;
  5         13  
  5         137  
7 5     5   27 use warnings;
  5         12  
  5         132  
8              
9 5     5   2134 use Moose;
  5         1542297  
  5         34  
10 5     5   36990 use namespace::autoclean;
  5         31883  
  5         22  
11              
12 5     5   2257 use JSON::Tiny qw(encode_json);
  5         49089  
  5         302  
13 5     5   758 use Sys::Hostname;
  5         1537  
  5         228  
14 5     5   715 use Data::UUID;
  5         963  
  5         238  
15 5     5   799 use POSIX qw(strftime);
  5         10518  
  5         34  
16              
17 5     5   4354 use Log::Log4perl;
  5         99352  
  5         40  
18              
19 5     5   2416 use Data::DTO::GELF::Types qw( LogLevel );
  5         20  
  5         46  
20 5     5   5565 use Devel::StackTrace;
  5         5029  
  5         684  
21              
22             our $GELF_VERSION = 1.1;
23              
24             has 'version' => (
25             is => 'ro',
26             isa => 'Str',
27             builder => '_build_version',
28             );
29              
30             has 'host' => (
31             is => 'rw',
32             isa => 'Str',
33             builder => '_build_host',
34             );
35              
36             has 'short_message' => (
37             is => 'rw',
38             isa => 'Str',
39             lazy => 1,
40             builder => '_long_to_short'
41             );
42              
43             has 'full_message' => (
44             is => 'rw',
45             isa => 'Str',
46             );
47              
48             has 'timestamp' => (
49             is => 'ro',
50             isa => 'Int',
51             builder => '_build_timestamp',
52             );
53              
54             has 'level' => (
55             is => 'ro',
56             isa => LogLevel,
57             coerce => 1,
58             );
59             has '_facility' => (
60             is => 'rw',
61             isa => 'Str',
62             );
63              
64             has '_line' => (
65             is => 'rw',
66             isa => 'Int',
67             );
68              
69             has '_file' => (
70             is => 'rw',
71             isa => 'Str',
72             );
73              
74             sub BUILD {
75             my $self = shift;
76             my $args = shift;
77             foreach my $key1 ( keys $args ) {
78             if ( ( substr $key1, 0, 1 ) eq "_" ) {
79             $self->meta->add_attribute( "$key1" => ( accessor => $key1 ) );
80             $self->meta->get_attribute($key1)
81             ->set_value( $self, $args->{$key1} );
82             }
83             }
84              
85             my $trace = Devel::StackTrace->new;
86             foreach my $frame ( $trace->frames ) {
87             if ( $frame->{subroutine} eq "Log::Log4perl::Logger::__ANON__" ) {
88             $self->_line( $frame->{line} );
89             $self->_file( $frame->{filename} );
90             $self->_facility( $frame->{package} );
91             }
92             }
93              
94             }
95              
96             sub _build_version {
97             my $self = shift;
98             return "$GELF_VERSION";
99             }
100              
101             sub _build_host {
102             my $self = shift;
103             return hostname();
104             }
105              
106             sub _build_timestamp {
107             my $self = shift;
108             return time();
109             }
110              
111             sub message {
112             my $self = shift;
113             my $m = shift;
114             if ( defined $m ) {
115             $self->full_message($m);
116             }
117             else {
118             return $self->full_message();
119             }
120              
121             return;
122             }
123              
124             sub _long_to_short {
125             my $self = shift;
126             my $msg = $self->full_message();
127             $msg =~ s/\n//sg;
128             $msg =~ s/\s\s//sg;
129             $msg = substr $msg, 0, 100;
130             return $msg;
131             }
132              
133             sub TO_HASH {
134             my $self = shift;
135             { $self->short_message() } #fire off lazy message builder
136             return {%$self};
137             }
138              
139             sub TO_JSON {
140             my $self = shift;
141             { $self->short_message() } #fire off lazy message builder
142             return {%$self};
143             }
144              
145             1;
146              
147             __END__
148              
149             =pod
150              
151             =encoding UTF-8
152              
153             =head1 NAME
154              
155             Data::DTO::GELF - The DTO object for GELF version 1.1
156              
157             =head1 VERSION
158              
159             version 1.6
160              
161             =head1 AUTHOR
162              
163             Brandon "Dimentox Travanti" Husbands <xotmid@gmail.com>
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is copyright (c) 2017 by Brandon "Dimentox Travanti" Husbands.
168              
169             This is free software; you can redistribute it and/or modify it under
170             the same terms as the Perl 5 programming language system itself.
171              
172             =cut