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

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 106 CC 8 SCAR 22.1
CC
8
Cov
0%
initialize_logging
30.0
enable_awapi_logging
6.0
enable_soap_logging
6.0
CRAP 9.1
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 warnings;
19 use version;
20  
21 # The following needs to be on one line because CPAN uses a particularly hacky
22 # eval() to determine module versions.
23 use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
24  
25 use File::HomeDir;
26 use File::Spec;
27 use Log::Log4perl qw(get_logger :levels);
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 = <<TEXT;
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 unless (Log::Log4perl->initialized()) {
57 # Trying to read from the log4perl.conf file if not configuring the
58 # defaults.
59 my $log4perl_conf =
60 File::Spec->catfile(File::HomeDir->my_home, "log4perl.conf");
61 if (-r $log4perl_conf) {
62 Log::Log4perl->init($log4perl_conf);
63 } else {
64 mkdir ${logs_folder} unless -d ${logs_folder};
65 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 unless ($awapi_logger and $soap_logger) {
72 $awapi_logger = get_logger("Google::Ads::AdWords::AWAPI");
73 $soap_logger = get_logger("Google::Ads::AdWords::SOAP_XML");
74 $awapi_logger->level($OFF);
75 $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 initialize_logging();
83 if ($_[0]) {
84 $awapi_logger->level($DEBUG);
85 } else {
86 $awapi_logger->level($INFO);
87 }
88 }
89  
90 # Disables all AdWords API logging.
91 sub disable_awapi_logging {
92 $awapi_logger->level($OFF);
93 }
94  
95 # Enables the logging of the SOAP Traffic (request and responses).
96 sub enable_soap_logging {
97 initialize_logging();
98 if ($_[0]) {
99 $soap_logger->level($DEBUG);
100 } else {
101 $soap_logger->level($INFO);
102 }
103 }
104  
105 # Disables all SOAP Traffic logging.
106 sub disable_soap_logging {
107 $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 initialize_logging();
114 if ($_[0]) {
115 $awapi_logger->level($DEBUG);
116 $soap_logger->level($DEBUG);
117 } else {
118 $awapi_logger->level($INFO);
119 $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 $soap_logger->level($OFF);
127 $awapi_logger->level($OFF);
128 }
129  
130 # Retrieves the SOAP logger used to log SOAP requests and responses.
131 sub get_soap_logger {
132 initialize_logging();
133 return $soap_logger;
134 }
135  
136 # Retrieves the AWAPI logger used to log messages and errors.
137 sub get_awapi_logger {
138 initialize_logging();
139 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<Google::Ads::AdWords::Logging> 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