File Coverage

blib/lib/Helios/Logger.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Helios::Logger;
2              
3 1     1   952 use 5.008;
  1         3  
  1         33  
4 1     1   4 use strict;
  1         1  
  1         25  
5 1     1   3 use warnings;
  1         1  
  1         23  
6              
7 1     1   375 use Helios::Job;
  0            
  0            
8             use Helios::Error::LoggingError;
9             use Helios::ObjectDriver::DBI;
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             sub debug { my $self = shift; @_ ? $Debug = shift : $Debug; }
79              
80             sub setConfig {
81             my $var = $_[0]."::Config";
82             no strict 'refs';
83             $$var = $_[1];
84             }
85             sub getConfig {
86             my $var = $_[0]."::Config";
87             no strict 'refs';
88             return $$var;
89             }
90              
91             sub setJobType {
92             my $var = $_[0]."::JobType";
93             no strict 'refs';
94             $$var = $_[1];
95             }
96             sub getJobType {
97             my $var = $_[0]."::JobType";
98             no strict 'refs';
99             return $$var;
100             }
101              
102             # BEGIN CODE Copyright (C) 2014 by Logical Helion, LLC.
103             sub setService {
104             setJobType(@_);
105             }
106             sub getService {
107             getJobType(@_);
108             }
109             # END CODE Copyright (C) 2014 by Logical Helion, LLC.
110              
111              
112             sub setHostname {
113             my $var = $_[0]."::Hostname";
114             no strict 'refs';
115             $$var = $_[1];
116             }
117             sub getHostname {
118             my $var = $_[0]."::Hostname";
119             no strict 'refs';
120             return $$var;
121             }
122              
123             sub setDriver {
124             my $var = $_[0]."::Driver";
125             no strict 'refs';
126             $$var = $_[1];
127             }
128             sub getDriver {
129             return initDriver(@_);
130             }
131             sub initDriver {
132             my $self = shift;
133             my $config = $self->getConfig();
134             if ($self->debug) { print $config->{dsn},$config->{user},$config->{password},"\n"; }
135             my $driver = Helios::ObjectDriver::DBI->new(
136             dsn => $config->{dsn},
137             username => $config->{user},
138             password => $config->{password}
139             );
140             if ($self->debug) { print 'Logger->initDriver() DRIVER: ',$driver,"\n"; }
141             $self->setDriver($driver);
142             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             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             throw Helios::Error::LoggingError('logMsg() not defined for '. __PACKAGE__);
187             }
188              
189              
190             1;
191             __END__