File Coverage

blib/lib/HeliosX/Logger/HiRes.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package HeliosX::Logger::HiRes;
2              
3 1     1   23823 use 5.008;
  1         5  
  1         50  
4 1     1   6 use strict;
  1         2  
  1         50  
5 1     1   7 use warnings;
  1         11  
  1         37  
6 1     1   458 use parent 'Helios::Logger';
  1         251  
  1         5  
7             use constant MAX_RETRIES => 3;
8             use constant RETRY_INTERVAL => 5;
9             use Time::HiRes 'time';
10              
11             use Helios::LogEntry::Levels ':all';
12             use Helios::Error::LoggingError;
13             use HeliosX::Logger::HiRes::LogEntry;
14              
15             our $VERSION = '0.10_2751';
16              
17             =head1 NAME
18              
19             HeliosX::Logger::HiRes - enhanced, high-resolution logging for Helios applications
20              
21             =head1 SYNOPSIS
22              
23             # in a helios.ini file:
24             [MyService]
25             loggers=HeliosX::Logger::HiRes
26             internal_logger=off
27              
28             --OR--
29            
30             # using helios_config_set
31             helios_config_set -s MyService -H="*" -p loggers -v HeliosX::Logger::HiRes
32             helios_config_set -s MyService -H="*" -p internal_logger -v off
33              
34             # then, use heliosx_logger_hires_search to search the log
35             heliosx_logger_hires_search --service=MyService
36              
37              
38             =head1 DESCRIPTION
39              
40             HeliosX::Logger::HiRes is a Helios::Logger logging class that provides logging
41             with high-resolution timestamp precision with a more normalized database
42             structure. It also provides L, a command to
43             view and search for log messages at the command line.
44              
45             =head1 CONFIGURATION
46              
47             HeliosX::Logger::HiRes must be added to your service using the B
48             directive either using the B command or in B.
49              
50             Additionally, as HeliosX::Logger::HiRes is largely intended to replace the
51             Helios internal logger, once you are sure it is working properly in your
52             installation you should turn off the Helios default logger using the
53             B option.
54              
55             See the L page for complete information about the
56             B and B directives.
57              
58             HeliosX::Logger::HiRes itself can be configured using the options below:
59              
60             =over 4
61              
62             =item * log_priority_threshold
63              
64             Unlike L and L,
65             HeliosX::Logger::HiRes supports the Helios internal logger's
66             B option to limit the messages actually being logged
67             to a certain level. Unlike the others, HeliosX::Logger::HiRes is intended to
68             replace rather than augment the Helios internal logger, so most users running
69             HeliosX::Logger::HiRes will most likely turn off the Helios internal
70             logger. Rather than create confusion with a separate threshold option,
71             HeliosX::Logger::HiRes honors the internal logger's built-in
72             B option.
73              
74             The B value should be an integer matching one of the
75             Helios logging priorities in L:
76              
77             Priority Name Integer Value
78             LOG_EMERG 0
79             LOG_ALERT 1
80             LOG_CRIT 2
81             LOG_ERR 3
82             LOG_WARNING 4
83             LOG_NOTICE 5
84             LOG_INFO 6
85             LOG_DEBUG 7
86              
87             Examples:
88              
89             # in helios.ini
90             # for all services on this host, log everything but debug messages
91             [global]
92             log_priority_threshold=6
93              
94             # at the command line, set all instances of MyService
95             # to only log warnings and worse
96             helios_config_set -s MyService -H="*" -p log_priority_threshold -v 4
97            
98             =back
99              
100             =head1 IMPLEMENTED METHODS
101              
102             =head2 init()
103              
104             HeliosX::Logger::HiRes->init() is empty.
105              
106             =cut
107              
108             sub init { }
109              
110             =head2 logMsg($job, $priority, $message)
111              
112             The logMsg() method takes a job, priority, and log message and savesthe message
113             to the high-resolution log table in the Helios collective database.
114              
115             The job parameter should be a Helios::Job object. If the job value is
116             undefined, no jobid is saved with the message.
117              
118             If the priority parameter is undefined, logMsg() defaults the message's
119             priority to 6 (LOG_INFO).
120              
121             =cut
122              
123             sub logMsg {
124             my $self = shift;
125             unless (scalar @_ == 3) { Helios::Error::LoggingError->throw(__PACKAGE__."->logMsg() ERROR: logMsg() requires 3 arguments: \$job, \$priority, \$message."); }
126             my ($job, $priority, $message) = @_;
127              
128             # deal with the log priority & threshold (if set)
129             $priority = defined($priority) ? $priority : LOG_INFO;
130             my $threshold = defined($self->getConfig()->{log_priority_threshold}) ? $self->getConfig()->{log_priority_threshold} : LOG_DEBUG;
131             if ($priority > $threshold) {
132             return 1;
133             }
134            
135             my $success = 0;
136             my $retries = 0;
137             my $err;
138              
139             # deal with jobid & jobtypeid
140             my $jobid = defined($job) ? $job->getJobid() : undef;
141             my $jobtypeid = defined($job) ? $job->getJobtypeid() : undef;
142            
143             do {
144             eval {
145              
146             my $drvr = $self->getDriver();
147             my $obj = HeliosX::Logger::HiRes::LogEntry->new(
148             log_time => sprintf("%.6f", time()),
149             host => $self->getHostname(),
150             pid => $$,
151             jobid => $jobid,
152             jobtypeid => $jobtypeid,
153             service => $self->getService(),
154             priority => $priority,
155             message => $message,
156             );
157             $drvr->insert($obj);
158             1;
159             };
160             if ($@) {
161             $err = $@;
162             $retries++;
163             sleep RETRY_INTERVAL;
164             } else {
165             # no exception? then declare success and move on
166             $success = 1;
167             }
168             } until ($success || ($retries > MAX_RETRIES));
169            
170             unless ($success) {
171             Helios::Error::LoggingError->throw(__PACKAGE__."->logMsg() ERROR: $err");
172             }
173            
174             return 1;
175             }
176              
177             1;
178             __END__