File Coverage

blib/lib/XAS/Lib/Log/Json.pm
Criterion Covered Total %
statement 6 16 37.5
branch n/a
condition n/a
subroutine 2 4 50.0
pod 2 2 100.0
total 10 22 45.4


line stmt bran cond sub pod time code
1             package XAS::Lib::Log::Json;
2              
3             our $VERSION = '0.01';
4              
5 1     1   735 use XAS::Factory;
  1         2  
  1         7  
6              
7             use XAS::Class
8 1         4 debug => 0,
9             version => $VERSION,
10             base => 'XAS::Base',
11             utils => ':validation level2syslog',
12             codec => 'JSON',
13             accessors => 'spooler',
14             constants => 'HASHREF',
15             filesystem => 'Dir',
16 1     1   71 ;
  1         1  
17              
18             # note to self: Don't but $self->log->debug() statements in here
19             # it will produce a nice race condition.
20              
21             # ----------------------------------------------------------------------
22             # Public Methods
23             # ----------------------------------------------------------------------
24              
25             sub output {
26 0     0 1   my $self = shift;
27 0           my ($args) = validate_params(\@_, [
28             { type => HASHREF }
29             ]);
30              
31             my $message = sprintf('[%s] %-5s - %s',
32             $args->{'datetime'}->strftime('%Y-%m-%d %H:%M:%S'),
33             uc($args->{'priority'}),
34 0           $args->{'message'}
35             );
36              
37             # create a logstash "json_event"
38              
39             my $data = {
40             '@timestamp' => $args->{'datetime'}->strftime('%Y-%m-%dT%H:%M:%S.%3N%z'),
41             '@version' => '1',
42             '@message' => $message,
43             type => 'xas-logs',
44             message => $args->{'message'},
45             hostname => $args->{'hostname'},
46             priority => level2syslog($args->{'priority'}),
47             facility => $args->{'facility'},
48             process => $args->{'process'},
49 0           pid => $args->{'pid'}
50             };
51              
52 0           my $json = encode($data);
53              
54             # write the spool file
55              
56 0           $self->spooler->write($json);
57              
58             }
59              
60             # ----------------------------------------------------------------------
61             # Private Methods
62             # ----------------------------------------------------------------------
63              
64             sub init {
65 0     0 1   my $class = shift;
66              
67 0           my $self = $class->SUPER::init(@_);
68              
69 0           $self->{'spooler'} = XAS::Factory->module('spool', {
70             -directory => Dir($self->env->spool, 'logs'),
71             -lock => Dir($self->env->spool, 'logs', 'locked')->path,
72             });
73              
74 0           return $self;
75              
76             }
77              
78             1;
79              
80             __END__
81              
82             =head1 NAME
83              
84             XAS::Lib::Log::Json - A class for logging with JSON output
85              
86             =head1 DESCRIPTION
87              
88             This module creates JSON output in the logstash "json_event" format which
89             is then logged to the logs spool directory.
90              
91             =head1 METHODS
92              
93             =head2 new
94              
95             This method initializes the module. It creates a spool object for writing
96             the "json_event".
97              
98             =head2 output($hashref)
99              
100             This method formats the hashref and writes out the results. The JSON data
101             structure has the following fields:
102              
103             @timestamp - current time in GMT
104             @version - 1
105             @message - the line that would have gone to a log file
106             type - 'xas-logs',
107             message - the log line
108             hostname - from the environment host name
109             pid - the pid of the process
110             msgid - 0
111             priority - converted from XAS log level to syslog priority
112             facility - from the environment log_facility
113             process - from the environment script name
114              
115             =head1 SEE ALSO
116              
117             =over 4
118              
119             =item L<XAS::Lib::Log|XAS::Lib::Log>
120              
121             =item L<XAS|XAS>
122              
123             =back
124              
125             =head1 AUTHOR
126              
127             Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             Copyright (c) 2012-2015 Kevin L. Esteb
132              
133             This is free software; you can redistribute it and/or modify it under
134             the terms of the Artistic License 2.0. For details, see the full text
135             of the license at http://www.perlfoundation.org/artistic_license_2_0.
136              
137             =cut