Skip to content

Commit 83cbbeb

Browse files
authored
Fix laravel tutorial (GoogleCloudPlatform#829)
1 parent 73b58e4 commit 83cbbeb

File tree

8 files changed

+169
-88
lines changed

8 files changed

+169
-88
lines changed

appengine/php72/laravel-framework/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ laravel.com. This version was tested to work with `laravel/laravel-framework:^5.
3333

3434
env_variables:
3535
# Put production environment variables here.
36-
APP_LOG: errorlog
3736
APP_KEY: YOUR_APP_KEY
3837
APP_STORAGE: /tmp
38+
VIEW_COMPILED_PATH: /tmp
3939

4040
1. Copy the [`bootstrap/app.php`](bootstrap/app.php) file included in this sample
4141
into the `bootstrap` directory of your Laravel application. This file ensures
@@ -107,9 +107,9 @@ Laravel, you need to manually add the `DB_SOCKET` value to
107107

108108
env_variables:
109109
# Put production environment variables here.
110-
APP_LOG: errorlog
111110
APP_KEY: YOUR_APP_KEY
112111
APP_STORAGE: /tmp
112+
VIEW_COMPILED_PATH: /tmp
113113
CACHE_DRIVER: database
114114
SESSION_DRIVER: database
115115
## Set these environment variables according to your CloudSQL configuration.
@@ -172,6 +172,18 @@ You can write logs to Stackdriver Logging from PHP applications by using the Sta
172172
],
173173
```
174174

175+
1. Finally, set the environment variable `LOG_CHANNEL` in `app.yaml` to
176+
`stackdriver` to use the Stackdriver logger you created:
177+
178+
```yaml
179+
runtime: php72
180+
181+
env_variables:
182+
# Put production environment variables here.
183+
LOG_CHANNEL: stackdriver
184+
#...
185+
```
186+
175187
1. Now you can log to Stackdriver logging anywhere in your application!
176188

177189
```php
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
runtime: php72
22

33
env_variables:
4-
# Put production environment variables here.
5-
APP_LOG: errorlog
4+
## Put production environment variables here.
65
APP_KEY: YOUR_APP_KEY
76
APP_STORAGE: /tmp
7+
VIEW_COMPILED_PATH: /tmp
88
CACHE_DRIVER: database
99
SESSION_DRIVER: database
1010
## Set these environment variables according to your CloudSQL configuration.
1111
DB_DATABASE: YOUR_DB_DATABASE
1212
DB_USERNAME: YOUR_DB_USERNAME
1313
DB_PASSWORD: YOUR_DB_PASSWORD
1414
DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME"
15+
16+
## To use Stackdriver logging in your Laravel application, copy
17+
## "app/Logging/CreateStackdriverLogger.php" and "config/logging.php"
18+
## into your Laravel application. Then uncomment the following line:
19+
# LOG_CHANNEL: stackdriver
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
runtime: php72
22

33
env_variables:
4-
# Put production environment variables here.
5-
LOG_CHANNEL: stackdriver
4+
## Put production environment variables here.
65
APP_KEY: YOUR_APP_KEY
76
APP_STORAGE: /tmp
87
VIEW_COMPILED_PATH: /tmp
8+
9+
## To use Stackdriver logging in your Laravel application, copy
10+
## "app/Logging/CreateStackdriverLogger.php" and "config/logging.php"
11+
## into your Laravel application. Then uncomment the following line:
12+
# LOG_CHANNEL: stackdriver

appengine/php72/laravel-framework/config/logging.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@
6666
'level' => 'debug',
6767
],
6868

69+
# [START] Add Stackdriver Logging and Error Reporting to your Laraval application
6970
'stackdriver' => [
7071
'driver' => 'custom',
7172
'via' => App\Logging\CreateStackdriverLogger::class,
7273
'level' => 'debug',
7374
],
75+
# [END]
7476

7577
],
7678

appengine/php72/laravel-framework/test/DeployDatabaseTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class DeployDatabaseTest extends TestCase
2929

3030
public static function beforeDeploy()
3131
{
32+
// ensure logging output is displayed in phpunit
33+
self::$logger = new \Monolog\Logger('phpunit');
34+
3235
$connection = self::requireEnv('LARAVEL_CLOUDSQL_CONNECTION_NAME');
3336
$dbName = self::requireEnv('LARAVEL_DB_DATABASE');
3437
$dbUser = self::requireEnv('LARAVEL_DB_USERNAME');

appengine/php72/laravel-framework/test/DeployLaravelTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ private static function createLaravelProject()
4646
self::$gcloudWrapper->setDir($tmpDir);
4747
chdir($tmpDir);
4848

49+
// fix "beyondcode/laravel-dump-server" issue
50+
file_put_contents(
51+
'composer.json',
52+
json_encode(
53+
array_merge_recursive(
54+
json_decode(file_get_contents('composer.json'), true),
55+
['extra' => ['laravel' => ['dont-discover' => 'beyondcode/laravel-dump-server']]]
56+
),
57+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
58+
)
59+
);
60+
4961
return $tmpDir;
5062
}
5163

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
2+
/**
3+
* Copyright 2018 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\AppEngine\Laravel;
19+
20+
use Google\Cloud\Logging\LoggingClient;
21+
use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
22+
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
class DeployStackdriverTest extends TestCase
26+
{
27+
use DeployLaravelTrait;
28+
use AppEngineDeploymentTrait;
29+
use EventuallyConsistentTestTrait;
30+
31+
public static function beforeDeploy()
32+
{
33+
// ensure logging output is displayed in phpunit
34+
self::$logger = new \Monolog\Logger('phpunit');
35+
36+
$tmpDir = self::createLaravelProject();
37+
38+
// Uncomment the stackdriver logging channel line in app.yaml
39+
file_put_contents(
40+
$tmpDir . '/app.yaml',
41+
str_replace(
42+
'# LOG_CHANNEL: stackdriver',
43+
'LOG_CHANNEL: stackdriver',
44+
file_get_contents(__DIR__ . '/../app.yaml')
45+
)
46+
);
47+
48+
self::addAppKeyToAppYaml($tmpDir);
49+
50+
mkdir("$tmpDir/app/Logging", 0700, true);
51+
self::copyFiles([
52+
'routes/web.php',
53+
'config/logging.php',
54+
'app/Exceptions/Handler.php',
55+
'app/Logging/CreateStackdriverLogger.php',
56+
], $tmpDir);
57+
58+
// require google cloud logging and error reporting dependencies
59+
self::execute('composer require google/cloud-logging google/cloud-error-reporting');
60+
}
61+
62+
public function testLogging()
63+
{
64+
$logging = new LoggingClient([
65+
'projectId' => self::getProjectId()
66+
]);
67+
68+
$token = uniqid();
69+
// The routes are defined in routes/web.php
70+
$resp = $this->client->request('GET', "/log/$token", [
71+
'http_errors' => false
72+
]);
73+
$this->assertEquals('200', $resp->getStatusCode(), 'log page status code');
74+
75+
// 'app' is the default logname of our Stackdriver Logging integration.
76+
$logger = $logging->logger('app');
77+
$this->runEventuallyConsistentTest(function () use ($logger, $token) {
78+
$logs = $logger->entries([
79+
'pageSize' => 100,
80+
'orderBy' => 'timestamp desc',
81+
'resultLimit' => 100
82+
]);
83+
$found = false;
84+
foreach ($logs as $log) {
85+
$info = $log->info();
86+
if (false !== strpos($info['jsonPayload']['message'], "token: $token")) {
87+
$found = true;
88+
}
89+
}
90+
$this->assertTrue($found, "The log entry $token was not found");
91+
}, $eventuallyConsistentRetryCount = 5);
92+
}
93+
94+
public function testErrorReporting()
95+
{
96+
$logging = new LoggingClient([
97+
'projectId' => self::getProjectId()
98+
]);
99+
100+
$token = uniqid();
101+
// The routes are defined in routes/web.php
102+
$resp = $this->client->request('GET', "/exception/$token", [
103+
'http_errors' => false
104+
]);
105+
$this->assertEquals('500', $resp->getStatusCode(), 'exception page status code');
106+
107+
// 'app-error' is the default logname of our Stackdriver Error Reporting integration.
108+
$logger = $logging->logger('app-error');
109+
$this->runEventuallyConsistentTest(function () use ($logger, $token) {
110+
$logs = $logger->entries([
111+
'pageSize' => 100,
112+
'orderBy' => 'timestamp desc',
113+
'resultLimit' => 100
114+
]);
115+
$found = false;
116+
foreach ($logs as $log) {
117+
$info = $log->info();
118+
if (false !== strpos($info['jsonPayload']['message'], "token: $token")) {
119+
$found = true;
120+
}
121+
}
122+
$this->assertTrue($found, 'The log entry was not found');
123+
}, $eventuallyConsistentRetryCount = 5);
124+
}
125+
}

appengine/php72/laravel-framework/test/DeployTest.php

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717

1818
namespace Google\Cloud\Samples\AppEngine\Laravel;
1919

20-
use Google\Cloud\Logging\LoggingClient;
2120
use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
22-
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
2321
use PHPUnit\Framework\TestCase;
2422

2523
class DeployTest extends TestCase
2624
{
2725
use DeployLaravelTrait;
2826
use AppEngineDeploymentTrait;
29-
use EventuallyConsistentTestTrait;
3027

3128
public static function beforeDeploy()
3229
{
@@ -36,17 +33,6 @@ public static function beforeDeploy()
3633
$tmpDir = self::createLaravelProject();
3734
copy(__DIR__ . '/../app.yaml', $tmpDir . '/app.yaml');
3835
self::addAppKeyToAppYaml($tmpDir);
39-
40-
mkdir("$tmpDir/app/Logging", 0700, true);
41-
self::copyFiles([
42-
'routes/web.php',
43-
'config/logging.php',
44-
'app/Exceptions/Handler.php',
45-
'app/Logging/CreateStackdriverLogger.php',
46-
], $tmpDir);
47-
48-
// require google cloud logging and error reporting dependencies
49-
self::execute('composer require google/cloud-logging google/cloud-error-reporting');
5036
}
5137

5238
public function testHomepage()
@@ -56,72 +42,4 @@ public function testHomepage()
5642
$this->assertEquals('200', $resp->getStatusCode(), 'top page status code');
5743
$this->assertContains('Laravel', $resp->getBody()->getContents());
5844
}
59-
60-
public function testLogging()
61-
{
62-
// bump up the retry count because logs can take a bit to show up
63-
$this->eventuallyConsistentRetryCount = 5;
64-
65-
$logging = new LoggingClient([
66-
'projectId' => self::getProjectId()
67-
]);
68-
69-
$token = uniqid();
70-
// The routes are defined in routes/web.php
71-
$resp = $this->client->request('GET', "/log/$token", [
72-
'http_errors' => false
73-
]);
74-
$this->assertEquals('200', $resp->getStatusCode(), 'log page status code');
75-
76-
// 'app' is the default logname of our Stackdriver Logging integration.
77-
$logger = $logging->logger('app');
78-
$this->runEventuallyConsistentTest(function () use ($logger, $token) {
79-
$logs = $logger->entries([
80-
'pageSize' => 100,
81-
'orderBy' => 'timestamp desc',
82-
'resultLimit' => 100
83-
]);
84-
$found = false;
85-
foreach ($logs as $log) {
86-
$info = $log->info();
87-
if (false !== strpos($info['jsonPayload']['message'], "token: $token")) {
88-
$found = true;
89-
}
90-
}
91-
$this->assertTrue($found, "The log entry $token was not found");
92-
});
93-
}
94-
95-
public function testErrorReporting()
96-
{
97-
$this->eventuallyConsistentRetryCount = 5;
98-
$logging = new LoggingClient([
99-
'projectId' => self::getProjectId()
100-
]);
101-
102-
$token = uniqid();
103-
// The routes are defined in routes/web.php
104-
$resp = $this->client->request('GET', "/exception/$token", [
105-
'http_errors' => false
106-
]);
107-
$this->assertEquals('500', $resp->getStatusCode(), 'exception page status code');
108-
109-
// 'app-error' is the default logname of our Stackdriver Error Reporting integration.
110-
$logger = $logging->logger('app-error');
111-
$this->runEventuallyConsistentTest(function () use ($logger, $token) {
112-
$logs = $logger->entries([
113-
'pageSize' => 100,
114-
'orderBy' => 'timestamp desc',
115-
'resultLimit' => 100
116-
]);
117-
$found = false;
118-
foreach ($logs as $log) {
119-
$info = $log->info();
120-
if (false !== strpos($info['jsonPayload']['message'], "token: $token")) {
121-
$found = true;
122-
}
123-
}
124-
$this->assertTrue($found, 'The log entry was not found');
125-
});
126-
}
12745
}

0 commit comments

Comments
 (0)