blib/lib/Google/Ads/AdWords/Logging.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 104 CC 8 SCAR 22.5
CC
8
Cov
0%
initialize_logging
30.0
enable_awapi_logging
6.0
enable_soap_logging
6.0
CRAP 9.5
1 # Copyright 2011, Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Ads::AdWords::Logging;
16  
17 use strict;
18 use version;
19  
20 # The following needs to be on one line because CPAN uses a particularly hacky
21 # eval() to determine module versions.
22 use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
23  
24 use File::HomeDir;
25 use File::Spec;
26 use Log::Log4perl qw(get_logger :levels);
27  
28 # Module initialization.
29 # This the log4perl configration format.
30 my $logs_folder = File::Spec->catfile(File::HomeDir->my_home, "logs");
31 my $awapi_log_file = File::Spec->catfile($logs_folder, "aw_api_lib.log");
32 my $soap_log_file = File::Spec->catfile($logs_folder, "soap_xml.log");
33 my $default_conf = <<TEXT;
34 log4perl.category.Google.Ads.AdWords.AWAPI = INFO, LogAWfile
35 log4perl.appender.LogAWfile = Log::Log4perl::Appender::File
36 log4perl.appender.LogAWfile.filename = ${awapi_log_file}
37 log4perl.appender.LogAWfile.create_at_logtime = 1
38 log4perl.appender.LogAWfile.layout = Log::Log4perl::Layout::PatternLayout
39 log4perl.appender.LogAWfile.layout.ConversionPattern = [%d{DATE} - %-5p] %m%n
40  
41 log4perl.category.Google.Ads.AdWords.SOAP_XML = INFO, LogSOAPfile
42 log4perl.appender.LogSOAPfile = Log::Log4perl::Appender::File
43 log4perl.appender.LogSOAPfile.filename = ${soap_log_file}
44 log4perl.appender.LogSOAPfile.create_at_logtime = 1
45 log4perl.appender.LogSOAPfile.layout = Log::Log4perl::Layout::PatternLayout
46 log4perl.appender.LogSOAPfile.layout.ConversionPattern = [%d{DATE} - %-5p] %m%n
47 TEXT
48  
49 # Static module-level variables.
50 my ($awapi_logger, $soap_logger);
51  
52 # Initializes Log4Perl infrastructure
53 sub initialize_logging {
54 # Only initialize once
55 unless (Log::Log4perl->initialized()) {
56 # Trying to read from the log4perl.conf file if not configuring the
57 # defaults.
58 my $log4perl_conf =
59 File::Spec->catfile(File::HomeDir->my_home, "log4perl.conf");
60 if (-r $log4perl_conf) {
61 Log::Log4perl->init($log4perl_conf);
62 } else {
63 mkdir ${logs_folder} unless -d ${logs_folder};
64 Log::Log4perl->init(\$default_conf);
65 }
66 }
67  
68 # Log4Perl may be initialized by another package; check that our loggers are
69 # set up
70 unless ($awapi_logger and $soap_logger) {
71 $awapi_logger = get_logger("Google::Ads::AdWords::AWAPI");
72 $soap_logger = get_logger("Google::Ads::AdWords::SOAP_XML");
73 $awapi_logger->level($OFF);
74 $soap_logger->level($OFF);
75 }
76 }
77  
78 # Enables the logging of the AdWords API takes one boolean
79 # parameter, if true enables the logging in debug (more verbose) mode.
80 sub enable_awapi_logging {
81 initialize_logging();
82 if ($_[0]) {
83 $awapi_logger->level($DEBUG);
84 } else {
85 $awapi_logger->level($INFO);
86 }
87 }
88  
89 # Disables all AdWords API logging.
90 sub disable_awapi_logging {
91 $awapi_logger->level($OFF);
92 }
93  
94 # Enables the logging of the SOAP Traffic (request and responses).
95 sub enable_soap_logging {
96 initialize_logging();
97 if ($_[0]) {
98 $soap_logger->level($DEBUG);
99 } else {
100 $soap_logger->level($INFO);
101 }
102 }
103  
104 # Disables all SOAP Traffic logging.
105 sub disable_soap_logging {
106 $soap_logger->level($OFF);
107 }
108  
109 # Enables all logging which includes SOAP Traffic logging and API Errors
110 # logging.
111 sub enable_all_logging {
112 initialize_logging();
113 if ($_[0]) {
114 $awapi_logger->level($DEBUG);
115 $soap_logger->level($DEBUG);
116 } else {
117 $awapi_logger->level($INFO);
118 $soap_logger->level($INFO);
119 }
120 }
121  
122 # Disables all logging which includes SOAP Traffic logging and API Errors
123 # logging.
124 sub disable_all_logging {
125 $soap_logger->level($OFF);
126 $awapi_logger->level($OFF);
127 }
128  
129 # Retrieves the SOAP logger used to log SOAP requests and responses.
130 sub get_soap_logger {
131 initialize_logging();
132 return $soap_logger;
133 }
134  
135 # Retrieves the AWAPI logger used to log messages and errors.
136 sub get_awapi_logger {
137 initialize_logging();
138 return $awapi_logger;
139 }
140  
141 return 1;
142  
143 =pod
144  
145 =head1 NAME
146  
147 Google::Ads::AdWords::Logging
148  
149 =head1 DESCRIPTION
150  
151 The class L<Google::Ads::AdWords::Logging> allows logging of outgoing and
152 incoming SOAP XML messages as API calls are executed. It initializes loggers
153 based on a provided log4perl.conf file or if the file is not found then based
154 on default parameters. It contains method to retrieve the message loggers.
155  
156 =head1 METHODS
157  
158 =head2 disable_all_logging
159  
160 Stops all logging.
161  
162 =head2 disable_awapi_logging
163  
164 Disables all logging for program errors and messages.
165  
166 =head2 disable_soap_logging
167  
168 Disables all logging for soap traffic, request and responses.
169  
170 =head2 enable_all_logging
171  
172 Enables all logging.
173  
174 =head3 Parameters
175  
176 A boolean if true will also include the logging of low level DEBUG messages.
177  
178 =head2 enable_awapi_logging
179  
180 Enables all logging for program errors and messages.
181  
182 =head3 Parameters
183  
184 A boolean if true will also include the logging of low level DEBUG messages.
185  
186 =head2 enable_soap_logging
187  
188 Enables all logging for soap traffic, request and responses.
189  
190 =head3 Parameters
191  
192 A boolean if true will also include the logging of low level DEBUG messages.
193  
194 =head2 get_awapi_logger
195  
196 Retrieves the program errors/messages logger.
197  
198 =head3 Returns
199  
200 A log4perl logger for program errors/messages logger.
201  
202 =head2 get_soap_logger
203  
204 Retrieves the soap request/response logger.
205  
206 =head3 Returns
207  
208 A log4perl logger for logging soap traffic.
209  
210 =head1 LICENSE AND COPYRIGHT
211  
212 Copyright 2011 Google Inc.
213  
214 Licensed under the Apache License, Version 2.0 (the "License");
215 you may not use this file except in compliance with the License.
216 You may obtain a copy of the License at
217  
218 http://www.apache.org/licenses/LICENSE-2.0
219  
220 Unless required by applicable law or agreed to in writing, software
221 distributed under the License is distributed on an "AS IS" BASIS,
222 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
223 See the License for the specific language governing permissions and
224 limitations under the License.
225  
226 =head1 AUTHOR
227  
228 David Torres E<lt>api.davidtorres at gmail.comE<gt>
229  
230 =head1 REPOSITORY INFORMATION
231  
232 $Rev: $
233 $LastChangedBy: $
234 $Id: $
235  
236 =cut