File Coverage

blib/lib/Google/Ads/AdWords/Logging.pm
Criterion Covered Total %
statement 39 54 72.2
branch 7 14 50.0
condition 2 3 66.6
subroutine 11 16 68.7
pod 8 9 88.8
total 67 96 69.7


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