File Coverage

blib/lib/Log/Dispatch/Slack.pm
Criterion Covered Total %
statement 27 54 50.0
branch 0 4 0.0
condition 0 3 0.0
subroutine 9 14 64.2
pod 0 3 0.0
total 36 78 46.1


line stmt bran cond sub pod time code
1             package Log::Dispatch::Slack;
2              
3             # ABSTRACT: Dispatch log events to Slack
4              
5 1     1   716 use strict;
  1         2  
  1         25  
6 1     1   6 use warnings;
  1         2  
  1         45  
7              
8             our $VERSION = '0.0001';
9              
10 1     1   783 use WebService::Slack::WebApi;
  1         161350  
  1         30  
11 1     1   954 use Log::Dispatch::Output;
  1         10983  
  1         28  
12 1     1   9 use Try::Tiny;
  1         2  
  1         61  
13 1     1   5 use JSON::XS qw/decode_json/;
  1         2  
  1         53  
14              
15 1     1   5 use base qw( Log::Dispatch::Output );
  1         2  
  1         76  
16              
17 1     1   5 use Params::Validate qw(validate SCALAR BOOLEAN);
  1         1  
  1         330  
18             Params::Validate::validation_options( allow_extra => 1 );
19              
20 0     0 0   sub APPEND {0}
21              
22             sub new {
23 0     0 0       my $proto = shift;
24 0   0           my $class = ref $proto || $proto;
25              
26 0               my %p = @_;
27              
28 0               my $self = bless {}, $class;
29              
30 0               $self->_basic_init(%p);
31 0               $self->_make_handle;
32              
33 0               return $self;
34             }
35              
36             sub _basic_init {
37 0     0         my $self = shift;
38              
39 0               $self->SUPER::_basic_init(@_);
40              
41 0               my %p = validate(
42                     @_, {
43                         token => { type => SCALAR },
44                         channel => { type => SCALAR },
45                         user => { type => SCALAR, optional => 1 }
46                     }
47                 );
48              
49 0               $self->{channel} = $p{channel};
50 0               $self->{token} = $p{token};
51 0               $self->{username} = $p{username};
52             }
53              
54             sub _make_handle {
55 0     0         my $self = shift;
56              
57                 $self->{client} = WebService::Slack::WebApi->new(
58                     token => $self->{token},
59 0               );
60             }
61              
62             sub log_message {
63 0     0 0       my $self = shift;
64 0               my %p = @_;
65                 
66                 my %post_params = (
67                     channel => $self->{channel},
68                     text => $p{message},
69 0               );
70 0 0             if( $self->{username} ){
71 0                   $post_params{username} = $self->{username};
72                 }else{
73 0                   $post_params{as_user} = 1;
74                 }
75                 
76 1     1   792     use YAML;
  1         7828  
  1         168  
77 0               print Dump( \%post_params );
78 0               my $response = $self->{client}->chat->post_message( %post_params );
79 0               print Dump( $response );
80                 
81 0 0             if( ! $response->{ok} ){
82 0                   die( sprintf( "Failed to send message to channel (%s): %s", $self->{channel}, $response->{error} ) );
83                 }
84             }
85              
86              
87             1;
88              
89             =head1 NAME
90            
91             Log::Dispatch::Slack
92            
93             =head1 DESCRIPTION
94            
95             Send log messages to Slack
96            
97             =head1 SYNOPSIS
98            
99             log4perl.appender.hipchat=Log::Dispatch::Slack
100             log4perl.appender.hipchat.auth_token=your-auth-token
101             log4perl.appender.hipchat.channel=channel-to-talk-to
102            
103             =head1 COPYRIGHT
104            
105             Copyright 2016, Robin Clarke
106            
107             =head1 AUTHOR
108            
109             Robin Clarke <robin@robinclarke.net>
110            
111