| 1 | package XAS::Lib::Modules::Log::Logstash; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use XAS::Factory; | ||
| 6 | use Params::Validate 'HASHREF'; | ||
| 7 | |||
| 8 | use XAS::Class | ||
| 9 | version => $VERSION, | ||
| 10 | base => 'XAS::Base', | ||
| 11 | codecs => 'JSON', | ||
| 12 | accessors => 'spool', | ||
| 13 | filesystem => 'Dir', | ||
| 14 | mixins => 'init_log output destroy', | ||
| 15 | ; | ||
| 16 | |||
| 17 | # ---------------------------------------------------------------------- | ||
| 18 | # Public Methods | ||
| 19 | # ---------------------------------------------------------------------- | ||
| 20 | |||
| 21 | sub output { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | $self = $self->prototype() unless ref $self; | ||
| 25 | |||
| 26 | my ($args) = $self->validate_params(\@_, [ | ||
| 27 | { type => HASHREF } | ||
| 28 | ]); | ||
| 29 | |||
| 30 | my $message = sprintf('[%s] %-5s - %s', | ||
| 31 | $args->{datetime}->strftime('%Y-%m-%d %H:%M:%S'), | ||
| 32 | uc($args->{priority}), | ||
| 33 | $args->{message} | ||
| 34 | ); | ||
| 35 | |||
| 36 | # create a logstash "json_event" | ||
| 37 | |||
| 38 | my $data = { | ||
| 39 | '@timestamp' => $args->{datetime}->strftime('%Y-%m-%dT%H:%M:%S.%3N%z'), | ||
| 40 | '@version' => '1', | ||
| 41 | '@message' => $message, | ||
| 42 | type => 'xas-log', | ||
| 43 | message => $args->{message}, | ||
| 44 | hostname => $args->{hostname}, | ||
| 45 | priority => $args->{priority}, | ||
| 46 | facility => $args->{facility}, | ||
| 47 | process => $args->{process}, | ||
| 48 | pid => $args->{pid} | ||
| 49 | }; | ||
| 50 | |||
| 51 | # write the spool file | ||
| 52 | |||
| 53 | $self->spool->write(encode($data)); | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | sub destroy { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | # ---------------------------------------------------------------------- | ||
| 63 | # Private Methods | ||
| 64 | # ---------------------------------------------------------------------- | ||
| 65 | |||
| 66 | sub init_log { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | $self->{spool} = XAS::Factory->module('spool', { | ||
| 70 | -directory = Dir($self->env->spool, 'logstash') | ||
| 71 | }); | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | __END__ | ||
| 78 | |||
| 79 | =head1 NAME | ||
| 80 | |||
| 81 | XAS::Lib::Modules::Log::Logstash - A mixin class for logging | ||
| 82 | |||
| 83 | =head1 DESCRIPTION | ||
| 84 | |||
| 85 | This module is a mixin for logging. It creates a logstash "json_event" which | ||
| 86 | is then logged to the logstash spool directory. | ||
| 87 | |||
| 88 | =head1 METHODS | ||
| 89 | |||
| 90 | =head2 init_log | ||
| 91 | |||
| 92 | This method initializes the module. It creates a spool object for writing | ||
| 93 | the "json_event". | ||
| 94 | |||
| 95 | =head2 output($hashref) | ||
| 96 | |||
| 97 | This method formats the hashref and writes out the results. The JSON data | ||
| 98 | structure has the following fields: | ||
| 99 | |||
| 100 | @timestamp - current time in GMT | ||
| 101 | @version - 1 | ||
| 102 | @message - the line that would have gone to a log file | ||
| 103 | message - the log line | ||
| 104 | hostname - the hostname | ||
| 105 | pid - the pid of the process | ||
| 106 | msgid - message id | ||
| 107 | priority - the priority from -priority | ||
| 108 | facility - the facility from -facility | ||
| 109 | process - the process from -process | ||
| 110 | |||
| 111 | =head2 destroy | ||
| 112 | |||
| 113 | This methods deinitializes the module. | ||
| 114 | |||
| 115 | =head1 SEE ALSO | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item L<XAS|XAS> | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =head1 AUTHOR | ||
| 124 | |||
| 125 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 126 | |||
| 127 | =head1 COPYRIGHT AND LICENSE | ||
| 128 | |||
| 129 | Copyright (C) 2014 Kevin L. Esteb | ||
| 130 | |||
| 131 | This library is free software; you can redistribute it and/or modify | ||
| 132 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 133 | at your option, any later version of Perl 5 you may have available. | ||
| 134 | |||
| 135 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 136 | |||
| 137 | =cut |