File Coverage

blib/lib/Helios/Logger.pm
Criterion Covered Total %
statement 38 68 55.8
branch 0 6 0.0
condition n/a
subroutine 13 27 48.1
pod 2 14 14.2
total 53 115 46.0


line stmt bran cond sub pod time code
1             package Helios::Logger;
2              
3 1     1   903 use 5.008;
  1         4  
4 1     1   6 use strict;
  1         1  
  1         28  
5 1     1   5 use warnings;
  1         1  
  1         31  
6              
7 1     1   552 use Helios::Job;
  1         1  
  1         30  
8 1     1   5 use Helios::Error::LoggingError;
  1         2  
  1         13  
9 1     1   48 use Helios::ObjectDriver::DBI;
  1         2  
  1         6  
10              
11             our $VERSION = '2.81';
12              
13             =head1 NAME
14              
15             Helios::Logger - Base class for sending Helios logging information to external loggers
16              
17             =head1 SYNOPSIS
18              
19             # in your logging system shim class
20             use strict;
21             use warnings;
22             use base qw(Helios::Logger);
23             # optional, but probably useful
24             use Helios::LogEntry::Levels qw(:all);
25             use Helios::Error::LoggingError;
26            
27             sub init {
28             my $self = shift;
29              
30             ...initialization code for your logging system...
31             }
32            
33             sub logMsg {
34             my $self = shift;
35             my $job = shift;
36             my $priority_level = shift;
37             my $log_message = shift;
38             ...code to log the message to your logging system...
39             }
40              
41              
42             =head1 DESCRIPTION
43              
44             Helios::Logger is the base class to extend to provide interfaces to external
45             logging systems for Helios services. It provides a basic set of accessor
46             methods to get and store information about the Helios environment, and defines
47             interface methods for you to override in your class to actually implement the
48             logging code.
49              
50             It should be noted that all methods are actually class methods, not instance
51             (object) methods, because Helios::Logger subclasses are not instantiated. If
52             you need to implement other methods outside of the interface methods defined
53             below, make sure you implement them as class methods.
54              
55             =head1 ACCESSOR METHODS
56              
57             Helios::Logger provides 4 set/get accessor pairs to provide access to
58             information from the Helios collective environment:
59              
60             set/getConfig(); # config hashref from Helios::Service->getConfig()
61             set/getJobType(); # job type/service name string from Helios::Service->getJobType()
62             set/getHostname(); # host job is running on from Helios::Service->getHostname()
63             set/getDriver(); # Data::ObjectDriver object connected to Helios collective
64             database from Helios::Service->getDriver()
65            
66             Normally, Helios::Service->logMsg() calls the set* methods before the init()
67             method to properly initialize the logging class. If these values can be
68             adjusted in the init() method if necessary.
69              
70             =cut
71              
72             my $Config;
73             my $JobType;
74             my $Hostname;
75             my $Driver;
76             my $Debug;
77              
78 0 0   0 0   sub debug { my $self = shift; @_ ? $Debug = shift : $Debug; }
  0            
79              
80             sub setConfig {
81 0     0 0   my $var = $_[0]."::Config";
82 1     1   81 no strict 'refs';
  1         2  
  1         52  
83 0           $$var = $_[1];
84             }
85             sub getConfig {
86 0     0 0   my $var = $_[0]."::Config";
87 1     1   4 no strict 'refs';
  1         2  
  1         40  
88 0           return $$var;
89             }
90              
91             sub setJobType {
92 0     0 0   my $var = $_[0]."::JobType";
93 1     1   4 no strict 'refs';
  1         3  
  1         47  
94 0           $$var = $_[1];
95             }
96             sub getJobType {
97 0     0 0   my $var = $_[0]."::JobType";
98 1     1   4 no strict 'refs';
  1         2  
  1         74  
99 0           return $$var;
100             }
101              
102             # BEGIN CODE Copyright (C) 2014 by Logical Helion, LLC.
103             sub setService {
104 0     0 0   setJobType(@_);
105             }
106             sub getService {
107 0     0 0   getJobType(@_);
108             }
109             # END CODE Copyright (C) 2014 by Logical Helion, LLC.
110              
111              
112             sub setHostname {
113 0     0 0   my $var = $_[0]."::Hostname";
114 1     1   5 no strict 'refs';
  1         1  
  1         46  
115 0           $$var = $_[1];
116             }
117             sub getHostname {
118 0     0 0   my $var = $_[0]."::Hostname";
119 1     1   4 no strict 'refs';
  1         1  
  1         44  
120 0           return $$var;
121             }
122              
123             sub setDriver {
124 0     0 0   my $var = $_[0]."::Driver";
125 1     1   3 no strict 'refs';
  1         2  
  1         220  
126 0           $$var = $_[1];
127             }
128             sub getDriver {
129 0     0 0   return initDriver(@_);
130             }
131             sub initDriver {
132 0     0 0   my $self = shift;
133 0           my $config = $self->getConfig();
134 0 0         if ($self->debug) { print $config->{dsn},$config->{user},$config->{password},"\n"; }
  0            
135             my $driver = Helios::ObjectDriver::DBI->new(
136             dsn => $config->{dsn},
137             username => $config->{user},
138             password => $config->{password}
139 0           );
140 0 0         if ($self->debug) { print 'Logger->initDriver() DRIVER: ',$driver,"\n"; }
  0            
141 0           $self->setDriver($driver);
142 0           return $driver;
143             }
144              
145              
146             =head1 INTERFACE METHODS
147              
148             The following methods must be implemented by your Helios::Logger subclass for
149             external logging to work.
150              
151             =head2 init()
152              
153             Class method to initialize the logging code for the external logging system.
154             This will be run once when the service daemon starts. You also can use init()
155             to check for configuration errors or adjust configuration parameters
156             (set/getConfig()). If your logging system doesn't need any
157             initialization, go ahead and declare an empty init() method in your class
158             anyway.
159              
160             =cut
161              
162             sub init {
163 0     0 1   throw Helios::Error::LoggingError('init() not defined for '. __PACKAGE__);
164             }
165              
166              
167             =head2 logMsg($job, $priority_level, $message)
168              
169             Class method to send the message to the external logging system for logging.
170             The $job parameter is a Helios::Job object, while $priority_level is an integer
171             corresponding to one of the symbols defined in Helios::LogEntry::Levels (which
172             are the same as Sys::Syslog's). The implementation of this method in your
173             subclass should use the external logging system to log these values
174             appropriately. It should throw a Helios::Error::LoggingError if it encounters
175             an error.
176              
177             NOTE: When Helios::Service->logMsg() calls this method, it is guaranteed to
178             have three values, so when implementing this method in your subclass you don't
179             have to worry about parameter parsing as much as the calling routine does.
180             However, the values of $job and/or $priority_level may be undefined values, and
181             your implementation of logMsg() will have to deal with that appropriately.
182              
183             =cut
184              
185             sub logMsg {
186 0     0 1   throw Helios::Error::LoggingError('logMsg() not defined for '. __PACKAGE__);
187             }
188              
189              
190             1;
191             __END__