line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Sysadmin::Log::Simple::HTTP; |
2
|
3
|
|
|
3
|
|
3808
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
119
|
|
3
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
149
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: a HTTP (maybe RESTful?) logger for App::Sysadmin::Log::Simple |
5
|
|
|
|
|
|
|
our $VERSION = '0.009'; # VERSION |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
18
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
255
|
|
8
|
3
|
|
|
3
|
|
3763
|
use HTTP::Tiny; |
|
3
|
|
|
|
|
69211
|
|
|
3
|
|
|
|
|
147
|
|
9
|
3
|
|
|
3
|
|
2750
|
use URI::Escape qw(uri_escape); |
|
3
|
|
|
|
|
4177
|
|
|
3
|
|
|
|
|
1722
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $HTTP_TIMEOUT = 10; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
4
|
|
|
4
|
1
|
464
|
my $class = shift; |
16
|
4
|
|
|
|
|
15
|
my %opts = @_; |
17
|
4
|
|
|
|
|
10
|
my $app = $opts{app}; |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
100
|
|
|
35
|
$app->{http}->{uri} ||= 'http://localhost'; |
20
|
4
|
|
100
|
|
|
29
|
$app->{http}->{method} ||= 'post'; |
21
|
4
|
|
|
|
|
16
|
$app->{http}->{method} = uc $app->{http}->{method}; |
22
|
|
|
|
|
|
|
|
23
|
4
|
|
|
|
|
37
|
return bless { |
24
|
|
|
|
|
|
|
do_http => $app->{do_http}, |
25
|
|
|
|
|
|
|
http => $app->{http}, |
26
|
|
|
|
|
|
|
user => $app->{user}, |
27
|
|
|
|
|
|
|
}, $class; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub log { |
32
|
2
|
|
|
2
|
1
|
4
|
my $self = shift; |
33
|
2
|
|
|
|
|
5
|
my $logentry = shift; |
34
|
|
|
|
|
|
|
|
35
|
2
|
50
|
|
|
|
15
|
return unless $self->{do_http}; |
36
|
|
|
|
|
|
|
|
37
|
0
|
0
|
|
|
|
|
my $ua = HTTP::Tiny->new( |
38
|
|
|
|
|
|
|
timeout => $HTTP_TIMEOUT, |
39
|
|
|
|
|
|
|
agent => __PACKAGE__ . '/' . (__PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'), |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
my $res = sub { |
42
|
0
|
0
|
|
0
|
|
|
if ( $self->{http}->{method} eq 'GET' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $params = $ua->www_form_urlencode({ |
44
|
|
|
|
|
|
|
user => $self->{user}, |
45
|
|
|
|
|
|
|
log => $logentry, |
46
|
|
|
|
|
|
|
}); |
47
|
0
|
|
|
|
|
|
my $uri = $self->{http}->{uri} . "?$params"; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
return $ua->get($uri); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
elsif ( $self->{http}->{method} eq 'POST' ) { |
52
|
0
|
|
|
|
|
|
return $ua->post_form($self->{http}->{uri}, { |
53
|
|
|
|
|
|
|
user => $self->{user}, |
54
|
|
|
|
|
|
|
log => $logentry, |
55
|
|
|
|
|
|
|
}); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ( $self->{http}->{method} eq 'PUT' ) { |
58
|
0
|
|
|
|
|
|
return $ua->put($self->{http}->{uri}, { |
59
|
|
|
|
|
|
|
user => $self->{user}, |
60
|
|
|
|
|
|
|
log => $logentry, |
61
|
|
|
|
|
|
|
}); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
else { |
64
|
0
|
|
|
|
|
|
croak 'This shouldnt happen, as the method is populated internally. Something bad has happened' |
65
|
|
|
|
|
|
|
} |
66
|
0
|
|
|
|
|
|
}->(); |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
carp sprintf('Failed to http log via %s to %s with code %d and error %s', |
69
|
|
|
|
|
|
|
$self->{http}->{method}, |
70
|
|
|
|
|
|
|
$self->{http}->{uri}, |
71
|
|
|
|
|
|
|
$res->{status}, |
72
|
|
|
|
|
|
|
$res->{reason}, |
73
|
|
|
|
|
|
|
) unless $res->{success}; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
return "Logged to $self->{http}->{uri} via $self->{http}->{method}" |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
__END__ |