| 1 | package XAS::Lib::Modules::Log::Json; | ||
| 2 | |||
| 3 | 1 | our $VERSION = '0.01'; | |
| 4 | |||
| 5 | 1 | ▶ | use XAS::Factory; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Params::Validate 'HASHREF'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | use XAS::Class | ||
| 9 | 1 | version => $VERSION, | |
| 10 | base => 'XAS::Base', | ||
| 11 | codecs => 'JSON', | ||
| 12 | accessors => 'spool', | ||
| 13 | filesystem => 'Dir', | ||
| 14 | mixins => 'init_log output destroy', | ||
| 15 | 1 | ▶ | ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | # ---------------------------------------------------------------------- | ||
| 18 | # Public Methods | ||
| 19 | # ---------------------------------------------------------------------- | ||
| 20 | |||
| 21 | sub output { | ||
| 22 | 0 | ▶ | my $self = shift; |
|
Subroutine
output: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 23 | 0 | my ($args) = $self->validate_params(\@_, [ | |
| 24 | { type => HASHREF } | ||
| 25 | ]); | ||
| 26 | |||
| 27 | my $message = sprintf('[%s] %-5s - %s', | ||
| 28 | $args->{datetime}->strftime('%Y-%m-%d %H:%M:%S'), | ||
| 29 | uc($args->{priority}), | ||
| 30 | $args->{message} | ||
| 31 | 0 | ); | |
| 32 | |||
| 33 | # create a logstash "json_event" | ||
| 34 | |||
| 35 | my $data = { | ||
| 36 | '@timestamp' => $args->{datetime}->strftime('%Y-%m-%dT%H:%M:%S.%3N%z'), | ||
| 37 | '@version' => '1', | ||
| 38 | '@message' => $message, | ||
| 39 | type => 'xas-logs', | ||
| 40 | message => $args->{message}, | ||
| 41 | hostname => $args->{hostname}, | ||
| 42 | priority => $args->{priority}, | ||
| 43 | facility => $args->{facility}, | ||
| 44 | process => $args->{process}, | ||
| 45 | pid => $args->{pid} | ||
| 46 | 0 | }; | |
| 47 | |||
| 48 | # write the spool file | ||
| 49 | |||
| 50 | 0 | $self->spool->write(encode($data)); | |
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | sub destroy { | ||
| 55 | 0 | ▶ | my $self = shift; |
|
Subroutine
destroy: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | # ---------------------------------------------------------------------- | ||
| 60 | # Private Methods | ||
| 61 | # ---------------------------------------------------------------------- | ||
| 62 | |||
| 63 | sub init_log { | ||
| 64 | 0 | ▶ | my $self = shift; |
|
Subroutine
init_log: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 65 | |||
| 66 | 0 | $self->{spool} = XAS::Factory->module('spool', { | |
| 67 | -directory => Dir($self->env->spool, 'logs') | ||
| 68 | }); | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | 1 | 1; | |
| 73 | |||
| 74 | __END__ | ||
| 75 | |||
| 76 | =head1 NAME | ||
| 77 | |||
| 78 | XAS::Lib::Modules::Log::JSON - A mixin class for logging | ||
| 79 | |||
| 80 | =head1 DESCRIPTION | ||
| 81 | |||
| 82 | This module is a mixin for logging. It creates JSON output in the logstash | ||
| 83 | "json_event" format which is then logged to the logs spool directory. | ||
| 84 | |||
| 85 | =head1 METHODS | ||
| 86 | |||
| 87 | =head2 init_log | ||
| 88 | |||
| 89 | This method initializes the module. It creates a spool object for writing | ||
| 90 | the "json_event". | ||
| 91 | |||
| 92 | =head2 output($hashref) | ||
| 93 | |||
| 94 | This method formats the hashref and writes out the results. The JSON data | ||
| 95 | structure has the following fields: | ||
| 96 | |||
| 97 | @timestamp - current time in GMT | ||
| 98 | @version - 1 | ||
| 99 | @message - the line that would have gone to a log file | ||
| 100 | type - 'xas-logs', | ||
| 101 | message - the log line | ||
| 102 | hostname - the hostname | ||
| 103 | pid - the pid of the process | ||
| 104 | msgid - message id | ||
| 105 | priority - the priority from -priority | ||
| 106 | facility - the facility from -facility | ||
| 107 | process - the process from -process | ||
| 108 | |||
| 109 | =head2 destroy | ||
| 110 | |||
| 111 | This methods deinitializes the module. | ||
| 112 | |||
| 113 | =head1 SEE ALSO | ||
| 114 | |||
| 115 | =over 4 | ||
| 116 | |||
| 117 | =item L<XAS::Lib::Modules::Log|XAS::Lib::Modules::Log> | ||
| 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 is free software; you can redistribute it and/or modify it under | ||
| 132 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 133 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 134 | |||
| 135 | =cut | ||