Logging/GoogleAdsLogger.pm

filter: Statementstmt 89.1% 90 / 101 Branchbran 73.5% 25 / 34 Conditioncond 85.7% 18 / 21 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 83.3% 20 / 24 Podpod 100% 11 / 11 totaltotal 84.5% 170 / 201 CC 25 SCAR 14.8
CC
25
Cov
85%
log_summary
8.3
initialize_logging
7.0
enable_summary_logging
6.0
CRAP 4.4
« Logging/DetailStats.pm ↑ Summary Common/OAuth2BaseHandler.pm »
1 # Copyright 2019, Google LLC
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 # The logger for outgoing and incoming REST messages as API calls.
16  
17 package Google::Ads::GoogleAds::Logging::GoogleAdsLogger;
18  
19 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # The following needs to be on one line because CPAN uses a particularly hacky
24 # eval() to determine module versions.
25 11 ▶use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use Google::Ads::GoogleAds::Logging::SummaryStats;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶use Google::Ads::GoogleAds::Logging::DetailStats;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 11 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 11 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # Module initialization.
35 # This is the log4perl configuration format.
36 11 my $logs_folder = File::Spec->catfile(File::HomeDir->my_home, "logs");
37 11 my $summary_log_file = File::Spec->catfile($logs_folder, "summary.log");
38 11 my $detail_log_file = File::Spec->catfile($logs_folder, "detail.log");
39 11 my $default_conf = <<TEXT;
40 log4perl.category.Google.Ads.GoogleAds.Summary = INFO, SummaryFile
41 log4perl.appender.SummaryFile = Log::Log4perl::Appender::File
42 log4perl.appender.SummaryFile.filename = ${summary_log_file}
43 log4perl.appender.SummaryFile.create_at_logtime = 1
44 log4perl.appender.SummaryFile.mkpath = 1
45 log4perl.appender.SummaryFile.layout = Log::Log4perl::Layout::PatternLayout
46 log4perl.appender.SummaryFile.layout.ConversionPattern = [%d{DATE} - %-5p] %m%n
47  
48 log4perl.category.Google.Ads.GoogleAds.Detail = INFO, DetailFile
49 log4perl.appender.DetailFile = Log::Log4perl::Appender::File
50 log4perl.appender.DetailFile.filename = ${detail_log_file}
51 log4perl.appender.DetailFile.create_at_logtime = 1
52 log4perl.appender.DetailFile.mkpath = 1
53 log4perl.appender.DetailFile.layout = Log::Log4perl::Layout::PatternLayout
54 log4perl.appender.DetailFile.layout.ConversionPattern = [%d{DATE} - %-5p] %m%n
55 TEXT
56  
57 # Static module-level variables.
58 11 my ($summary_logger, $detail_logger);
59  
60 # Initializes Log4Perl infrastructure.
61 13 ▶sub initialize_logging {
Subroutine initialize_logging: called CC=7 SCAR=19.5
Pod documented
62 # Only initialize once.
63 13 ▶ unless (Log::Log4perl->initialized()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Log::Log4perl"->initialized) 6 7 13
64 6 my $log4perl_conf = shift;
65 # Trying to read from the ~/log4perl.conf file if no .conf file is specified as a parameter.
66 6 ▶ $log4perl_conf =
Branch: 1 branch
BranchTrueFalseTotal
unless defined $log4perl_conf 5 1 6
67 File::Spec->catfile(File::HomeDir->my_home, "log4perl.conf")
68 unless defined $log4perl_conf;
69  
70 # Create the log files in rw-rw-rw- mode.
71 6 umask 0000;
72 6 ▶ if (-r $log4perl_conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $log4perl_conf) { } 1 5 6
73 1 Log::Log4perl->init($log4perl_conf);
74 } else {
75 5 ▶ mkdir ${logs_folder} unless -d ${logs_folder};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $logs_folder 1 4 5
76 5 Log::Log4perl->init(\$default_conf);
77 }
78 }
79  
80 # Log4Perl may be initialized by another package; check if the loggers are set up.
81 13 ▶ unless ($summary_logger and $detail_logger) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($summary_logger and $detail_logger) 6 7 13
Condition66%2/3
$summary_logger and $detail_logger
!ll&&!rl&&r
607
MC/DC50%1/2
$summary_logger and $detail_logger
$summary_logger $detail_logger
Truth table66%2/3
A and B
A= $summary_loggerB= $detail_logger
ABresult
0X0
100
111
82 6 $summary_logger = get_logger("Google::Ads::GoogleAds::Summary");
83 6 $detail_logger = get_logger("Google::Ads::GoogleAds::Detail");
84 }
85 }
86  
87 # Enables the summary logging. Takes one boolean parameter, to enable the logging
88 # in debug (more verbose) mode when set to true.
89 0 ▶sub enable_summary_logging {
Subroutine enable_summary_logging: not called CC=2 SCAR=17.9
Pod documented
90 0 initialize_logging();
91 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
92 0 $summary_logger->level($DEBUG);
93 } else {
94 0 $summary_logger->level($INFO);
95 }
96 }
97  
98 # Disables all summary logging.
99 0 ▶sub disable_summary_logging {
Subroutine disable_summary_logging: not called CC=1 SCAR=6.9
Pod documented
100 0 $summary_logger->level($OFF);
101 }
102  
103 # Enables the traffic detail (request and responses) logging. Takes one boolean
104 # parameter, to enable the logging in debug (more verbose) mode when set to true.
105 0 ▶sub enable_detail_logging {
Subroutine enable_detail_logging: not called CC=2 SCAR=17.9
Pod documented
106 0 initialize_logging();
107 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
108 0 $detail_logger->level($DEBUG);
109 } else {
110 0 $detail_logger->level($INFO);
111 }
112 }
113  
114 # Disables all traffic detail logging.
115 0 ▶sub disable_detail_logging {
Subroutine disable_detail_logging: not called CC=1 SCAR=6.9
Pod documented
116 0 $detail_logger->level($OFF);
117 }
118  
119 # Enables all logging including summary logging and traffic detail logging.
120 2 ▶sub enable_all_logging {
Subroutine enable_all_logging: called CC=2 SCAR=6.9
Pod documented
121 2 initialize_logging();
122 2 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 1 1 2
123 1 $summary_logger->level($DEBUG);
124 1 $detail_logger->level($DEBUG);
125 } else {
126 1 $summary_logger->level($INFO);
127 1 $detail_logger->level($INFO);
128 }
129 }
130  
131 # Disables all logging including summary logging and traffic detail logging.
132 2 ▶sub disable_all_logging {
Subroutine disable_all_logging: called CC=1 SCAR=0.0
Pod documented
133 2 $summary_logger->level($OFF);
134 2 $detail_logger->level($OFF);
135 }
136  
137 # Retrieves the summary logger used to log the one-line summary.
138 1 ▶sub get_summary_logger {
Subroutine get_summary_logger: called CC=1 SCAR=0.0
Pod documented
139 1 initialize_logging();
140 1 return $summary_logger;
141 }
142  
143 # Retrieves the detail logger used to log the traffic detail.
144 1 ▶sub get_detail_logger {
Subroutine get_detail_logger: called CC=1 SCAR=0.0
Pod documented
145 1 initialize_logging();
146 1 return $detail_logger;
147 }
148  
149 # Logs the one-line summary for each REST API request.
150 4 ▶sub log_summary {
Subroutine log_summary: called CC=8 SCAR=21.1
Pod documented
151 4 my ($http_request, $http_response) = @_;
152  
153 # Validate the response status and log level.
154 return
155 4 ▶ unless ($http_response->is_success and $summary_logger->is_info)
Branch: 1 branch
BranchTrueFalseTotal
unless $http_response->is_success and $summary_logger->is_info or not $http_response->is_success and $summary_logger->is_warn 1 3 4
Condition77%7/9
$http_response->is_success and $summary_logger->is_info or not $http_response->is_success and $summary_logger->is_warn
l!l&&r!l&&!r
211
$http_response->is_success and $summary_logger->is_info
!ll&&!rl&&r
202
not $http_response->is_success and $summary_logger->is_warn
!ll&&!rl&&r
011
MC/DC50%2/4
$http_response->is_success and $summary_logger->is_info or not $http_response->is_success and $summary_logger->is_warn
$http_response->is_success $summary_logger->is_info not $http_response->is_success $summary_logger->is_warn
Truth table42%3/7
A and B or not(C) and D
A= $http_response->is_successB= $summary_logger->is_infoC= not $http_response->is_successD= $summary_logger->is_warn
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
156 or (!$http_response->is_success and $summary_logger->is_warn);
157  
158 3 ▶ my $summary_stats = Google::Ads::GoogleAds::Logging::SummaryStats->new({
Branch: 2 branches
BranchTrueFalseTotal
$http_request->uri =~ m[customers/\d+] ? : 3 0 3
$http_response->header('request-id') ? : 3 0 3
159 host => __parse_host($http_request),
160 customer_id => $http_request->uri =~ /customers\/\d+/
161 ? $http_request->uri =~ /customers\/(\d+)/
162 : "",
163 # The service method name in which the logger is invoked.
164 method => (caller(3))[3],
165 request_id => $http_response->header("request-id")
166 ? $http_response->header("request-id")
167 : ""
168 });
169  
170 3 ▶ if ($http_response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_response->is_success) { } 2 1 3
171 2 $summary_logger->info($summary_stats);
172 } else {
173 1 $summary_stats->set_is_fault(1);
174 1 $summary_stats->set_fault_message(
175 __parse_fault_message($http_response->decoded_content));
176 1 $summary_logger->warn($summary_stats);
177 }
178 }
179  
180 # Full log of REST API traffic detail.
181 4 ▶sub log_detail {
Subroutine log_detail: called CC=6 SCAR=17.9
Pod documented
182 4 my ($http_request, $http_response) = @_;
183  
184 # Validate the response status and log level.
185 return
186 4 ▶ unless ($http_response->is_success and $detail_logger->is_debug)
Branch: 1 branch
BranchTrueFalseTotal
unless $http_response->is_success and $detail_logger->is_debug or not $http_response->is_success and $detail_logger->is_info 2 2 4
Condition100%9/9
$http_response->is_success and $detail_logger->is_debug or not $http_response->is_success and $detail_logger->is_info
l!l&&r!l&&!r
112
$http_response->is_success and $detail_logger->is_debug
!ll&&!rl&&r
211
not $http_response->is_success and $detail_logger->is_info
!ll&&!rl&&r
111
MC/DC75%3/4
$http_response->is_success and $detail_logger->is_debug or not $http_response->is_success and $detail_logger->is_info
$http_response->is_success $detail_logger->is_debug not $http_response->is_success $detail_logger->is_info
Truth table57%4/7
A and B or not(C) and D
A= $http_response->is_successB= $detail_logger->is_debugC= not $http_response->is_successD= $detail_logger->is_info
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
187 or (!$http_response->is_success and $detail_logger->is_info);
188  
189 2 my $detail_stats = Google::Ads::GoogleAds::Logging::DetailStats->new({
190 host => __parse_host($http_request),
191 # The service method name in which the logger is invoked.
192 method => (caller(3))[3],
193 request_headers => $http_request->headers,
194 request_content => $http_request->content,
195 response_headers => $http_response->headers
196 });
197  
198 2 ▶ if ($http_response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_response->is_success) { } 1 1 2
199 1 $detail_stats->set_response_content($http_response->decoded_content);
200 1 $detail_logger->debug($detail_stats);
201 } else {
202 1 $detail_stats->set_fault(__parse_faults($http_response->decoded_content));
203 1 $detail_logger->info($detail_stats);
204 }
205 }
206  
207 # Parses the host name from a HTTP request.
208 5 ▶sub __parse_host {
Subroutine __parse_host: called CC=1 SCAR=0.0
209 5 my $http_request = shift;
210 5 my $uri = $http_request->uri;
211 5 return $uri->scheme . "://" . $uri->host;
212 }
213  
214 # Parses the fault message from the HTTP response JSON payload.
215 1 ▶sub __parse_fault_message {
Subroutine __parse_fault_message: called CC=3 SCAR=11.2
216 1 my $response_content = shift;
217 1 my $response_body = decode_json($response_content);
218  
219 # When the fault is a GoogleAdsFailure.
220 my $fault_message =
221 1 $response_body->{error}{details}[0]{errors}[0]{message};
222  
223 # When the fault is a GRPC error, e.g. BadRequest, PreconditionFailure, QuotaFailure.
224 1 ▶ $fault_message = $response_body->{error}{message} if not $fault_message;
Branch: 1 branch
BranchTrueFalseTotal
unless $fault_message 0 1 1
225  
226 1 ▶ $fault_message = $response_content if not $fault_message;
Branch: 1 branch
BranchTrueFalseTotal
unless $fault_message 0 1 1
227  
228 1 return $fault_message;
229 }
230  
231 # Parses all the faults from the HTTP response JSON payload.
232 1 ▶sub __parse_faults {
Subroutine __parse_faults: called CC=2 SCAR=7.2
233 1 my $response_content = shift;
234 1 my $json_coder = JSON::XS->new->utf8->pretty;
235 1 my $response_body = $json_coder->decode($response_content);
236  
237 1 my $faults = $response_body->{error}{details}[0];
238 1 ▶ return $json_coder->encode($faults) if $faults;
Branch: 1 branch
BranchTrueFalseTotal
if $faults 1 0 1
239  
240 0 return $response_content;
241 }
242  
243 11 1;
244  
245 =pod
246  
247 =head1 NAME
248  
249 Google::Ads::GoogleAds::Logging::GoogleAdsLogger
250  
251 =head1 SYNOPSIS
252  
253 use Google::Ads::GoogleAds::Logging::GoogleAdsLogger;
254  
255 Google::Ads::GoogleAds::Logging::GoogleAdsLogger::enable_all_logging(1);
256  
257 Google::Ads::GoogleAds::Logging::GoogleAdsLogger::log_summary($http_request, $http_response);
258  
259 Google::Ads::GoogleAds::Logging::GoogleAdsLogger::log_detail($http_request, $http_response);
260  
261 =head1 DESCRIPTION
262  
263 This class allows logging of outgoing and incoming REST messages as executed API
264 calls. It initializes the loggers based on a provided F<log4perl.conf> file or
265 default parameters if the file is not found. It contains methods to retrieve the
266 summary and detail loggers.
267  
268 =head1 METHODS
269  
270 =head2 initialize_logging
271  
272 Initializes the loggers based on the default F<log4perl.conf> file or default
273 parameters if the file is not found.
274  
275 =head2 enable_summary_logging
276  
277 Enables the logging for the one-line summary.
278  
279 =head3 Parameters
280  
281 A boolean value of whether to include the DEBUG level messages.
282  
283 =head2 disable_summary_logging
284  
285 Disables the one-line summary logging.
286  
287 =head2 enable_detail_logging
288  
289 Enables the logging for traffic detail of HTTP request and response.
290  
291 =head3 Parameters
292  
293 A boolean value of whether to include the DEBUG level messages.
294  
295 =head2 disable_detail_logging
296  
297 Disables the traffic detail logging.
298  
299 =head2 enable_all_logging
300  
301 Enables all logging for the one-line summary and the traffic detail.
302  
303 =head3 Parameters
304  
305 A boolean value of whether to include the DEBUG level messages.
306  
307 =head2 disable_all_logging
308  
309 Stops all logging.
310  
311 =head2 get_summary_logger
312  
313 Retrieves the summary logger used to log the one-line summary.
314  
315 =head3 Returns
316  
317 A L<Log::Log4perl> logger for the one-line summary.
318  
319 =head2 get_detail_logger
320  
321 Retrieves the detail logger used to log the traffic detail.
322  
323 =head3 Returns
324  
325 A L<Log::Log4perl> logger for the traffic detail.
326  
327 =head2 log_summary
328  
329 Logs a one-line summary for each REST API request.
330  
331 =head3 Parameters
332  
333 =over
334  
335 =item *
336  
337 I<http_request>: The REST HTTP request sent to Google Ads API server.
338  
339 =item *
340  
341 I<http_response>: The HTTP response received from Google Ads API server.
342  
343 =back
344  
345 =head2 log_detail
346  
347 Full log of the traffic detail about the request/response payload.
348  
349 =head3 Parameters
350  
351 =over
352  
353 =item *
354  
355 I<http_request>: The REST HTTP request sent to Google Ads API server.
356  
357 =item *
358  
359 I<http_response>: The HTTP response received from Google Ads API server.
360  
361 =back
362  
363 =head2 __parse_host
364  
365 The private method to parse the hostname from a HTTP request.
366  
367 =head3 Parameters
368  
369 =over
370  
371 =item *
372  
373 I<http_request>: The REST HTTP request sent to Google Ads API server.
374  
375 =back
376  
377 =head3 Returns
378  
379 The parsed hostname in the format of <scheme>://<domain>.
380  
381 =head2 __parse_fault_message
382  
383 The private method to parse the fault message from the HTTP response, if an error
384 has occurred at the server side. This message can be used to construct a
385 L<Google::Ads::GoogleAds::Logging::SummaryStats>.
386  
387 =head2 __parse_faults
388  
389 The private method to parse all the faults from the HTTP response, and C<encode>
390 them in the JSON format. These faults will be used to construct a
391 L<Google::Ads::GoogleAds::Logging::DetailStats>.
392  
393 =head1 LICENSE AND COPYRIGHT
394  
395 Copyright 2019 Google LLC
396  
397 Licensed under the Apache License, Version 2.0 (the "License");
398 you may not use this file except in compliance with the License.
399 You may obtain a copy of the License at
400  
401 http://www.apache.org/licenses/LICENSE-2.0
402  
403 Unless required by applicable law or agreed to in writing, software
404 distributed under the License is distributed on an "AS IS" BASIS,
405 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
406 See the License for the specific language governing permissions and
407 limitations under the License.
408  
409 =head1 REPOSITORY INFORMATION
410  
411 $Rev: $
412 $LastChangedBy: $
413 $Id: $
414  
415 =cut
« Logging/DetailStats.pm ↑ Summary Common/OAuth2BaseHandler.pm »