line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Dispatch::Slack; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
1867
|
use strict; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
57
|
|
6
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
89
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.0006'; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
167
|
|
11
|
2
|
|
|
2
|
|
1179
|
use Encode qw(encode_utf8); |
|
2
|
|
|
|
|
22830
|
|
|
2
|
|
|
|
|
153
|
|
12
|
2
|
|
|
2
|
|
944
|
use WebService::Slack::WebApi; |
|
2
|
|
|
|
|
179885
|
|
|
2
|
|
|
|
|
80
|
|
13
|
2
|
|
|
2
|
|
1058
|
use Log::Dispatch::Output; |
|
2
|
|
|
|
|
565975
|
|
|
2
|
|
|
|
|
93
|
|
14
|
2
|
|
|
2
|
|
21
|
use Try::Tiny; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
142
|
|
15
|
2
|
|
|
2
|
|
14
|
use JSON::XS qw/decode_json/; |
|
2
|
|
|
|
|
21
|
|
|
2
|
|
|
|
|
136
|
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
11
|
use base qw( Log::Dispatch::Output ); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
208
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
1307
|
use Params::Validate qw(validate SCALAR BOOLEAN); |
|
2
|
|
|
|
|
6009
|
|
|
2
|
|
|
|
|
1331
|
|
20
|
|
|
|
|
|
|
Params::Validate::validation_options( allow_extra => 1 ); |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
0
|
0
|
0
|
sub APPEND {0} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub new { |
25
|
4
|
|
|
4
|
0
|
45157
|
my $proto = shift; |
26
|
4
|
|
33
|
|
|
40
|
my $class = ref $proto || $proto; |
27
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
23
|
my %p = @_; |
29
|
|
|
|
|
|
|
|
30
|
4
|
|
|
|
|
11
|
my $self = bless {}, $class; |
31
|
|
|
|
|
|
|
|
32
|
4
|
|
|
|
|
47
|
$self->_basic_init(%p); |
33
|
4
|
|
|
|
|
17
|
$self->_make_handle; |
34
|
|
|
|
|
|
|
|
35
|
4
|
|
|
|
|
165
|
return $self; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _basic_init { |
39
|
4
|
|
|
4
|
|
12
|
my $self = shift; |
40
|
|
|
|
|
|
|
|
41
|
4
|
|
|
|
|
32
|
$self->SUPER::_basic_init(@_); |
42
|
|
|
|
|
|
|
|
43
|
4
|
|
|
|
|
640
|
my %p = validate( |
44
|
|
|
|
|
|
|
@_, { |
45
|
|
|
|
|
|
|
token => { type => SCALAR }, |
46
|
|
|
|
|
|
|
channel => { type => SCALAR }, |
47
|
|
|
|
|
|
|
icon => { type => SCALAR, optional => 1 }, |
48
|
|
|
|
|
|
|
username => { type => SCALAR, optional => 1 }, |
49
|
|
|
|
|
|
|
die_on_error => { type => BOOLEAN, optional => 1, default => 1 }, |
50
|
|
|
|
|
|
|
utf8 => { type => BOOLEAN, optional => 1 }, |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
4
|
|
|
|
|
40
|
$self->{channel} = $p{channel}; |
55
|
4
|
|
|
|
|
10
|
$self->{token} = $p{token}; |
56
|
4
|
|
|
|
|
7
|
$self->{username} = $p{username}; |
57
|
4
|
|
|
|
|
12
|
$self->{icon} = $p{icon}; |
58
|
4
|
|
|
|
|
5
|
$self->{die_on_error} = $p{die_on_error}; |
59
|
4
|
|
|
|
|
8
|
$self->{utf8} = $p{utf8}; |
60
|
|
|
|
|
|
|
|
61
|
4
|
|
|
|
|
10
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _make_handle { |
65
|
2
|
|
|
2
|
|
4
|
my $self = shift; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
try { |
68
|
|
|
|
|
|
|
$self->{client} = WebService::Slack::WebApi->new( |
69
|
|
|
|
|
|
|
token => $self->{token}, |
70
|
2
|
|
|
2
|
|
116
|
); |
71
|
|
|
|
|
|
|
} catch { |
72
|
0
|
|
|
0
|
|
0
|
my $msg = "Error initialising Webservice::Slack::WebApi: $_"; |
73
|
0
|
0
|
|
|
|
0
|
if ($self->{die_on_error}) { |
74
|
0
|
|
|
|
|
0
|
croak $msg; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
|
|
|
|
0
|
carp $msg; |
77
|
2
|
|
|
|
|
18
|
}; |
78
|
2
|
|
|
|
|
46
|
return; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub log_message { |
82
|
5
|
|
|
5
|
0
|
455
|
my $self = shift; |
83
|
5
|
|
|
|
|
16
|
my %p = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my %post_params = ( |
86
|
|
|
|
|
|
|
text => $self->{utf8} ? encode_utf8($p{message}) : $p{message}, |
87
|
|
|
|
|
|
|
channel => $p{channel} || $self->{channel}, |
88
|
5
|
100
|
33
|
|
|
39
|
); |
89
|
5
|
50
|
|
|
|
49
|
if( $p{icon} ){ |
|
|
100
|
|
|
|
|
|
90
|
0
|
|
|
|
|
0
|
$post_params{icon_url} = $p{icon}; |
91
|
|
|
|
|
|
|
}elsif( $self->{icon} ){ |
92
|
2
|
|
|
|
|
6
|
$post_params{icon_url} = $self->{icon}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
5
|
50
|
|
|
|
20
|
if( $p{username} ){ |
|
|
100
|
|
|
|
|
|
96
|
0
|
|
|
|
|
0
|
$post_params{username} = $p{username}; |
97
|
|
|
|
|
|
|
}elsif( $self->{username} ){ |
98
|
2
|
|
|
|
|
47
|
$post_params{username} = $self->{username}; |
99
|
|
|
|
|
|
|
}else{ |
100
|
3
|
|
|
|
|
6
|
$post_params{as_user} = 1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
5
|
|
|
|
|
40
|
my $response = $self->{client}->chat->post_message( %post_params ); |
104
|
|
|
|
|
|
|
|
105
|
5
|
100
|
|
|
|
2652
|
if( ! $response->{ok} ){ |
106
|
|
|
|
|
|
|
|
107
|
2
|
|
|
|
|
68
|
my $msg = sprintf( "Failed to send message to channel (%s): %s", $self->{channel}, $response->{error} ); |
108
|
2
|
100
|
|
|
|
10
|
if ($self->{die_on_error}) { |
109
|
1
|
|
|
|
|
19
|
croak $msg; |
110
|
|
|
|
|
|
|
} |
111
|
1
|
|
|
|
|
11
|
carp $msg; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
4
|
|
|
|
|
530
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 NAME |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Log::Dispatch::Slack |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Send log messages to Slack |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SYNOPSIS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
log4perl.appender.hipchat=Log::Dispatch::Slack |
131
|
|
|
|
|
|
|
log4perl.appender.hipchat.auth_token=your-auth-token |
132
|
|
|
|
|
|
|
log4perl.appender.hipchat.channel=channel-to-talk-to |
133
|
|
|
|
|
|
|
log4perl.appender.hipchat.die_on_error=1 |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
log4perl.appender.hipchat.username=set-to-something-if-using-app-token |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 COPYRIGHT |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Copyright 2016, Robin Clarke |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 AUTHOR |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Robin Clarke <robin@robinclarke.net> |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Bartlomiej Fulanty <starlight@cpan.org>, Hurra.com |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|