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.5'; # VERSION 1.5
5             our $VERSION=1.5;
6 5     5   735040 use strict;
  5         12  
  5         124  
7 5     5   23 use warnings;
  5         10  
  5         152  
8              
9 5     5   2146 use Moose;
  5         1546135  
  5         38  
10 5     5   38060 use namespace::autoclean;
  5         33311  
  5         20  
11              
12 5     5   2329 use JSON::Tiny qw(encode_json);
  5         48865  
  5         298  
13 5     5   779 use Sys::Hostname;
  5         1509  
  5         203  
14 5     5   752 use Data::UUID;
  5         982  
  5         231  
15 5     5   795 use POSIX qw(strftime);
  5         10901  
  5         34  
16              
17 5     5   4457 use Log::Log4perl;
  5         100429  
  5         35  
18              
19 5     5   2411 use Data::DTO::GELF::Types qw( LogLevel );
  5         19  
  5         47  
20 5     5   5794 use Devel::StackTrace;
  5         4871  
  5         725  
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            
86            
87             my $trace = Devel::StackTrace->new;
88             foreach my $frame($trace->frames)
89             {
90             if($frame->{subroutine} eq "Log::Log4perl::Logger::__ANON__")
91             {
92             $self->_line($frame->{line});
93             $self->_file( $frame->{filename});
94             $self->_facility($frame->{package});
95             }
96             }
97              
98              
99              
100             }
101              
102             sub _build_version {
103             my $self = shift;
104             return "$GELF_VERSION";
105             }
106              
107             sub _build_host {
108             my $self = shift;
109             return hostname();
110             }
111              
112             sub _build_timestamp {
113             my $self = shift;
114             return time();
115             }
116              
117             sub message {
118             my $self = shift;
119             my $m = shift;
120             if ( defined $m ) {
121             $self->full_message($m);
122             }
123             else {
124             return $self->full_message();
125             }
126              
127             return;
128             }
129              
130             sub _long_to_short {
131             my $self = shift;
132             my $msg = substr $self->full_message(), 0, 50;
133             $msg =~ s/\n.*//s;
134             return $msg;
135             }
136             sub TO_HASH {
137             my $self = shift;
138             { $self->short_message() } #fire off lazy message builder
139             return {%$self};
140             }
141             sub TO_JSON {
142             my $self = shift;
143             { $self->short_message() } #fire off lazy message builder
144             return {%$self};
145             }
146              
147             1;
148              
149             __END__
150              
151             =pod
152              
153             =encoding UTF-8
154              
155             =head1 NAME
156              
157             Data::DTO::GELF - The DTO object for GELF version 1.1
158              
159             =head1 VERSION
160              
161             version 1.5
162              
163             =head1 AUTHOR
164              
165             Brandon "Dimentox Travanti" Husbands <xotmid@gmail.com>
166              
167             =head1 COPYRIGHT AND LICENSE
168              
169             This software is copyright (c) 2017 by Brandon "Dimentox Travanti" Husbands.
170              
171             This is free software; you can redistribute it and/or modify it under
172             the same terms as the Perl 5 programming language system itself.
173              
174             =cut