Skip to content

Commit 2a55b90

Browse files
committed
Merge pull request googleapis#394 from whatthejeff/logging
Add a PSR-3 style logging system that should be compatible with PHP 5.2+
2 parents 281602f + 4a0aa72 commit 2a55b90

21 files changed

+1513
-48
lines changed

src/Google/Auth/AppIdentity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public function sign(Google_Http_Request $request)
9292
// No token, so nothing to do.
9393
return $request;
9494
}
95+
96+
$this->client->getLogger()->debug('App Identity authentication');
97+
9598
// Add the OAuth2 header to the request
9699
$request->setRequestHeaders(
97100
array('Authorization' => 'Bearer ' . $this->token['access_token'])

src/Google/Auth/OAuth2.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,21 @@ public function sign(Google_Http_Request $request)
233233
if ($this->assertionCredentials) {
234234
$this->refreshTokenWithAssertion();
235235
} else {
236+
$this->client->getLogger()->debug('OAuth2 access token expired');
236237
if (! array_key_exists('refresh_token', $this->token)) {
237-
throw new Google_Auth_Exception(
238-
"The OAuth 2.0 access token has expired,"
239-
." and a refresh token is not available. Refresh tokens"
240-
." are not returned for responses that were auto-approved."
241-
);
238+
$error = "The OAuth 2.0 access token has expired,"
239+
." and a refresh token is not available. Refresh tokens"
240+
." are not returned for responses that were auto-approved.";
241+
242+
$this->client->getLogger()->error($error);
243+
throw new Google_Auth_Exception($error);
242244
}
243245
$this->refreshToken($this->token['refresh_token']);
244246
}
245247
}
246248

249+
$this->client->getLogger()->debug('OAuth2 authentication');
250+
247251
// Add the OAuth2 header to the request
248252
$request->setRequestHeaders(
249253
array('Authorization' => 'Bearer ' . $this->token['access_token'])
@@ -295,6 +299,7 @@ public function refreshTokenWithAssertion($assertionCredentials = null)
295299
}
296300
}
297301

302+
$this->client->getLogger()->debug('OAuth2 access token expired');
298303
$this->refreshTokenRequest(
299304
array(
300305
'grant_type' => 'assertion',
@@ -314,6 +319,14 @@ public function refreshTokenWithAssertion($assertionCredentials = null)
314319

315320
private function refreshTokenRequest($params)
316321
{
322+
if (isset($params['assertion'])) {
323+
$this->client->getLogger()->info(
324+
'OAuth2 access token refresh with Signed JWT assertion grants.'
325+
);
326+
} else {
327+
$this->client->getLogger()->info('OAuth2 access token refresh');
328+
}
329+
317330
$http = new Google_Http_Request(
318331
self::OAUTH2_TOKEN_URI,
319332
'POST',

src/Google/Auth/Simple.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public function sign(Google_Http_Request $request)
5454
{
5555
$key = $this->client->getClassConfig($this, 'developer_key');
5656
if ($key) {
57+
$this->client->getLogger()->debug(
58+
'Simple API Access developer key authentication'
59+
);
5760
$request->setQueryParam('key', $key);
5861
}
5962
return $request;

src/Google/Cache/Apc.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
17+
1818
require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
1919

2020
/**
@@ -27,11 +27,21 @@
2727
*/
2828
class Google_Cache_Apc extends Google_Cache_Abstract
2929
{
30+
/**
31+
* @var Google_Client the current client
32+
*/
33+
private $client;
34+
3035
public function __construct(Google_Client $client)
3136
{
3237
if (! function_exists('apc_add') ) {
33-
throw new Google_Cache_Exception("Apc functions not available");
38+
$error = "Apc functions not available";
39+
40+
$client->getLogger()->error($error);
41+
throw new Google_Cache_Exception($error);
3442
}
43+
44+
$this->client = $client;
3545
}
3646

3747
/**
@@ -41,12 +51,26 @@ public function get($key, $expiration = false)
4151
{
4252
$ret = apc_fetch($key);
4353
if ($ret === false) {
54+
$this->client->getLogger()->debug(
55+
'APC cache miss',
56+
array('key' => $key)
57+
);
4458
return false;
4559
}
4660
if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) {
61+
$this->client->getLogger()->debug(
62+
'APC cache miss (expired)',
63+
array('key' => $key, 'var' => $ret)
64+
);
4765
$this->delete($key);
4866
return false;
4967
}
68+
69+
$this->client->getLogger()->debug(
70+
'APC cache hit',
71+
array('key' => $key, 'var' => $ret)
72+
);
73+
5074
return $ret['data'];
5175
}
5276

@@ -55,10 +79,21 @@ public function get($key, $expiration = false)
5579
*/
5680
public function set($key, $value)
5781
{
58-
$rc = apc_store($key, array('time' => time(), 'data' => $value));
82+
$var = array('time' => time(), 'data' => $value);
83+
$rc = apc_store($key, $var);
84+
5985
if ($rc == false) {
86+
$this->client->getLogger()->error(
87+
'APC cache set failed',
88+
array('key' => $key, 'var' => $var)
89+
);
6090
throw new Google_Cache_Exception("Couldn't store data");
6191
}
92+
93+
$this->client->getLogger()->debug(
94+
'APC cache set',
95+
array('key' => $key, 'var' => $var)
96+
);
6297
}
6398

6499
/**
@@ -67,6 +102,10 @@ public function set($key, $value)
67102
*/
68103
public function delete($key)
69104
{
105+
$this->client->getLogger()->debug(
106+
'APC cache delete',
107+
array('key' => $key)
108+
);
70109
apc_delete($key);
71110
}
72111
}

src/Google/Cache/File.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,37 @@ class Google_Cache_File extends Google_Cache_Abstract
3131
private $path;
3232
private $fh;
3333

34+
/**
35+
* @var Google_Client the current client
36+
*/
37+
private $client;
38+
3439
public function __construct(Google_Client $client)
3540
{
36-
$this->path = $client->getClassConfig($this, 'directory');
41+
$this->client = $client;
42+
$this->path = $this->client->getClassConfig($this, 'directory');
3743
}
38-
44+
3945
public function get($key, $expiration = false)
4046
{
4147
$storageFile = $this->getCacheFile($key);
4248
$data = false;
43-
49+
4450
if (!file_exists($storageFile)) {
51+
$this->client->getLogger()->debug(
52+
'File cache miss',
53+
array('key' => $key, 'file' => $storageFile)
54+
);
4555
return false;
4656
}
4757

4858
if ($expiration) {
4959
$mtime = filemtime($storageFile);
5060
if ((time() - $mtime) >= $expiration) {
61+
$this->client->getLogger()->debug(
62+
'File cache miss (expired)',
63+
array('key' => $key, 'file' => $storageFile)
64+
);
5165
$this->delete($key);
5266
return false;
5367
}
@@ -59,6 +73,11 @@ public function get($key, $expiration = false)
5973
$this->unlock($storageFile);
6074
}
6175

76+
$this->client->getLogger()->debug(
77+
'File cache hit',
78+
array('key' => $key, 'file' => $storageFile, 'var' => $data)
79+
);
80+
6281
return $data;
6382
}
6483

@@ -71,17 +90,36 @@ public function set($key, $value)
7190
$data = serialize($value);
7291
$result = fwrite($this->fh, $data);
7392
$this->unlock($storageFile);
93+
94+
$this->client->getLogger()->debug(
95+
'File cache set',
96+
array('key' => $key, 'file' => $storageFile, 'var' => $value)
97+
);
98+
} else {
99+
$this->client->getLogger()->notice(
100+
'File cache set failed',
101+
array('key' => $key, 'file' => $storageFile)
102+
);
74103
}
75104
}
76105

77106
public function delete($key)
78107
{
79108
$file = $this->getCacheFile($key);
80109
if (file_exists($file) && !unlink($file)) {
110+
$this->client->getLogger()->error(
111+
'File cache delete failed',
112+
array('key' => $key, 'file' => $file)
113+
);
81114
throw new Google_Cache_Exception("Cache file could not be deleted");
82115
}
116+
117+
$this->client->getLogger()->debug(
118+
'File cache delete',
119+
array('key' => $key, 'file' => $file)
120+
);
83121
}
84-
122+
85123
private function getWriteableCacheFile($file)
86124
{
87125
return $this->getCacheFile($file, true);
@@ -91,7 +129,7 @@ private function getCacheFile($file, $forWrite = false)
91129
{
92130
return $this->getCacheDir($file, $forWrite) . '/' . md5($file);
93131
}
94-
132+
95133
private function getCacheDir($file, $forWrite)
96134
{
97135
// use the first 2 characters of the hash as a directory prefix
@@ -100,26 +138,34 @@ private function getCacheDir($file, $forWrite)
100138
$storageDir = $this->path . '/' . substr(md5($file), 0, 2);
101139
if ($forWrite && ! is_dir($storageDir)) {
102140
if (! mkdir($storageDir, 0755, true)) {
141+
$this->client->getLogger()->error(
142+
'File cache creation failed',
143+
array('dir' => $storageDir)
144+
);
103145
throw new Google_Cache_Exception("Could not create storage directory: $storageDir");
104146
}
105147
}
106148
return $storageDir;
107149
}
108-
150+
109151
private function acquireReadLock($storageFile)
110152
{
111153
return $this->acquireLock(LOCK_SH, $storageFile);
112154
}
113-
155+
114156
private function acquireWriteLock($storageFile)
115157
{
116158
$rc = $this->acquireLock(LOCK_EX, $storageFile);
117159
if (!$rc) {
160+
$this->client->getLogger()->notice(
161+
'File cache write lock failed',
162+
array('file' => $storageFile)
163+
);
118164
$this->delete($storageFile);
119165
}
120166
return $rc;
121167
}
122-
168+
123169
private function acquireLock($type, $storageFile)
124170
{
125171
$mode = $type == LOCK_EX ? "w" : "r";
@@ -134,7 +180,7 @@ private function acquireLock($type, $storageFile)
134180
}
135181
return true;
136182
}
137-
183+
138184
public function unlock($storageFile)
139185
{
140186
if ($this->fh) {

0 commit comments

Comments
 (0)